Runtime
The AgentRuntime is the core orchestration engine of elizaOS. It manages the complete lifecycle of an agent, coordinating plugins, services, actions, memory, and model interactions.Overview
The runtime is the central hub that:- Registers and initializes plugins
- Manages service lifecycle
- Composes state from providers
- Routes model inference requests
- Executes actions and evaluators
- Handles memory storage and retrieval
- Emits and listens for events
Construction
Basic Setup
Constructor Options
Anonymous Agents
Anonymous Agents
When no character is provided, the runtime creates an anonymous agent:Anonymous agents:
- Auto-increment counter for unique names
- Skip character provider registration
- Useful for testing or minimal setups
Initialization
Theinitialize() method must be called before using the runtime:
Initialization Flow
What happens during initialization
What happens during initialization
-
Bootstrap Plugin Registration
- Core actions: REPLY, IGNORE, NONE
- Core providers: time, character info
- Core services: task, approval, action filter
-
Optional Plugin Registration
- Advanced planning (if
character.advancedPlanning === true) - Advanced memory (if
character.advancedMemory === true)
- Advanced planning (if
-
Character Plugin Registration
- Plugins from
character.pluginsarray - Each plugin’s components registered
- Plugins from
-
Database Setup
- Adapter initialization (or InMemoryAdapter if allowed)
- Schema migrations for all loaded plugins
-
Agent Setup
- Agent record created/loaded from database
- Settings and secrets merged
- Agent entity created
- Default room created for agent
-
Embedding Setup
- Detect embedding model dimension
- Ensure database schema supports it
-
Service Startup
- Services start asynchronously
- Init promise resolved when ready
State Composition
State is the context passed to all components (actions, evaluators, LLM prompts).State Structure
Composing State
- Check Cache - Return cached state if available (keyed by message ID)
- Load Recent Messages - Fetch conversation history (respects
conversationLength) - Execute Static Providers - Always-run and non-dynamic providers
- Execute Dynamic Providers - Only if relevant keywords match message
- Merge Results - Combine text, values, and data from all providers
- Cache State - Store in LRU cache (max 200 entries)
Provider Execution
Provider Ordering
Provider Ordering
Providers are executed in this order:
- Position-ordered providers (negative positions first)
- Static/alwaysRun providers
- Dynamic providers (if keywords match)
- Position-ordered providers (positive positions last)
Model Usage
The runtime provides model abstraction with automatic routing.Model Types
Using Models
Model Registration
Plugins register model handlers:Model Selection
When multiple handlers are registered for a model type:- Handlers are sorted by priority (descending)
- Highest priority handler is selected
- Plugin-specific handler can be requested by provider name
LLM Mode Override
Force model size globally:DEFAULT- No overrideSMALL- Force TEXT_SMALLLARGE- Force TEXT_LARGE
Streaming
Streaming is handled via context:Action Processing
Actions are the primary way agents perform tasks.Action Execution
Action Planning Modes
Single Action Mode (Planning Disabled)
- Select single action
- Extract parameters if defined
- Execute action
- Return result
Multi-Action Mode (Planning Enabled)
- Generate action plan
- For each action:
- Extract parameters
- Execute action
- Update state with results
- Return all results
Action Results
Actions return structured results:- Stored in working memory
- Available to subsequent actions
- Merged into state for following steps
Action Context
Actions receive context about previous executions:Evaluators
Evaluators run at different pipeline phases.Pre-Phase Evaluators
Run before memory storage (security gates):Post-Phase Evaluators
Run after response generation (reflection):Memory Management
Creating Memories
Searching Memories
Embedding Generation
Embeddings are generated asynchronously:Memory Updates
Service Management
Getting Services
Service Loading
Services load asynchronously. Wait for specific service:Event System
The runtime provides an event bus for pub/sub communication.Event Types
Emitting Events
Listening for Events
Settings Management
Getting Settings
character.secrets[key]character.settings[key]character.settings.extra[key]character.settings.secrets[key]runtime.settings[key]process.env[key]
Setting Settings
Built-in Settings
| Setting | Type | Default | Description |
|---|---|---|---|
CONVERSATION_LENGTH | number | 100 | Messages in context |
ACTION_PLANNING | boolean | true | Multi-action execution |
LLM_MODE | string | ”DEFAULT” | Force model size |
CHECK_SHOULD_RESPOND | boolean | true | Always respond? |
ENABLE_AUTONOMY | boolean | false | Autonomous operation |
DISABLE_IMAGE_DESCRIPTION | boolean | false | Skip vision analysis |
MAX_WORKING_MEMORY_ENTRIES | number | 50 | Action result cache |
Run Tracking
Track sequences of related model calls:- Trajectory logging
- Debugging multi-step operations
- Performance analysis
Sandbox Mode
Secure secrets in multi-tenant deployments:- Secrets never exposed to action code
- Tokens replaced at network boundary
- Audit trail of token usage
- Fail-closed mode (error if replacement fails)
Best Practices
Runtime optimization tips
Runtime optimization tips
-
Initialize Once
- Create runtime once, reuse for multiple messages
- Initialization is expensive (plugin loading, DB setup)
-
Cache State
- State caching is automatic (200 entry LRU)
- Don’t compose state multiple times for same message
-
Use Appropriate Models
- TEXT_SMALL for simple tasks
- TEXT_LARGE for complex reasoning
- Consider cost vs quality tradeoffs
-
Action Planning
- Disable for frequently updating state (games)
- Enable for complex workflows
-
Memory Management
- Queue embeddings for bulk operations
- Use unique flag to deduplicate messages
- Adjust conversation length based on token budget
-
Service Dependencies
- Await service load promises if critical
- Handle service failures gracefully
-
Event Handling
- Keep event handlers fast
- Use events for loose coupling
- Don’t block on event emission
-
Logging
- Use appropriate log levels
- Room-specific log level overrides available
- Structured logging for better observability
Next Steps
Actions
Build custom actions
Plugins
Create plugins
Memory
Memory system details
Services
Service development