# ferrite-agents app

The reference application on the agent SDK: two agents, the ferrite.json manifest, required secrets, billing paths, and deployment verification.

Your product's agent is part of your product. It deploys and rolls back with your application, keeps its conversations in your application's own storage, and declares everything it can reach in the same `ferrite.json` manifest as the rest of your routes and bindings. `ferrite-agents` is the reference application on the agent SDK. It runs as an ordinary Ferrite application, deploys from source, and draws hosting from the same prepaid balance as any other app. You only touch three source files (`agents.rs`, `lib.rs`, and `main.rs`) alongside the vendored SDK and protocol packages and `ferrite.json`.

To build your own agent application from scratch, follow [Build your own agent app](/developers/agents/#build-your-own-agent-app) and start from the manifest shape and secret contract below.

## Limits to check before you deploy

Verify four operational constraints before wiring your manifest and clients:

- Every model call goes to `<origin>/v1/responses`. The origin in `agents-gateway-url` must speak the Responses API, either Ferrite's AI gateway (which serves a supported subset for the models in its catalog) or OpenAI directly. An endpoint that exposes only Chat Completions or Anthropic's Messages API cannot serve the SDK.
- Each deployment uses one client token with no per-user identity across the agent faces. Any caller presenting `agents-client-token` holds equal authority over every agent and instance. If your agent serves multiple end users, put the `auth` package in front of your own pages and call the agent faces from your backend; see [Agent API](/developers/agent-api/#authentication).
- MCP clients must speak protocol revision `2026-07-28`. Clients on earlier revisions cannot connect to `/mcp`; see [Agent API](/developers/agent-api/#mcp).
- A turn that outlives the platform's buffered-route deadline answers `504`. The submission keeps running in the background, and the transcript at `/events` remains the authoritative record of what happened.

## The agents

All agent definitions live in `src/agents.rs`, where both reference agents run on `openai/gpt-5.4`.

`assistant` is a general assistant equipped with the Ferrite workspace, a packaged `verify-change` skill, a `researcher` delegate, and a custom `word_count` tool. When the deployment grants egress, it also mounts the web tools (`web_fetch`, `web_search`) and the read-only GitHub tools (`github_read_file`, `github_search_code`, `github_list_issues`). It omits `github_api` and `github_create_issue` on purpose: because it reads untrusted web pages and public repositories, external text could try to instruct the model to write using the deployment's GitHub token.

`triage` is a structured-output agent that classifies incoming reports. It accepts `project` as initial data, tracks how many reports it has processed in persistent state, and returns typed output through `finish` whenever the caller passes a result schema.

`registry()` registers both agents, and `lib.rs` passes that registry and the injected `AppContext` into `AgentApp`. It also calls `AgentApp::index_html` to serve an unauthenticated landing page at `GET /`. The default agent for the thread API, ACP, and A2A is the alphabetically first registered name, which here is `assistant`. To ship your own agents, edit `src/agents.rs` and redeploy; nothing else in the application is specific to these two agents.

### Layout

```text
ferrite-agents/
├─ ferrite.json                 deployment manifest: bindings, egress, routes
├─ ferrite-packages.json        the source packages vendored into src/
└─ src/
   ├─ main.rs                   the binary entry (app_main)
   ├─ lib.rs                    AgentsApp: hands the registry to AgentApp
   ├─ agents.rs                 the agents this deployment serves
   ├─ agent_sdk/  mcp/  acp/    vendored packages (never edited by hand)
   └─ http_query/  regex_lite/  crypto/
```

Author skills as markdown string constants, tools as functions returning `Tool`, and delegates as functions returning `Subagent`, grouped in modules alongside `agents.rs`. Mount inbound channels with `AgentApp::route` in `lib.rs`; see [Agent Channels](/developers/agent-channels/).

## Deployment contract

Your manifest binds the storage primitives the runtime needs, declares scoped outbound egress, and publishes the protocol faces. Model calls reach the AI gateway over ordinary HTTP egress rather than a service dependency. Ferrite Core accepts this manifest shape:

```json filename="ferrite.json (excerpt)"
{
  "services": [
    { "name": "agents-requests", "primitive": "queue", "workload": { "read_qps": 48, "write_qps": 113, "record_size_kb": 1, "keying": "PerKey", "hot_set_gib": 1 } },
    { "name": "agents-replies", "primitive": "queue", "workload": { "read_qps": 48, "write_qps": 113, "record_size_kb": 1, "keying": "PerKey", "hot_set_gib": 1 } },
    { "name": "agents-stream-requests", "primitive": "queue", "workload": { "read_qps": 48, "write_qps": 113, "record_size_kb": 1, "keying": "PerKey", "hot_set_gib": 1 } },
    { "name": "agents-session-state", "primitive": "ape", "workload": { "read_qps": 262, "write_qps": 73, "record_size_kb": 1, "keying": "PerKey", "hot_set_gib": 1 } },
    { "name": "agents-workspace-index", "primitive": "store", "workload": { "read_qps": 262, "write_qps": 177, "record_size_kb": 1, "keying": "PerKey", "hot_set_gib": 0 } },
    { "name": "agents-workspace-content", "primitive": "blob", "workload": { "read_qps": 2148, "write_qps": 3, "record_size_kb": 1, "keying": "PerKey", "hot_set_gib": 1 } }
  ],
  "applications": [
    {
      "name": "ferrite-agents",
      "artifact_key": "apps/ferrite-agents@v1",
      "artifact_sha256": "0000000000000000000000000000000000000000000000000000000000000000",
      "bindings": [
        { "handle": "requests", "kind": "queue", "service": "agents-requests" },
        { "handle": "replies", "kind": "queue", "service": "agents-replies" },
        { "handle": "stream-requests", "kind": "queue", "service": "agents-stream-requests" },
        { "handle": "session-state", "kind": "ape", "service": "agents-session-state" },
        { "handle": "workspace-index", "kind": "store", "service": "agents-workspace-index" },
        { "handle": "workspace-content", "kind": "blob", "service": "agents-workspace-content" }
      ],
      "egress": [
        { "handle": "gateway-egress", "destinations": ["https://<the gateway's host>/v1/"] },
        { "handle": "web-egress", "destinations": ["https://google.serper.dev/", "https://api.perplexity.ai/"] },
        { "handle": "github-egress", "destinations": ["https://api.github.com/"] },
        { "handle": "mcp-egress", "destinations": ["https://<an MCP server's host>/"] }
      ],
      "routes": [
        { "prefix": "/api/stream/", "binding": "stream-requests", "kind": "stream" },
        { "prefix": "/api/", "binding": "requests", "kind": "api", "forward_headers": ["x-ferrite-agents-authorization", "authorization"] },
        { "prefix": "/mcp", "binding": "requests", "kind": "api", "forward_headers": ["x-ferrite-agents-authorization", "authorization", "mcp-protocol-version", "mcp-method", "mcp-name"] },
        { "prefix": "/acp", "binding": "requests", "kind": "api", "forward_headers": ["x-ferrite-agents-authorization", "authorization"] },
        { "prefix": "/a2a", "binding": "requests", "kind": "api", "forward_headers": ["x-ferrite-agents-authorization", "authorization"] },
        { "prefix": "/.well-known/", "binding": "requests", "kind": "api" },
        { "prefix": "/", "binding": "requests", "kind": "api" }
      ],
      "compute": { "memory_mb": 512, "cpu_percent": 100 }
    }
  ]
}
```

Ferrite Core accepts only two top-level keys, `services` and `applications`, and refuses unknown keys at either level, so `bindings`, `egress`, and `routes` belong inside the application object. Replace the placeholder gateway and MCP hosts with your real destinations. To call OpenAI directly instead of the AI gateway, point `gateway-egress` at `https://api.openai.com/v1/` and omit `mcp-egress` if your agents mount no remote MCP servers. Its `compute` block requests 512 MiB of memory and 100% of one CPU; see [What it costs, and who is billed](#what-it-costs-and-who-is-billed) for how trial accounts size this down.

Ferrite's AI gateway runs at `ai.<base>` (`https://ai.ferrite.cloud` on Ferrite Cloud). Normally, admission refuses any egress destination on the platform's own domain with `APP_EGRESS_INTERNAL_DESTINATION`. Where the installation configures a resident inference origin, admission makes one explicit exception for destinations under that exact origin's `/v1/` or `/gw/v1/` path, which lets `gateway-egress` name `https://ai.<base>/v1/` (`https://ai.ferrite.cloud/v1/` on Ferrite Cloud). Bare origins, explicit ports, and every other host or path on the platform's domain are still refused.

Authenticated routes forward both `x-ferrite-agents-authorization` and `authorization` so standard Bearer clients work directly. Each service entry specifies its `workload`, supplying the values the CLI would otherwise inject before submission.

Pay attention to two route and egress details:

- The `/api/stream/` route declares neither `forward_headers` nor `stream_mode`. On Ferrite Core, a `stream` route is GET-only server-sent events with no forwarded headers, and Core rejects manifests that specify either key on it. Stream clients authenticate with a short-lived `?token=` minted from the API instead.
- `gateway-egress` carries every model call: the SDK's egress transport posts to `/v1/responses` and reads a streamed reply without a `depends` entry or app-to-app coupling. Core bounds how long a streamed outbound call can take to open and how long it can stay silent between chunks, while leaving total call duration unbounded, and applications cannot declare a per-call deadline. A slow model call can keep running after the inbound request times out with `504`; read `/events` to recover the completed turn.

By default, the SDK opens only the egress handles listed in `AppConfig.egress`: `web-egress`, `github-egress`, and `mcp-egress`. Any egress grant under a different handle name is ignored by the SDK. If none of those three handles is granted, `app::http()` returns `None` and MCP connections have no transport, so tools registered under `if let Some(http) = app::http()` are omitted. If at least one handle is granted, all tools inside that guard mount, and calls to destinations outside the granted prefixes fail with an egress denial. For instance, granting only `github-egress` still exposes `web_fetch` to the model, and invoking `web_fetch` fails at call time.

### Secrets

| Secret | Required | Use |
| --- | --- | --- |
| `agents-client-token` | yes, for any authenticated call | The client token verified by every authenticated face |
| `agents-gateway-url` | yes, for model calls | The origin for model calls: `https://api.openai.com` for direct OpenAI access, or the resident AI gateway origin `https://ai.<base>` when `gateway-egress` grants `https://ai.<base>/v1/`. The transport posts to `<origin>/v1/responses`, so the grant must cover that path. |
| `agents-gateway-key` | yes, for model calls | With `https://api.openai.com`, an OpenAI API key: the transport sends it as `Authorization: Bearer`, strips the `openai/` prefix from model IDs, and OpenAI bills that account. A Ferrite inference key or bearer token is refused there. With the AI gateway, an account-bound inference key minted through `POST /gw/v1/keys` or a Ferrite bearer token, sent in the request body's `key` field. See [What it costs, and who is billed](#what-it-costs-and-who-is-billed). |
| `agents-public-url` | for A2A | The base URL advertised in the A2A agent card |
| `serper-api-key` | optional | API key for `web_search` |
| `github-token` | optional | Token for the GitHub tools |

Once set, a secret value is never returned by any read API. The running application receives the secret in its runtime context and can only open outbound connections to the destinations declared in `ferrite.json`. On the direct path, your agents app holds the OpenAI key inside its own egress boundary. Through the AI gateway, your app holds an account inference key instead, while provider keys stay inside the gateway. See [Secrets & Egress](/developers/secrets-egress/) for the full security model.

In direct OpenAI mode, any model ID without the `openai/` prefix is forwarded unchanged, so another provider's catalog ID such as `anthropic/claude-sonnet-4-6` is rejected by OpenAI.

Set all required secrets with `ferrite secrets set` (or the MCP `set_secret` tool) before running `ferrite deploy`, because secrets only reach a process through a deploy. Verify afterward that the serving generation can complete an authenticated call; seeing a secret name in `ferrite secrets list` does not confirm that the currently running generation received it. If a deploy is interrupted, resume the attempt instead of starting a duplicate build; see [Deployments](/developers/deployments/).

### What it costs, and who is billed

Run `ferrite cost` (MCP: `estimate_cost`) before your first deploy to check your account balance, deploy fee, and projected monthly hosting cost at current rates. It charges nothing and starts no build; see [Before you deploy: what it will cost](/developers/deployments/#before-you-deploy-what-it-will-cost). Because the reference manifest requests 512 MiB of memory, a full CPU, and six storage services (queues, an APE session log, a Store index, and a Blob store), it costs more to run than a minimal web app.

A trial account runs for 48 hours with $5 of credit, enough to run an app or a few, and allows one instance per application at up to 256 MiB of memory and half a CPU. On a trial account, the manifest's larger compute request scales down to those bounds.

Model billing follows whichever path you wire:

- Through the AI gateway, calls are attributed to the account that owns the deployment. Platform-funded calls debit your Ferrite credit and stop once that balance runs out; calls backed by a sealed BYOK provider key are metered against your key cap without debiting Ferrite credit. On Ferrite Cloud, the gateway serves calls only after your account has sealed its own provider key; see [AI Gateway](/developers/ai-gateway/).
- Directly to OpenAI, as configured in the checked-in manifest, calls bill the OpenAI account that owns `agents-gateway-key`. Ferrite neither sees nor bounds that spend against your Ferrite balance.

When calling the AI gateway, each model response's `usage` object includes `billing` (`platform-credit` or `byok`) and `cost_micros`. The SDK retains both fields on `Usage` (as `billing` and `costMicros`), sums them across a turn, and returns them in the thread API turn response so your application can track exact per-turn cost and billing source. Direct OpenAI calls report neither field.

## Run it locally

Because `ferrite add` vendors the SDK and protocol packages as source inside `src/`, your application keeps a single Cargo dependency on `ferrite-app`. In your own project, the full local verification loop is:

```bash
ferrite check
ferrite test                                     # unit tests and scripted agent turns
ferrite dev --egress allow                       # exercise live Responses endpoints locally
```

Use `RuntimeDeps::in_memory` with `ScriptedProvider` for deterministic tests without network access, or run `ferrite dev --egress allow` with local secrets in `ferrite.local.toml` to drive a real Responses endpoint locally (see [Testing an agent](/developers/agent-sdk/#testing-an-agent)).

## Verify a deployment

To verify a deployed agent application end to end against a live installation:

1. Mint an account-scoped inference key with `POST /gw/v1/keys` on the AI gateway (`https://ai.<base>`).
2. Set `agents-gateway-url` to that gateway origin, `agents-gateway-key` to the minted key, and `agents-client-token` to your client bearer token.
3. Deploy the application from source with `ferrite deploy` so the running generation receives the secrets.
4. Confirm that unauthenticated calls to `/api/…`, `/mcp`, and `/acp` fail closed (`503` when no client token is configured, `401` on a bad bearer token), and that an authorized `GET /api/agent` returns `loaded: true`.
5. Run one real turn and read `/events` back until the assistant's final answer is stored.

You never deploy a gateway application of your own because the AI gateway runs as a resident service of the installation.

Continue with [Agent SDK](/developers/agent-sdk/), [Agent API & Protocols](/developers/agent-api/), and [AI Gateway](/developers/ai-gateway/).
