# Agent SDK

> Write an agent as a Rust function inside your Ferrite application: compose hooks for models, tools, skills, subagents, MCP connections, a workspace, and structured results.

Your product's agent is part of your product. When you add the `agent-sdk` source package with `ferrite add agent-sdk`, the agent runs inside your Ferrite application: you deploy and roll it back with the rest of your code, keep its conversations in the application's own storage, and list every host it can reach in the same `ferrite.json` manifest.

Its authoring surface is modeled on [Flue](https://github.com/withastro/flue): you write an agent as a plain Rust function, declare what it can use through hooks, and build tools, skills, subagents, and MCP connections with `define_*` helpers. Underneath, the runtime ports [pi](https://github.com/earendil-works/pi)'s agent loop, session tree, compaction, and crash recovery onto Ferrite's storage and egress primitives. One deployment serves people and other agents over a JSON API, a live SSE stream, MCP, ACP, and A2A, backed by models from the AI gateway's catalog. Use it for support desks, issue triagers, scheduled reporters, or tool-using chats. This page covers authoring; [Agent Runtime](/docs/agent-runtime) explains how the runtime executes and recovers underneath.

## An agent is a function

Before each submission, the SDK calls your agent function once (a **render**), records every hook it called, and assembles the system prompt and tool set for that run. If a tool writes persistent state or enables a discovered tool mid-submission, the SDK renders the function again before the next model turn and narrates what changed to the model. Your function returns the agent's `Instruction`.

```rust
use crate::agent_sdk::prelude::*;

pub fn assistant() -> Instruction {
    use_model("anthropic/claude-sonnet-4-6");
    use_sandbox(workspace());
    use_skill(
        define_skill(SkillDefinition::new(
            "verify-change",
            "Verify a code change before reporting it done.",
            "1. Re-read every file you edited with `read`.\n2. `grep` for each changed symbol.",
        ))
        .expect("a valid skill definition"),
    );
    use_subagent(define_subagent(
        "researcher",
        "Explores the workspace or the web to answer a focused question.",
        || "You research. Read broadly, answer narrowly.",
    ));
    use_tool(word_count());
    "You are a helpful assistant working inside a Ferrite workspace. \
     Read before you write, keep answers concise, and show file paths clearly."
        .into()
}
```

Take `AgentProps` when instructions or bindings depend on which instance is rendering:

```rust
pub fn triage(props: AgentProps) -> Instruction {
    use_model("anthropic/claude-sonnet-4-6");
    format!("You triage incoming reports for instance {}.", props.id).into()
}
```

Hooks are free functions that record onto the active render frame. Calling a hook outside a render panics with a message naming the hook. A root render that omits `use_model` fails with an authoring error naming the agent, and two tools with the same name are refused at render time.

## Register and serve

Collect your agents in an `Agents` registry. `Agent::new` binds a name to a function; `.describe` attaches its description and `.initial_data` attaches the JSON Schema callers may pass when creating an instance.

```rust
pub fn registry() -> Agents {
    Agents::new()
        .register(Agent::new("assistant", assistant).describe("A general assistant"))
        .register(
            Agent::new("triage", triage)
                .describe("Classifies incoming reports")
                .initial_data(serde_json::json!({ "type": "object", "properties": { "project": { "type": "string" } } })),
        )
}
```

Use `Agents::new().agent("name", function)` when you need no extra metadata. Hand the registry to `AgentApp` in your application's entry point:

```rust
impl FerriteApp for AgentsApp {
    async fn run(ctx: AppContext) -> io::Result<()> {
        agent_sdk::app::AgentApp::new(ctx).agents(agents::registry()).serve().await
    }
}
```

The registry sorts agents alphabetically by name, so the default agent for the thread API, ACP, and A2A is the alphabetically first name, regardless of registration order. Registering a second agent under an existing name replaces the first without error. Start from `AppConfig::default()` and pass `AgentApp::config(AppConfig { .. })` to override binding or secret names, opened egress handles (`egress`), `default_agent`, or `dev_mode` (which adds `dev` diagnostic fields to error envelopes). Call `AgentApp::index_html(html)` to serve an unauthenticated landing page on `GET /`, as the reference app does.

## Hooks

| Hook | Declares |
| --- | --- |
| `use_model(id)` / `use_model_with(id, options)` | The model as a gateway catalog id (`provider/model`); options set a `ThinkingLevel` and compaction config (`compaction(config)` or `compaction_disabled()`) |
| `use_instruction(text)` | An additional instruction block appended to the prompt |
| `use_tool(tool)` | A tool from `define_tool` or a built-in tool family |
| `use_skill(skill)` | A skill the model can activate by name |
| `use_subagent(subagent)` | A delegate reachable through the framework `task` tool |
| `use_mcp_connection(connection)` | A remote MCP server whose tools join the tool set |
| `use_tool_catalog(catalog)` | A catalog of discoverable tools for `search_tools` and `enable_tool` |
| `use_sandbox(factory)` / `use_sandbox_in(factory, cwd)` | The environment the file tools operate on; `workspace()` is the Ferrite workspace rooted at `/`, and `workspace_in(cwd)` sets a working directory created on attach and seen by tools, harness, and prompt |
| `use_persistent_state(name, default)` | Durable per-instance state returning the current value and a setter |
| `use_initial_data::<T>()` / `harness::hooks::use_initial_data_value()` | The initial data passed when the instance was created |
| `use_delivery()` | The message being delivered in the current submission, when present |
| `use_agent_start(hook)` / `use_agent_finish(hook)` | Async hooks running before and after a submission |
| `use_response_start(hook)` / `use_response_finish(hook)` | Per-model-response hooks returning metadata to record |
| `use_data_writer(name)` | A writer for named message data clients can read back |
| `use_dispatch_message()` | A dispatcher sending a message to this instance (`dispatch_self(message)`) or another (`dispatch(agent, id, message)`), resolving to the submission id |

Subagent renders reject root-only hooks. Every render of an agent must declare the same set of `use_data_writer` names, whereas models, resources, and instructions can vary freely across renders.

## Tools

Define a tool with a name, description, JSON Schema input, optional output schema, and an async `run` closure. Use `ObjectSchema` with the `string`, `integer`, `number`, `boolean`, `array`, and `enumeration` helpers for common shapes, or pass any raw JSON Schema value.

```rust
fn word_count() -> Tool {
    define_tool("word_count", "Count the words in a text")
        .input(ObjectSchema::new().required("text", string("The text to count")).build())
        .output(serde_json::json!({ "type": "object", "properties": { "words": { "type": "integer" } }, "required": ["words"] }))
        .run(|ctx: ToolContext| async move {
            let text = ctx.data["text"].as_str().unwrap_or("");
            Ok(serde_json::json!({ "words": text.split_whitespace().count() }))
        })
}
```

The prelude re-exports the `serde_json` module and `Value` without the `json!` macro, so examples write `serde_json::json!`; add `use ferrite_app::serde_json::json;` if you prefer the short macro name.

`ToolContext` carries the validated arguments (`data`, or `args::<T>()` for a typed struct), the call id, an `AbortSignal` that fires when the submission aborts, and a `Logger`. Returning `Err(ToolError)` hands the model an error result it can react to; a tool error never aborts the loop. A tool declared without an input schema advertises an open object and accepts extra arguments the model volunteers, so a zero-argument `check_fact` tool is not rejected when the model sends `{"fact": "..."}`.

`run` accepts anything convertible into `ToolRun`: a `Value` or string becomes tool output, `ToolRun::none()` returns empty output, and `.terminating()` on a `ToolRun` asks the loop to stop after the current batch. When every result in a batch is terminating, the agent finishes without another model turn. `run` panics on an invalid tool definition when the module loads; call `try_run` to receive a `Result` instead.

Three modifiers control how the runtime schedules and runs a tool:

- `.sequential()` runs calls to this tool one at a time, never alongside other calls in the same batch.
- `.durable()` persists the step's result before returning it, so a redelivered or resumed submission replays the saved output instead of running a mutating tool twice.
- `.harness()` exposes the harness surface to the handler: `prompt(text, options)` runs a scratch conversation on the agent's model with the agent's own tools (file tools, skills, delegates, MCP, and custom tools, minus harness tools themselves) under the agent's compaction config, and `sandbox()` exposes the live workspace. Running a harness tool standalone fails with `tool_requires_harness`.

The names `task`, `activate_skill`, `read_skill_resource`, `finish`, `give_up`, `search_tools`, and `enable_tool` are reserved by the framework.

### Built-in families

| Family | Tools | Notes |
| --- | --- | --- |
| `file_tools(sandbox)` | `read`, `write`, `edit`, `ls`, `glob`, `grep`, `delete` | Mounted automatically by `use_sandbox`; byte-bounded output (`read` 2000 lines / 50 KB, `grep` 100 matches / 64 KB, `glob` 1000 results, `ls` 500 entries) |
| `web_fetch_tool(http)` / `web_search_tool(http, key)` | `web_fetch`, `web_search` | Fetch converts HTML to text; search queries Serper with a secret resolved per call |
| `github_tools(http, token)` | `github_api`, `github_read_file`, `github_search_code`, `github_list_issues`, `github_create_issue` | Real HTTP verbs against `api.github.com`, including writes |
| `github_read_tools(http, token)` | `github_read_file`, `github_search_code`, `github_list_issues` | Fixed GET shapes only; the model cannot pick a mutating verb or endpoint |

Any agent that reads untrusted text (web pages, search results, public repositories) should receive `github_read_tools` instead of `github_tools`. Untrusted text can instruct the model to invoke tools, and `github_api` or `github_create_issue` would let prompt injection write with your deployment's GitHub token. The reference `assistant` enforces this boundary.

Agents run without shell access. The default sandbox is the **Ferrite workspace**: file bodies live on Blob, the node index lives on Store, each agent instance gets an isolated namespace, and the file tools define everything the model can do there. Applications that use an external execution environment can attach it through the same `SandboxFactory` seam.

The HTTP and secret handles come from the app environment. `app::http()` returns `Some` when the deployment grants at least one of the egress handles the SDK opens (`web-egress`, `github-egress`, and `mcp-egress` by default). Outbound requests to any destination outside the granted handles fail with an egress denial:

```rust
if let Some(http) = app::http() {
    use_tool(web_fetch_tool(http.clone()));
    use_tool(web_search_tool(http.clone(), app::secret("serper-api-key")));
    for tool in github_read_tools(http, app::secret("github-token")) {
        use_tool(tool);
    }
}
```

Evals and live test suites running outside `AgentApp::serve` can provide the same environment once via `app::install_env(AppEnv { http, secrets })`; the first environment installed in a process stands.

## Skills

A skill packages instructions that the model activates on demand via `activate_skill`, along with optional supporting files read via `read_skill_resource`. Build one in code with `SkillDefinition::new(name, description, instructions)`, or load a packaged `SKILL.md` (YAML frontmatter plus body) with `Skill::from_markdown`. The initial prompt lists only each skill's name and description, so long procedures consume no context tokens until the model activates them.

## Subagents

`define_subagent(name, description, function)` declares a delegate reachable through the framework `task` tool. Each delegation runs in a detached child session keyed by the tool call, while the delegate roster renders into the prompt and the `task` tool schema stays static so provider prompt caches remain valid. `general_subagent()` provides a ready-made explorer. Chain `.model(id)` and `.thinking_level(level)` on a `Subagent` to override parent settings for that delegate. `define_subagent` panics on an empty name or description; `try_define_subagent` returns the error instead. Delegation depth is capped at 4, and delegate renders reject root-only hooks.

## MCP connections

`define_mcp_connection(name, url)` mounts tools from a remote MCP server that speaks protocol revision `2026-07-28` over streamable HTTP. A server supporting only an earlier revision, such as `2025-11-25`, cannot be mounted: the revisions are incompatible on the wire and the SDK's client sends no `initialize` handshake. Check the server's revision before relying on it (see [Which clients can connect](/docs/mcp-servers#which-clients-can-connect)), and mark the connection `.optional()` if the agent should still render when that server is unreachable. Mounted servers page through `tools/list` and join the tool set as `mcp__<server>__<tool>`.

```rust
let docs_token = app::secret("docs-mcp-token");
use_mcp_connection(
    define_mcp_connection("docs", "https://mcp.example.com/mcp")
        .auth_resolver(move || docs_token())
        .only_tools(&["search_docs", "read_page"])
        .timeout_ms(30_000),
);
```

`.bearer` sets a fixed token, `.auth_resolver` resolves one per call, `.header` adds a static header, `.only_tools` restricts mounted tools (and fails loudly if the server does not advertise a named tool), and `.optional()` allows rendering when the server is unreachable. Every MCP request obeys `.timeout_ms` (30 s by default). Every remote MCP host requires an egress grant in `ferrite.json`.

With `agent-sdk` 0.7 or newer, a connection can do more than list and call tools. Each capability below is declared to the server only when you ask for it, except tasks, which are on:

```rust
use_mcp_connection(
    define_mcp_connection("tracker", "https://mcp.tracker.example/mcp")
        .token_source(tokens)
        .elicit_forms()
        .elicit_urls()
        .files()
        .skills()
        .ui(),
);
```

| Builder | What it does |
| --- | --- |
| `.elicit_forms()`, `.elicit_urls()` | Let the server ask a person a question, in a form or through a link |
| `.without_tasks()`, `.task_timeout_ms(ms)` | Wait for every result in the call, or bound how long a task is followed |
| `.files()`, `.files_preapproved()` | Exchange files with the agent's sandbox, with or without a person's approval of each upload |
| `.skills()`, `.skills_ask()` | Add the server's [skills](/docs/mcp-skills) to the catalog, at once or after a person approves each |
| `.ui()` | Pass a tool's [view](/docs/mcp-apps) to a client that can show it |
| `.token_source(source)` | Obtain and renew the credential with OAuth or through an enterprise identity provider |
| `.mount_limit(n)` | The number of tools above which the server is searched instead of mounted (40) |

A connected server's instructions join the system prompt, marked as that server's text, and its list of tools is renewed when the server says it has expired. A tool whose name a model provider would refuse is mapped onto one it accepts.

### Questions a server asks

A question becomes a record on the Store before the agent waits for it, so any replica can answer it and a restart does not ask twice. It carries the connection's own label, and either a form schema with defaults or the full link and its host. Show it in your interface and send the answer:

| Where | How to list and answer |
| --- | --- |
| Thread API | `GET /api/threads/{id}/inputs`, then `POST /api/threads/{id}/inputs/{input}` with `{"action": "accept", "content": {}}` |
| Agents API | The same routes under `/api/agents/{agent}/instances/{id}` |
| Stream | `input_requested` and `input_resolved` events |
| A2A | The task becomes `input-required`, and a data part `{"inputId", "answers"}` answers it |
| ACP | `_ferrite/inputs/list` and `_ferrite/inputs/answer` |
| MCP | A client that declares elicitation is asked through the call or its task |

A question nobody answers within 15 minutes is cancelled, which is never an approval. Open a link only when the person chooses to; the SDK never fetches one. Your own tools can ask a yes or no question the same way with `harness::host::confirm`.

### Long calls

When a server answers with a task, the SDK records it, polls at the server's interval, and returns the result to the model as if the call had waited. If the process restarts, the same call with the same arguments resumes that task and starts nothing new. Stopping the submission asks the server to cancel the task.

### Files

With `.files()`, a tool that takes a file gets a sandbox path from the model. A small file travels in the call when the server allows it, and a larger one goes to the upload target the server names, with that target's headers and none of the connection's credentials. Downloads are checked against their size and digest and saved under `/downloads/<connection>/` without replacing anything. A person approves each upload unless you chose `.files_preapproved()`. Declare the storage hosts a server uses as egress destinations too.

### Skills from a server

A server's skill is listed as `<connection>/<skill>`, so it never stands in for one of your own. Its files are read when they are needed and checked against the digests in the server's list, and its `allowed-tools` is ignored. An approval covers the files as they were when it was given. While such a skill is in use, a sandbox shell and any tool you marked `.executes_code()` ask a person first.

### Sign in to a server

`OAuthTokens` signs one of your users in to a server and keeps the tokens in your Store, apart for each owner, issuer and resource:

```rust
let now = ctx.clone();
let clock: Clock = Arc::new(move || now.now_ms());
let tokens = OAuthTokens::new(
    http,                                    // app::http()
    Arc::new(StoreKv::new(ctx.store("workspace-index")?)),
    clock,
    owner,                                   // the user or workspace
    "https://mcp.tracker.example/mcp",
    OAuthClient {
        client_id: "https://app.example/oauth/client.json".into(),
        auth: ClientAuth::None,
        redirect_uri: "https://app.example/connect/callback".into(),
    },
    secret,                                  // at least 32 bytes you keep
)?;
```

Send the person to the URL `tokens.begin().await?` returns, and call `tokens.finish(code, state, iss).await?` from your redirect route (see [custom routes](/docs/agent-channels)). The connection refreshes by itself afterwards. `needs_step_up()` says the server asked for more access, `connected()` whether a token is held, and `disconnect()` forgets it. Declare the authorization server as an egress destination when its host differs from the MCP server's.

`EnterpriseTokens` uses [enterprise-managed authorization](/docs/enterprise-managed-authorization) instead: it exchanges a person's single sign-on token at their company's identity provider for access to one MCP server, so nobody completes a second sign-in.

```rust
let tokens = EnterpriseTokens::new(
    http,
    clock,
    EnterpriseIdp {
        token_endpoint: "https://login.company.example/oauth2/token".into(),
        client_id: "my-app".into(),
        auth: ClientAuth::SecretPost(idp_secret),
        subject: id_token,                   // a closure returning the person's ID token
        subject_type: oauth::TOKEN_TYPE_ID_TOKEN,
    },
    EnterpriseResource {
        resource: "https://mcp.tracker.example/mcp".into(),
        issuer: "https://auth.tracker.example".into(),
        client_id: "https://app.example/oauth/client.json".into(),
        auth: ClientAuth::None,
        scopes: vec!["issues:read".into()],
    },
);
```

## Dynamic tool discovery

When an agent can choose from a large tool set, register catalogs instead of mounting every tool in the initial prompt. This is tool search for your own agent: definitions enter the context only when a request needs them.

```rust
use crate::agent_sdk::protocols::a2a::A2aCatalog;

use_tool_catalog(Arc::new(StaticCatalog::new("reports", "Reporting tools", reporting_tools())));
use_tool_catalog(Arc::new(McpCatalog::new(define_mcp_connection("crm", crm_url), "The CRM", http.clone())));
use_tool_catalog(Arc::new(A2aCatalog::new("billing", "https://billing.example/.well-known/agent-card.json", http)));
```

The framework mounts `search_tools` (which ranks entries across all catalogs against a query) and `enable_tool` (which adds a chosen tool to the live set). Enabled tool names persist per instance and re-resolve on subsequent submissions; a previously enabled name that no catalog advertises anymore is reported rather than dropped without warning.

A connected MCP server with more than 40 tools is searched this way without a catalog: its tools stay out of the prompt until `search_tools` finds one and `enable_tool` adds it. `.mount_limit(n)` changes the number, and `.only_tools(names)` mounts exactly the tools you name.

## Your agent's MCP endpoint

Every agent app serves `/mcp` with one tool per agent (see [Agent API](/docs/agent-api#mcp)). `AgentApp::mcp_face` adds tools of your own, published skills, views and instructions to it:

```rust
let sure = serde_json::json!({
    "type": "object",
    "properties": { "sure": { "type": "boolean", "title": "Delete it" } },
    "required": ["sure"]
});
let delete_note = FaceTool::new("delete_note", "Delete a note by id", schema, move |call: FaceCall| {
    let sure = sure.clone();
    async move {
        match call.accepted("sure") {
            Some(answer) if answer["sure"] == true => Ok(FaceOutcome::text("Deleted.")),
            Some(_) => Ok(FaceOutcome::text("Nothing was deleted.")),
            None if call.answers.is_empty() => {
                Ok(FaceOutcome::ask_form("sure", "Delete this note?", sure, Value::Null))
            }
            None => Ok(FaceOutcome::text("Nothing was deleted.")),
        }
    }
})
.title("Delete a note")
.scopes(&["notes:write"]);

let face = McpFace::new()
    .identity("notes", "1.0.0")
    .instructions("Notes for the support team. Search before you create one.")
    .publish_agent_skills()
    .resource(FaceResource::view("ui://notes/board.html", "board", include_str!("board.html")))
    .tool(delete_note);

agent_sdk::app::AgentApp::new(ctx).agents(agents).mcp_face(face).serve().await
```

- An agent's tool returns a task to a client that declares tasks, so a long run does not hold the call open. Other clients get the settled answer.
- `FaceOutcome::ask_form` and `ask_url` ask the person. The call comes back with the answer already checked against the form, sealed to the caller, the tool and its arguments, and usable once. Check `call.can_ask(mode)` when the tool can also finish without asking.
- `FaceOutcome::Upload` asks for a file, `with_downloads` offers one, and `.files(upload, max_inline_bytes)` on the face declares what it accepts.
- `.view(uri)` on a tool shows its result in a view the face serves, and `.only_for_view()` keeps a tool out of the model's list.
- `.title`, `.output`, `.read_only()` and `.additive()` describe a tool to clients. A tool that says nothing is announced as destructive.

### Require sign-in at `/mcp`

By default `/mcp` checks the deployment's client token. `protect` verifies each caller's access token instead, answers `401` and `403` with the challenge that starts a client's sign-in, and publishes the protected-resource metadata:

```rust
let verifier: McpVerifier = Arc::new(|caller: McpCaller| {
    Box::pin(async move { verify_access_token(caller.bearer.as_deref()).await })
});
let face = face.protect(
    "https://notes.example/mcp",
    &["https://auth.notes.example"],
    &["notes:read"],
    verifier,
);
```

Your verifier checks the token's signature or introspects it, its expiry, and that it was issued for this address, and returns an `McpPrincipal`. Each subject then has its own conversations, tasks and idempotency keys. With more than one issuer, build the principal with `McpPrincipal::issued_by`, so the same name at two issuers is two people. A caller needs every listed scope to run an agent or a tool, unless `agent_scopes` or the tool's own `scopes` say otherwise.

Forward the headers the endpoint checks, and publish the metadata path. A tool whose schema marks an argument with `x-mcp-header` also needs the matching `mcp-param-` header:

```json
{ "prefix": "/mcp", "binding": "requests", "kind": "api",
  "forward_headers": ["authorization", "mcp-protocol-version", "mcp-method", "mcp-name", "origin", "mcp-param-region"] },
{ "prefix": "/.well-known/", "binding": "requests", "kind": "api" }
```

A request from a web page is refused unless its origin is the deployment's public URL (the `agents-public-url` secret) or one you passed to `allow_origin`.

## Structured results

A caller can request typed output by sending a `resultSchema` with a message. The SDK mounts `finish` (returning a value matching the schema) and `give_up` (explaining why no value can be produced), validates the returned value, and sends a bounded number of follow-up reminders if the model stops without calling either tool. The validated result travels in both the submission record and the API reply.

## Durable state and lifecycle hooks

```rust
pub fn triage(props: AgentProps) -> Instruction {
    use_model("anthropic/claude-sonnet-4-6");
    let (seen, set_seen) = use_persistent_state::<u32>("reports_seen", 0);
    use_agent_finish(move |_ctx| {
        let set_seen = set_seen.clone();
        async move {
            let _ = set_seen.set(seen + 1);
        }
    });
    format!("You triage reports for instance {}. You have triaged {seen} so far.", props.id).into()
}
```

`use_persistent_state` reads the latest durable value for the instance and returns a setter that writes a `state_write` record. The next render observes the new value. Because a state write mid-submission triggers a re-render before the next model turn, a tool that writes state can change the tool set inside the same submission, and the SDK narrates the diff to the model as a `resources` or `instructions` signal.

`use_agent_start` runs before the model turn and is where you load per-conversation data once (`samples/guide/agent_hooks.rs`, `account_support`). `use_agent_finish` runs after the loop settles and may queue follow-up work as extra cycles; cycles are capped at 32 per submission and counted from durable records so the cap survives restarts. `use_response_start` and `use_response_finish` return metadata recorded on the submission and returned alongside the text, such as a model label for a client footer or the tool calls an eval harness inspects.

## Conditional tools and phases

Because the agent function renders before each submission and again before the next turn after a state write, available tools can track durable state. A tool that records an escalation can mount a follow-up tool on the next render. Alternatively, a phase machine built from `use_persistent_state` and `use_instruction` can keep every phase mounted behind a `guarded()` wrapper that refuses calls outside the active phase, so the tool schema stays constant while guards decide what runs. Both patterns live in `guide/agent_hooks.rs` and `support_desk/`; see [Agent Samples](/docs/agent-samples).

Bind identifiers the model must never choose (such as the instance id assigned by your backend or channel, or attributes from a verified signal) from trusted context rather than tool input (`guide/tools.rs`, `customer_orders`).

## Custom hooks

A custom hook is a plain Rust function that calls SDK hooks and returns instruction text. Use custom hooks to share behavior across agents:

```rust
fn use_escalation() -> &'static str {
    use_tool(escalate_case());
    "Escalate to a specialist only after you have confirmed the account and issue."
}

pub fn support_assistant() -> Instruction {
    use_model("anthropic/claude-haiku-4-5");
    let escalation = use_escalation();
    format!("Answer customer support questions accurately. {escalation}").into()
}
```

## Durability

In the agent SDK, durable means an acknowledged write or message survives a crash, a restart, and a redeploy of your application. Every registered agent declares a retry budget and timeout for submissions (defaulting to 10 attempts within one hour):

```rust
Agent::new("issue_triage", issue_triage).durability(DurabilityConfig { max_attempts: 5, timeout_ms: 7_200_000 })
```

Inside a tool, `.durable()` and `ctx.step()?.do_(name, work)` checkpoint each step's result so an interrupted call replays completed steps instead of repeating mutations. See [Agent Runtime](/docs/agent-runtime) for how interrupted submissions recover.

## Schedules and channels

A scheduled agent is an ordinary agent that receives a `schedule` signal, and a channel is a webhook handler that verifies a provider signature and dispatches a signal into the target conversation. Both call `AgentRuntime::dispatch`, which creates the instance with its initial data on first contact and continues it afterwards, keyed by the delivery's idempotency key. See [Agent Channels](/docs/agent-channels).

## Models and compaction

Model ids follow the AI gateway's catalog format (`anthropic/claude-sonnet-4-6`, `openai/gpt-5.5`). The runtime derives context windows, maximum output limits, and compaction defaults from the model family, and compacts the conversation using pi's algorithm and prompts whenever the next turn would not fit. Call `use_model_with` to override the thinking level or compaction settings (`reserve_tokens`, `keep_recent_tokens`, or a separate model for summaries).

## Testing an agent

Every storage plane has an in-memory implementation paired with a scripted model provider, so you can test agent behavior without a running fleet. Build the test dependencies with `RuntimeDeps::in_memory(provider)`, create the runtime with `AgentRuntime::new(agents, deps, events)`, and send requests through `Router` against the same protocol faces a deployed app serves. Call `render_agent` to render an agent function without running it and assert on its model, tools, skills, and instructions. An eval scripts the model and checks the transcript or recorded metadata (for example verifying that a tool ran before the final answer, as in `samples/evals.rs`); against a live model, the same assertions run over the thread API.

Continue with [Agent Runtime](/docs/agent-runtime) for how renders execute and recover, [Agent API & Protocols](/docs/agent-api) for how clients call deployed agents, and [Agent Samples](/docs/agent-samples) for every pattern above as compiling Rust.
