agent-base
A lightweight Agent Runtime Kernel for building AI agents in Rust.
agent-base provides the minimal orchestration layer needed to build custom AI agents — LLM integration, tool dispatch, multi-turn conversation, approval flows, event streaming, and error recovery — all with zero business assumptions.
Installation
[]
= "0.1.2"
Design Principles
- Clear semantics —
RunOutcomeexplicitly distinguishesCompletedfromFailed; events capture the process, the return value captures the final result. - Simple state model — Runtime memory is the source of truth for live sessions;
SessionStoreis an optional persistence adapter. - Conservative by default — On tool failure, the runtime stops by default (
StopOnError) rather than guessing how to recover. - Strategy injection — All variable behaviors are injected via traits (
ToolErrorRecovery,ToolPolicy,ApprovalHandler,Middleware), not hardcoded.
Features
- LLM Abstraction —
LlmClienttrait with built-in OpenAI and Anthropic implementations - Tool System —
Tooltrait +ToolRegistryfor registration and dispatch - Approval Flow —
ApprovalHandlertrait withAllowOnce/AllowAlways/Denydecisions - Error Recovery —
ToolErrorRecoverytrait; defaults toStopOnError, opt-inRetryOnError - Event Streaming — Structured
RuntimeEventstream for UI, logging, auditing, and debugging - Multi-turn Sessions —
AgentSessionmanages message history;SessionStorefor optional persistence - Sub-Agents —
SubAgentToolwithEphemeral(default) orPersistentsession policies - Context Management — configurable
ContextWindowManagerfor token budget control - Middleware — hooks at
on_user_message,on_pre_llm, andon_post_llmfor extensions - Ephemeral Messages — messages can be marked ephemeral; visible to LLM during the current turn, automatically cleaned from memory after turn ends, excluded from persistence
- Plan Checklist — built-in
UpdatePlanToolfor multi-step task tracking - Checkpoints — structured
Checkpointevents enable future replay, debugging, and resume - Tool Enforcement —
ToolEnforcementMiddlewarenudges the LLM to call tools instead of just describing actions - Turn Tool Limit —
TurnToolLimitMiddlewarecaps tool calls per turn
Quick Start
1. Define a Tool
Any capability you want your agent to have is expressed as a Tool:
use ;
use async_trait;
use ;
;
2. Build the Agent
use Arc;
use ;
async
3. Handle Tool Errors
By default, tool failures stop the run. For self-healing agents (e.g. code agents that retry compilation), inject RetryOnError:
use RetryOnError;
let mut runtime = new
.register_tool
.error_recovery // ← retry on failure
.build;
4. Add Approval for Sensitive Tools
use ;
;
;
let mut runtime = new
.register_tool
.tool_policy
.approval_handler
.build;
5. Use a Sub-Agent
use SubAgentTool;
// Build a sub-agent runtime
let sub_llm = new;
let sub_runtime = new
.system_prompt
.build;
// Wrap it as a tool
let math_tool = new;
// Register in the parent agent
let mut parent = new
.register_tool
.build;
Each sub-agent call creates a fresh session by default. Use SubAgentTool::with_persistent() to share context across calls.
Examples
# Configure API key
# Edit .env with your OPENAI_API_KEY or ANTHROPIC_API_KEY
# Run the REPL example
# Run the SubAgent demo
# Run the Middleware demo
# Run the Plan demo
What agent-base Does NOT Do
- Built-in SSH, filesystem, or database tools
- Workflow DAG or multi-agent orchestration engine
- Memory or RAG (Retrieval-Augmented Generation) framework
- Terminal UI or built-in approval dialog
- Production-grade persistence or transaction system
Business-specific tools and strategies belong in upper layers (e.g. ops-agent, agent-works, db-agent, browser-agent).
Typical Layering
ops-agent / agent-works / ... ← Business agents / Enhanced toolkits
└── agent-base ← Lightweight Runtime Kernel
v1 Semantics
| Convention | Meaning |
|---|---|
run_turn_* → AgentResult<RunOutcome> |
Ok(Completed) = success, Ok(Failed) = finished with error |
RuntimeEvent::RunFinished |
Process ended — final status is in RunOutcome |
Tool failure → defaults to StopOnError |
Inject RetryOnError for self-healing agents |
SubAgent → defaults to Ephemeral |
Use with_persistent() for shared context |
| Session → memory is source of truth | SessionStore is an optional persistence adapter |
Acknowledgments
This project draws inspiration from the OpenAI Codex CLI project — particularly its approach to tool orchestration and task planning.
Stability
This project is in early development (v0.1.2). The core abstractions are settling but not yet frozen. Expect minor API changes as the ecosystem evolves.
License
MIT