Agents and integrations

Use Foundry with Claude Code and the Claude desktop app.

Point Claude Code or the Claude desktop app at Foundry with two settings and your key. Every Claude model the gateway lists is paid from your USDF balance, and every answer carries a receipt.

On this page

You need a Foundry API key with USDF on its balance. Create one in the dashboard; see Accounts and keys.

Both apps speak the Messages API. They take the gateway's address without /v1, https://api.usdf.fi, and add /v1/messages themselves. Replace sk_YOUR_KEY below with your key.

Set the address and the key in the shell, then start claude from that shell.

Shell

export ANTHROPIC_BASE_URL=https://api.usdf.fi
export ANTHROPIC_AUTH_TOKEN=sk_YOUR_KEY
claude

To check the address and the key before you start, send one request. An answer that starts with {"id":"req_ means both work; a 401 means the key was not read.

Check

curl -sS -X POST https://api.usdf.fi/v1/messages \
  -H "Authorization: Bearer $ANTHROPIC_AUTH_TOKEN" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{"model": "MODEL_ID", "max_tokens": 1, "messages": [{"role": "user", "content": "."}]}'

To keep the setting for every session, put it in the env block of ~/.claude/settings.json. A setting there applies wherever Claude Code runs, background sessions included. Keep the key out of a project's .claude/settings.json: that file is committed with the project.

~/.claude/settings.json

{
  "env": {
    "ANTHROPIC_BASE_URL": "https://api.usdf.fi",
    "ANTHROPIC_AUTH_TOKEN": "sk_YOUR_KEY"
  }
}

In Claude Code, /status shows the base URL and the credential in use.

The settings

SettingWhat it does
ANTHROPIC_BASE_URLThe gateway's address, https://api.usdf.fi, without /v1.
ANTHROPIC_AUTH_TOKENYour key, sent as Authorization: Bearer.
ANTHROPIC_API_KEYInstead of the token: your key, sent as x-api-key. The gateway reads either; Claude Code asks once before it uses a key set here. Set one of the two.
ANTHROPIC_MODELThe model a session starts on. Optional: see Models.
ANTHROPIC_DEFAULT_OPUS_MODEL, ANTHROPIC_DEFAULT_SONNET_MODEL, ANTHROPIC_DEFAULT_HAIKU_MODELThe models the opus, sonnet and haiku choices of /model run. Optional.
CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY1: the /model picker lists the Claude models the gateway serves.
CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC1: Claude Code sends nothing outside the gateway but what it needs (no update checks or telemetry). Optional.

The gateway serves Claude models under their own ids, so Claude Code's default model and its opus, sonnet and haiku choices work with no model setting. To pin them to the newest models the gateway lists now:

~/.claude/settings.json, with the models

{
  "env": {
    "ANTHROPIC_BASE_URL": "https://api.usdf.fi",
    "ANTHROPIC_AUTH_TOKEN": "sk_YOUR_KEY",
    "CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY": "1",
    "ANTHROPIC_MODEL": "MODEL_ID",
    "ANTHROPIC_DEFAULT_OPUS_MODEL": "MODEL_ID",
    "ANTHROPIC_DEFAULT_SONNET_MODEL": "MODEL_ID",
    "ANTHROPIC_DEFAULT_HAIKU_MODEL": "MODEL_ID"
  }
}

ANTHROPIC_DEFAULT_HAIKU_MODEL also runs Claude Code's background tasks, such as session titles and summaries. Without it they run on the main model.

A chat model of another family runs Claude Code too: set ANTHROPIC_MODEL to its id from Models, and pick one that calls tools, since Claude Code works through tool calls. Claude Code then warns that it does not know the id and keeps the conversation within the window it assumes for an unknown model.

Another model

ANTHROPIC_MODEL=MODEL_ID claude

With CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY=1, Claude Code reads the gateway's model list when it starts and adds the Claude models to /model, each with the line the gateway gives for it. A model that one of the built-in opus, sonnet or haiku rows already runs shows as that row. Models of other families are not listed there; choose them with ANTHROPIC_MODEL.

The list is kept in ~/.claude/cache/gateway-models.json and read again at each start.

The desktop app takes the gateway from its third-party inference settings, not from ANTHROPIC_BASE_URL or settings.json.

  1. Open Help, Troubleshooting, Enable Developer Mode. The app restarts with a Developer menu.
  2. Open Developer, Configure Third-Party Inference.
  3. In Connection, set Inference provider to Gateway.
  4. Set Gateway base URL to https://api.usdf.fi and Gateway API key to your key. Leave Credential kind on Static API key and Gateway auth scheme on Bearer; x-api-key works too.

The app lists the Claude models the gateway serves. To choose the list yourself, give their ids in inferenceModels; the first is the default.

For a rollout to many devices, the same settings are configuration keys:

Configuration keys

{
  "inferenceProvider": "gateway",
  "inferenceGatewayBaseUrl": "https://api.usdf.fi",
  "inferenceGatewayApiKey": "sk_YOUR_KEY",
  "inferenceGatewayAuthScheme": "bearer"
}

Checked with Claude Code against the gateway:

  • Streaming: the answer arrives event by event, and while a model works without writing, ping events keep the connection open.
  • Tools: Claude Code's tools run as usual, parallel calls, results and errors included, and its tool search loads the tools it holds back.
  • Thinking: adaptive and budgeted thinking, with each signed thinking block sent back on the next turn.
  • Prompt caching: Claude Code's cache breakpoints are kept, and the usage of each answer reports what was read from the cache and written to it.
  • Token counting: /context counts through POST /v1/messages/count_tokens.
  • Model discovery: the /model picker lists the gateway's Claude models.
  • Sessions: claude --resume carries on a conversation.
  • Other models: a model of another family answers and runs Claude Code's tools.

Every answer carries its receipt: what was charged and what it saved. See Receipts.

  • Token counts are the gateway's estimate, close to what is billed but not exact: /context shows approximate figures. What a request is charged is the provider's own count.
  • Tools that run on the API's side (its web search, code execution and connectors) are not run by the gateway: a request that asks for one is refused. Tools that run on your machine are not affected.
  • Context editing is accepted and the conversation is sent whole; Claude Code compacts it as usual.
  • Every request runs at standard speed.
What you seeWhyWhat to do
402 insufficient balance for this requestThe balance does not cover what the request can cost at the output Claude Code asks for.Add USDF to the balance; see Accounts and keys.
401 missing or malformed API keyNo Foundry key reached the gateway.Set ANTHROPIC_AUTH_TOKEN (or ANTHROPIC_API_KEY) to a key that starts with sk_, in the shell Claude Code starts from or in ~/.claude/settings.json.
404 model ... is not a model this gateway serves; the closest: ...The id is not on the gateway's list.Use an id from Models or from /model with discovery on.
400 input length and max_tokens exceed context limitThe model's window is smaller than the prompt and the answer Claude Code asks for.Claude Code asks again with less output, or compacts. If it stops there, choose a model with a larger window (its window is on its model page).
400 prompt is too longThe conversation alone is past the model's window.Claude Code compacts it; run /compact if it does not.
Claude Code doesn't recognize the modelThe id is not a Claude id it knows.Expected with another family's model: it runs, within the window Claude Code assumes.
Gateway models missing from /modelDiscovery is off.Set CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY=1 and start Claude Code again.
WebSearch says it is not availableClaude Code's web search runs on the API's side, which the gateway does not run.Search with a tool that runs on your machine, such as an MCP server.

Next: Cursor, Codex, Cline and other coding agents

Foundry documentation