Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Build an Agent

Use this when you need to assemble an agent with tools, persistence, and a provider into a running AgentRuntime.

Prerequisites

  • awaken crate added to Cargo.toml
  • An LlmExecutor implementation (provider) available
  • Familiarity with AgentSpec and AgentRuntimeBuilder

Steps

  1. Define the agent spec.
use awaken::engine::GenaiExecutor;
use awaken::{AgentSpec, AgentRuntimeBuilder, ModelSpec};

let spec = AgentSpec::new("assistant")
    .with_model("claude-sonnet")
    .with_system_prompt("You are a helpful assistant.")
    .with_max_rounds(10);
  1. Register tools.
use std::sync::Arc;

let builder = AgentRuntimeBuilder::new()
    .with_agent_spec(spec)
    .with_tool("search", Arc::new(SearchTool))
    .with_tool("calculator", Arc::new(CalculatorTool));
  1. Register a provider and a model.
let builder = builder
    .with_provider("anthropic", Arc::new(GenaiExecutor::new()))
    .with_model("claude-sonnet", ModelSpec {
        id: "claude-sonnet".into(),
        provider: "anthropic".into(),
        model: "claude-sonnet-4-20250514".into(),
    });
  1. Attach persistence.
use awaken::stores::InMemoryStore;

let store = Arc::new(InMemoryStore::new());
let builder = builder.with_thread_run_store(store);
  1. Build and validate.
let runtime = builder.build()?;

build resolves every registered agent and catches missing models, providers, or plugins at startup rather than at request time.

  1. Execute a run.
use std::sync::Arc;
use awaken::RunRequest;
use awaken::contract::event_sink::VecEventSink;

let request = RunRequest::new("thread-1", vec![user_message])
    .with_agent_id("assistant");

let sink = Arc::new(VecEventSink::new());
let handle = runtime.run(request, sink.clone()).await?;

Verify

Call the /health endpoint (if using the server feature) or inspect the returned AgentRunResult to confirm the agent loop completed without errors.

Common Errors

ErrorCauseFix
BuildError::ValidationFailedAgent spec references a model or provider not registered in the builderRegister the missing model/provider before calling build
BuildError::StateDuplicate state key registration across pluginsEnsure each StateKey is registered by exactly one plugin
RuntimeError at run timeProvider returns an inference errorCheck provider credentials and model ID

examples/src/research/ – a research agent with search and report-writing tools.

Key Files

  • crates/awaken-runtime/src/builder.rsAgentRuntimeBuilder
  • crates/awaken-contract/src/registry_spec.rsAgentSpec
  • crates/awaken-runtime/src/runtime/agent_runtime/mod.rsAgentRuntime