Skip to main content
The agent runtime orchestrates LLM interactions, tool execution, and state management for both interactive and background agents.

Runtime Interface

The core runtime interface provides a single method for triggering agent execution:
Location: apps/x/packages/core/src/agents/runtime.ts:32

AgentRuntime Class

The main runtime implementation handles:
  • Run locking - Prevents concurrent execution of the same run
  • Abort management - Handles user-requested stops
  • Event streaming - Publishes events to the message bus
  • State persistence - Saves events to runs repository
Location: apps/x/packages/core/src/agents/runtime.ts:36

Execution Flow

1. Run Initialization

Location: apps/x/packages/core/src/agents/runtime.ts:71

2. Main Loop

The runtime executes in a loop until completion:
Location: apps/x/packages/core/src/agents/runtime.ts:83

3. Cleanup

Always executed, even on error:
Location: apps/x/packages/core/src/agents/runtime.ts:139

Agent Streaming

The streamAgent function implements the core agent logic:
Location: apps/x/packages/core/src/agents/runtime.ts:672This is the heart of agent execution - it loads the agent, builds tools, runs the LLM loop, and handles tool execution.

Setup Phase

Location: apps/x/packages/core/src/agents/runtime.ts:696

Agent Loop

The agent executes in iterations:
Location: apps/x/packages/core/src/agents/runtime.ts:717

Tool Call Processing

After LLM response, process tool calls:
Location: apps/x/packages/core/src/agents/runtime.ts:917

Message Building

The StreamStepMessageBuilder assembles assistant messages from LLM stream events:
Location: apps/x/packages/core/src/agents/runtime.ts:224

Agent Loading

Agents are loaded from Markdown files or special built-in agents:
Location: apps/x/packages/core/src/agents/runtime.ts:313

Tool Building

Tools are mapped from agent definitions to executable functions:
Location: apps/x/packages/core/src/agents/runtime.ts:464

State Management

AgentState maintains execution state and processes events:
Location: apps/x/packages/core/src/agents/runtime.ts:484
Location: apps/x/packages/core/src/agents/runtime.ts:585

Abort Handling

The runtime supports graceful abort:
Location: apps/x/packages/core/src/agents/runtime.ts:84

Run Logging

Events are logged to JSONL files for debugging:
Location: apps/x/packages/core/src/agents/runtime.ts:193

Next Steps

Background Agents

Learn about scheduled execution

Skills

Explore the skill system