Skip to main content

Crate agent_code_lib

Crate agent_code_lib 

Source
Expand description

§agent-code-lib

The complete AI coding agent engine as a reusable library.

This crate contains everything needed to build an AI coding agent: LLM providers, tools, the query engine, memory, permissions, and all supporting services. The agent CLI binary is a thin wrapper over this library.

§Modules

ModulePurpose
configConfiguration loading and schema (TOML, layered merge)
errorUnified error types (LlmError, ToolError, ConfigError)
hooksLifecycle hooks (pre/post tool use, session events)
llmLLM communication: streaming client, message types, providers, retry
memoryPersistent context: project (AGENTS.md) and user memory
permissionsPermission system: rules, modes, protected directories
queryAgent loop: call LLM → execute tools → compact → repeat
servicesCross-cutting: tokens, compaction, sessions, git, MCP, plugins, diagnostics
skillsCustom workflow loading from markdown files
stateSession state: messages, usage tracking, cost
tools32 built-in tools and the Tool trait for custom tools

§Quick Example

use agent_code_lib::config::Config;
use agent_code_lib::tools::registry::ToolRegistry;
use agent_code_lib::state::AppState;

let config = Config::default();
let tools = ToolRegistry::default_tools();
let state = AppState::new(config);
// state.messages, state.total_cost_usd, etc. are now available

§Adding a Custom Tool

Implement the tools::Tool trait:

use async_trait::async_trait;
use agent_code_lib::tools::{Tool, ToolContext, ToolResult};

struct MyTool;

#[async_trait]
impl Tool for MyTool {
    fn name(&self) -> &'static str { "MyTool" }
    fn description(&self) -> &'static str { "Does something useful" }
    fn input_schema(&self) -> serde_json::Value {
        serde_json::json!({"type": "object", "properties": {}})
    }
    fn is_read_only(&self) -> bool { true }
    async fn call(
        &self,
        input: serde_json::Value,
        ctx: &ToolContext,
    ) -> Result<ToolResult, agent_code_lib::error::ToolError> {
        Ok(ToolResult { content: "done".into(), is_error: false })
    }
}

Then register it: registry.register(Arc::new(MyTool));

Modules§

config
Configuration system.
error
Unified error types for the application.
hooks
Hook system.
llm
LLM client layer.
memory
Memory system — 3-layer architecture.
permissions
Permission system.
query
Query engine: the core agent loop.
sandbox
Process-level sandboxing for subprocess-spawning tools.
schedule
Scheduled agent execution.
services
Core services layer.
skills
Skill system.
state
Application state management.
tools
Tool system.