# Agents

How agents fit on Ferrite: coding agents that build and operate applications, product agents running inside your app with the agent SDK, and applications your users' assistants can call.

Agents meet Ferrite in three roles. A coding agent such as Claude Code or Codex builds and operates your applications through the CLI and MCP. Your own product agent runs inside a Ferrite application on the `agent-sdk` package. And your users' assistants can read and call any application that publishes agent-facing discovery files and endpoints. In every role, the agent acts through explicit tools and declared capabilities, with no ambient credentials.

Your product's agent is part of your product. You deploy and roll it back with the application, keep its conversations in the application's own storage, and declare every host it can reach in the same `ferrite.json` manifest as the rest of your routes and bindings.

## Agents that build apps

Start with [Agent setup](/developers/plugin/) to install the CLI and wire every detected coding agent to Ferrite MCP under one shared login. Point your agent at the [Agent brief](/developers/agent-brief/) as the first page to read.

Use the CLI for work that touches local files or runs through a full build lifecycle:

```bash
ferrite new webapp notes
ferrite check --json
ferrite dev --json
ferrite cost
ferrite deploy
```

Before the first deploy, have your agent run `ferrite cost` (MCP: `estimate_cost`) and report the result. It returns your account balance, the deploy fee, and each application's projected monthly cost at current rates without charging or deploying anything. See [Before you deploy: what it will cost](/developers/deployments/#before-you-deploy-what-it-will-cost).

Use MCP for account discovery, app inventory, status checks, and targeted remote operations instead of replacing a full CLI workflow with dozens of low-level MCP calls.

## Agents that run in apps

An agent running inside a Ferrite application is built on the `agent-sdk` source package. Keeping the agent inside your application avoids running a sidecar with separate deployments, storage, and credentials. You write the agent as a plain Rust function that calls hooks for a model, tools, skills, subagents, MCP connections, and a workspace, then returns its instructions. The SDK owns the hard runtime work: durable sessions on Ferrite's storage primitives (where every acknowledged write and message survives crashes, restarts, and redeploys of your application), pi's agent loop, context compaction, crash recovery, and the protocol faces clients speak.

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

pub fn assistant() -> Instruction {
    use_model("openai/gpt-5.4");
    use_sandbox(workspace());
    use_tool(word_count());
    "You are a helpful assistant working inside a Ferrite workspace.".into()
}
```

Three layers make up the stack, each with one clear boundary:

```mermaid
flowchart LR
  client["Clients<br/>console · curl · MCP · ACP · A2A"] --> app
  subgraph app["ferrite-agents app"]
    faces["Protocol faces<br/>/api · /api/stream · /mcp · /acp · /a2a"] --> sdk["agent-sdk runtime<br/>loop · sessions · compaction · recovery"]
  end
  sdk -->|APE · Store · Blob| planes[("Ferrite planes")]
  sdk -->|scoped egress| gw["AI gateway (resident service)<br/>POST /v1/responses"]
  gw -->|scoped egress| providers["Anthropic · OpenAI"]
  sdk -->|scoped egress| tools["Web · GitHub · MCP servers"]
```

| Layer | Component | Read |
| --- | --- | --- |
| Authoring and runtime | `agent-sdk` source package | [Agent SDK](/developers/agent-sdk/), [Agent Runtime](/developers/agent-runtime/) |
| Faces clients speak | The SDK's `protocols` module | [Agent API & Protocols](/developers/agent-api/) |
| Reference application | `ferrite-agents` | [ferrite-agents App](/developers/agents-app/) |
| Model access | The resident AI gateway (`ai.ferrite.cloud`) | [AI Gateway](/developers/ai-gateway/) |

Your agent app opens no raw sockets of its own. Every model call leaves through the declared `gateway-egress` handle as streaming HTTP to `/v1/responses`. Two secrets control where those calls go and how they authenticate: `agents-gateway-url` names the origin covered by `gateway-egress`, and `agents-gateway-key` carries the credential. Together they choose one of two paths:

| Model path | `agents-gateway-url` | `agents-gateway-key` | Billing and reporting |
| --- | --- | --- | --- |
| Ferrite AI gateway | `https://ai.<base>` (`https://ai.ferrite.cloud` on Ferrite Cloud) | Account inference key or Ferrite token | Attributed to the owning account; replies include `usage.billing` and `usage.cost_micros` |
| Direct to OpenAI | `https://api.openai.com` | OpenAI API key | Billed directly by OpenAI; replies omit `usage.billing` and `usage.cost_micros` |

Through Ferrite's AI gateway (available on any installation whose operator admits `https://ai.<base>/v1/` as an egress destination, as Ferrite Cloud does), your application never connects directly to a model provider. You pass an inference key minted for the deployment's owning account or a Ferrite token, and the gateway owns the model catalog, routing, metering, and outbound provider egress. For single-tenant apps, seal your account's provider key once with `PUT /gw/v1/byok/{provider}`. For multi-user apps where each signed-in user supplies their own Anthropic, OpenAI, or Gemini key, add the open-source `byok-vault` source package (`ferrite add byok-vault`) to validate keys via `POST /gw/v1/byok/verify`, encrypt them per user in your app's `Store`, and forward `x-ferrite-provider-key` + `x-ferrite-byok-only: request` to the gateway; see [AI Gateway](/developers/ai-gateway/#byok). Always point `agents-gateway-url` at the installation's published resident gateway address.

Directly to OpenAI (`https://api.openai.com`), which is what the reference `ferrite-agents` manifest grants, `agents-gateway-key` holds an OpenAI API key sent as `Authorization: Bearer`. The SDK strips the `openai/` prefix from the model id, and OpenAI bills that account with no `usage.billing` or `usage.cost_micros` in the reply. On this path the OpenAI key lives as an application secret: you set it once, reads never return it, the running application receives its value, and egress grants restrict outbound traffic to declared destinations. Ferrite does not meter direct calls and your prepaid balance does not cap them, so set a spend limit in your OpenAI account.

On either path, the running application receives the value stored in `agents-gateway-key`. When calling the AI gateway, mint a dedicated inference key with a 30-day spend cap so a leaked credential cannot spend past that limit. See [Secrets & Egress](/developers/secrets-egress/) for how secrets are stored and delivered.

## Build your own agent app

Because `agent-sdk` is a source package, `ferrite add agent-sdk` vendors the entire stack (pi's agent loop, Flue-style hooks, and the protocol faces) into your project while keeping `ferrite-app` as your only Cargo dependency.

The default `webapp` scaffold manifest does not fit an agent app, and `ferrite add agent-sdk` adds no bindings or routes to `ferrite.json`. `AgentApp` requires an APE binding named `session-state` and stops at startup without it; it reads HTTP ingress from `requests`, serves streams from `stream-requests`, and stores workspace data on `workspace-index` and `workspace-content`. Protocol faces only answer where `ferrite.json` publishes `/api/`, `/api/stream/`, `/mcp`, `/acp`, `/a2a`, and `/.well-known/`. Start your manifest from a renamed copy of the reference manifest in [ferrite-agents App](/developers/agents-app/):

```bash
ferrite new webapp helpdesk
ferrite add agent-sdk
# replace ferrite.json with a renamed copy of the Agents app's manifest
# write your agent: a function composing use_model / use_tool / use_skill
openssl rand -base64 32 | ferrite secrets set agents-client-token
printf %s 'https://api.openai.com' | ferrite secrets set agents-gateway-url
printf %s "$OPENAI_API_KEY" | ferrite secrets set agents-gateway-key
ferrite check --remote
ferrite deploy
```

Once deployed, `AgentApp::new(ctx).agents(registry()).serve()` exposes the thread API, SSE streaming, MCP, ACP, and A2A over the same durable sessions. Without `agents-client-token`, every authenticated face fails closed with `503`. `ferrite secrets set` reads values from standard input; set `agents-gateway-url` and `agents-gateway-key` for whichever model path you chose above. Run `ferrite check --remote` before your first deploy to confirm the platform gate admits your crate and manifest. Read [ferrite-agents App](/developers/agents-app/) for the full manifest and secret contract, and [Agent Samples](/developers/agent-samples/) for compiling patterns you can copy into your app.

## Make an app usable by your users' assistants

An agent-ready Ferrite application serves clean Markdown representations, discovery files, OpenAPI, and MCP from one `Surface` declaration, and can register browser tools for the same operations. Assistants receive no authorization shortcut: protected actions reuse the signed-in browser session or require an explicit delegated token. Follow [Build for agents](/developers/build-for-agents/) to wire this into the Quickstart app step by step.

Continue with [Agent-ready apps](/developers/agent-native-apps/), [MCP Servers](/developers/mcp-servers/), and [WebMCP & OpenAPI](/developers/webmcp-openapi/).
