Retrieved article excerpt
Open article · Retrieved 2026-10-18T12:22:46.979503+00:00
# agent-bridge
**English** · [Italiano](https://github.com/R3n3r0/agent-bridge/blob/main/README.it.md)
`agent-bridge` is an HTTP server written in Go that exposes an OpenAI-compatible
API (`/v1/chat/completions`, `/v1/models`) and fulfils every request by running
the official Claude Code CLI in headless mode
(`claude -p --output-format stream-json`).
It can also run the OpenAI Codex CLI (`codex exec --json`) or the Kimi Code
CLI (`kimi acp`, over the Agent Client Protocol) instead, with the same API:
see the [Codex backend](https://github.com/R3n3r0/agent-bridge/blob/main/docs/codex.md) and the [Kimi backend](https://github.com/R3n3r0/agent-bridge/blob/main/docs/kimi.md).
Any client or framework that speaks OpenAI can use it by changing only
`base_url`, `api_key` and `model`: chat, streaming, function calling (tool
calling) and image input work with the official SDKs unchanged.
In short: an **OpenAI-compatible API server, proxy and gateway for Claude
Code**. It lets OpenAI-based multi-agent systems, ReAct orchestrators and
frameworks that accept a custom OpenAI `base_url` (for example LangChain,
LlamaIndex, CrewAI, AutoGen or the OpenAI SDKs for Python, Node and Go) use
Claude Code headless as a drop-in chat completions model or as a coding agent,
with tool calling, MCP servers, sessions, streaming (SSE) and per-agent models.
The same server is an OpenAI-compatible API, proxy and gateway for the
OpenAI Codex CLI and for the Kimi Code CLI (Moonshot AI, Kimi K2), and an
ACP (Agent Client Protocol) client.
> **Independent project.** agent-bridge is not developed, endorsed or
> supported by Anthropic. It runs the official `claude` binary installed on
> the machine, without modifying it, and never accesses its credentials.
- Single binary, Go standard library only at runtime.
- Linux, macOS and Windows (amd64 and arm64).
- Same build for development (subscription login) and production (API key).
- OpenAI function calling (`tools`, `tool_calls`), streaming included.
- Image input (`image_url` parts: data URLs and http(s) URLs).
- Three CLI backends: the default one, the OpenAI Codex CLI
(`backend: "codex"`, see [docs/codex.md](https://github.com/R3n3r0/agent-bridge/blob/main/docs/codex.md)) and the Kimi Code
CLI (`backend: "kimi"`, see [docs/kimi.md](https://github.com/R3n3r0/agent-bridge/blob/main/docs/kimi.md)).
- Free software, licensed under the GNU GPL v3 or later.
## Two ways to use it
| | As a model | As an agent on the server |
| --- | --- | --- |
| Who defines the agent | Your client or platform: `system`, `tools`, model choice | The bridge configuration file |
| Available tools | Only the functions the client sends in `tools`, executed by the client | CLI tools on the server machine (files, commands, search, MCP servers), plus any client functions |
| `model` | `bridge/opus`, `bridge/sonnet`, `bridge/haiku`, `bridge/<model name>` | The name of the configured agent |
| Configuration | None | JSON file |
| Section | [Using it as a model](https://github.com/R3n3r0/agent-bridge#using-it-as-a-model) | [Agents on the server](https://github.com/R3n3r0/agent-bridge#agents-on-the-server) |
"Tools" therefore come in two kinds, both supported:
- **client functions** (OpenAI function calling): defined and executed by the
client; the model calls them through `tool_calls`;
- **agent tools**: `Read`, `Edit`, `Bash`, MCP servers… executed by the CLI on
the server, within the agent's permissions; the client only sees the final
result.
## Quick start
Prerequisite: the `claude` CLI installed, version **2.1.128 or later**, and one
interactive login (`claude`).
```
go install github.com/r3n3r0/agent-bridge/cmd/agent-bridge@latest
AGENT_BRIDGE_AUTH_MODE=subscription agent-bridge
curl -s http://127.0.0.1:8787/v1/chat/completions \
-d '{"model":"bridge/haiku","messages":[{"role":"user","content":"Hello!"}]}'
```
Without a configuration file the server listens on `127.0.0.1:8787` and accepts
the models `bridge/opus`, `bridge/sonnet`, `bridge/haiku` and
`bridge/<model name>` (the name after the prefix is passed to the CLI as
is; the `bridge/` prefix can be changed with `builtin_prefix`). They are
*chat-only* models: no server tools, an empty temporary working directory,
but they accept client functions and images.
Alternatively, download a binary from the releases page (archives and
`checksums.txt` for every platform).
## Using it as a model
This is the scenario of a platform that already has its own agents, graph and
lifecycle (for example a ReAct system): the bridge behaves like an OpenAI
model and the agent is the one defined in the platform.
| In the platform | In the request |
| --- | --- |
| Agent instructions | `system` message |
| Agent tools | `tools` |
| Model | `bridge/<alias or model name>` |
Two options make the bridge stateless and make the platform's agent the only
agent. Set them in the configuration, or per request with a header:
| Configuration key | Header | Effect |
| --- | --- | --- |
| `client_tools.mode: "stateless"` | `X-Agent-Bridge-Tool-Mode: stateless` | After returning `tool_calls` the process exits: every request is one step of the graph and the history sent by the client is the only source of truth. Branches, retries and checkpoints stay in the platform |
| `system_messages: "replace"` | `X-Agent-Bridge-System-Mode: replace` | `system` messages become the effective system prompt instead of the CLI one, at every step. With `append` they are added to it |
Recommended configuration for this scenario:
```
{ "client_tools": { "mode": "stateless" }, "system_messages": "replace" }
```
In this scenario do not send `X-Session-Id`: every request starts a new process
with the full history. It costs some latency per step, in exchange for no
hidden state in the bridge.
## Function calling
Functions defined by the client with `tools` work as with the OpenAI API: the
model answers with `tool_calls` and `finish_reason: "tool_calls"`, the client
runs the functions and sends the results back as `role: "tool"` messages, and
the model continues. It also works when streaming (`delta.tool_calls`) and
without session headers.
```
r = client.chat.completions.create(model="bridge/sonnet", messages=messages, tools=tools)
while r.choices[0].finish_reason == "tool_calls":
messages.append(r.choices[0].message)
for call in r.choices[0].message.tool_calls:
messages.append({"role": "tool", "tool_call_id": call.id,
"content": run(call.function.name, call.function.arguments)})
r = client.chat.completions.create(model="bridge/sonnet", messages=messages, tools=tools)
```
Complete example: [examples/python/tools.py](https://github.com/R3n3r0/agent-bridge/blob/main/examples/python/tools.py).
Supported: `tools` (type `function`), `tool_choice` (`auto`, `none`,
`required`, a specific function), `parallel_tool_calls` (parallel calls
returned together) and the deprecated `functions`/`function_call` format.
**How it works.** Client functions are offered to the CLI as tools of a
standard MCP server, served by the bridge itself (internal `mcp-tools`
subcommand). When the model calls one, the bridge returns the call to the
client. Then, depending on `client_tools.mode`:
| Mode | What happens | When to choose it |
| --- | --- | --- |
| `stateful` (default) | The process waits; the request carrying the results is recognized by its `tool_call_id`s and the same process resumes where it stopped | Simple clients: lower latency between steps |
| `stateless` | The process exits; the request carrying the results starts a new process with the full history | Orchestrators with their own state (see [Using it as a model](https://github.com/R3n3r0/agent-bridge#using-it-as-a-model)) |
Differences to be aware of:
- `tool_choice` other than `auto`/`none` and `parallel_tool_calls: false` are
expressed as instructions to the model: the model follows them, but they
are not hard constraints as in the OpenAI API. Function `parameters`
schemas (and `strict`) also guide the model but are not enforced.
- In `stateful` mode the waiting process holds a concurrency slot. If the
results do not arrive within `client_tools.wait_timeout_seconds` (default
600) the process is stopped; a later request with the results starts a new
process from the full history, so it still works.
- With the deprecated `functions` format results carry no id: every call ends
the process, as in `stateless` mode.
- In a `tool_calls` response `usage` covers only the model's last message;
turn totals are in the logs and in `/stats`.
## Images
Messages can contain `image_url` parts, as in the OpenAI API, with any model:
```
r = client.chat.completions.create(model="bridge/sonnet", messages=[{"role": "user", "content": [
{"type": "text", "text": "What is in this picture?"},
{"type": "image_url", "image_url": {"url": "data:image/png;base64,..."}},
]}])
```
- **Sources**: `data:` URLs (base64) and `http(s)` URLs, which the bridge
downloads. Downloads from non-public addresses (loopback, private networks,
cloud metadata) are refused unless `images.allow_private_networks` is set;
`images.fetch_urls: false` accepts only `data:` URLs.
- **Formats**: PNG, JPEG, GIF, WebP, recognized from the content. Up to
`images.max_count` images per request (default 20) and `images.max_bytes`
each (default 5 MB); the whole input, images included, must stay within
9 MB.
- **How**: images are sent to the CLI as image blocks of a structured
message (`--input-format stream-json`); the text refers to them as
`[image 1]`, `[image 2]`… in order. Requests without images keep plain-text
input.
- In a resumed session only the images of the new messages are sent; earlier
ones are already in the session. `images.enabled: false` rejects images with
400 `unsupported_content`.
## Generation parameters
The bridge drives the CLI, not the model API directly, so it supports the
OpenAI parameters the CLI can express, and emulates two more:
| OpenAI parameter | How | Fidelity |
| --- | --- | --- |
| `reasoning_effort` | CLI `--effort`; `none`/`minimal` become `low`, `xhigh` and `max` are accepted as an extension | Direct |
| `response_format` (`json_object`, `json_schema`) | CLI `--json-schema`: the answer is validated against the schema and returned as compact JSON in `content` | Direct, validated |
| `max_tokens`, `max_completion_tokens` | `CLAUDE_CODE_MAX_OUTPUT_TOKENS` for the process | Partial: limits every model response of the turn, not the turn total; `finish_reason` is not set to `length` |
| `stop` (string or up to 4) | The final text is cut at the first occurrence | Emulated: the model still generates the full answer; never applied to `response_format` JSON or to progress texts |
| `n` (1-8) | `n` independent processes in parallel, one choice each, also when streaming | Emulated: costs `n` times; no session resume, and client functions run stateless |
| `temperature`, `top_p`, `seed`, `logprobs`, `presence_penalty`, `frequency_penalty`, `logit_bias` | — | Accepted and ignored: the CLI has no equivalent |
If `response_format` cannot be satisfied (the CLI gives up after its retries,
or ends without structured output), the request fails with 502.
## Agents on the server
A JSON file maps a model name to an agent: model, system prompt, CLI tools,
permissions and working directory. Use it when the agent must work on the
server machine, for example on a repository.
```
{
"agents": {
"reviewer": {
"model": "opus",
"system_prompt": "You are a strict reviewer. Do not modify files.",
"allowed_tools": ["Read", "Grep", "Glob"],
"cwd": "/srv/project"
},
"coder": {
"model": "sonnet",
"allowed_tools": ["Read", "Edit", "Write", "Bash(go test *)"],
"permission_mode": "acceptEdits",
"cwd": "/srv/project"
}
}
}
```
```
agent-bridge check-config -config agents.json # validate and print the resolved age