Skip to main content

Overview

The Agent API provides the core orchestration layer for IronClaw, managing:
  • Message routing from channels
  • Job scheduling and execution
  • Tool invocation with safety
  • Self-repair for stuck jobs
  • Proactive heartbeat execution
  • Routine-based scheduled and reactive jobs
  • Turn-based session management with undo
  • Context compaction for long conversations

Agent

The main agent struct that coordinates all components.

Constructor

src/agent/agent_loop.rs
Creates a new agent with the specified configuration and dependencies.
AgentConfig
required
Agent configuration including max parallel jobs, repair settings, and session timeouts
AgentDeps
required
Core dependencies including database, LLM provider, safety layer, and tool registry
Arc<ChannelManager>
required
Channel manager for handling multiple input/output channels
Option<HeartbeatConfig>
Optional configuration for proactive heartbeat checks
Option<RoutineConfig>
Optional configuration for scheduled and event-driven routines
Option<Arc<ContextManager>>
Optional pre-created context manager (creates new one if not provided)
Option<Arc<SessionManager>>
Optional pre-created session manager (creates new one if not provided)

run

src/agent/agent_loop.rs
Runs the agent main loop. Starts all channels, spawns background tasks (self-repair, heartbeat, routines), and processes incoming messages until shutdown. Returns: Result<(), Error> - Ok on clean shutdown, Error if startup fails

scheduler

src/agent/agent_loop.rs
Get the scheduler for external wiring (e.g., CreateJobTool). Returns: Shared reference to the scheduler

AgentDeps

Core dependencies for the agent, bundling shared components to reduce argument count.

Fields

Option<Arc<dyn Database>>
Optional database for persistence
Arc<dyn LlmProvider>
Primary LLM provider for agent responses
Option<Arc<dyn LlmProvider>>
Optional cheap/fast LLM for lightweight tasks (heartbeat, routing, evaluation). Falls back to main LLM if None.
Arc<SafetyLayer>
Safety layer for content filtering and policy enforcement
Arc<ToolRegistry>
Registry of available tools
Option<Arc<Workspace>>
Optional workspace for persistent memory
Option<Arc<ExtensionManager>>
Optional extension manager for WASM and MCP tools
Option<Arc<RwLock<SkillRegistry>>>
Optional skill registry for prompt-level skills
Arc<HookRegistry>
Hook registry for intercepting inbound/outbound messages
Arc<CostGuard>
Cost enforcement guardrails (daily budget, hourly rate limits)

Session Management

Session

Manages conversation state and turn history for a user.
src/agent/session.rs

Thread

Represents a conversation thread with turn-based history.
src/agent/session.rs

Turn

A single turn in a conversation (user input + agent response).
src/agent/session.rs

Context Compaction

The agent automatically compacts conversation context when it exceeds thresholds.

ContextMonitor

src/agent/context_monitor.rs
Monitors context usage and triggers compaction when needed.

CompactionStrategy

src/agent/context_monitor.rs

Self-Repair

The agent includes self-repair capabilities for stuck jobs and broken tools.

SelfRepair Trait

src/agent/self_repair.rs

RepairResult

src/agent/self_repair.rs

Example