Connect a model API
Configure an OpenAI or Anthropic model provider for the built-in harness.
Connect a model
Section titled “Connect a model”Connect a model provider to createHarness() so the built-in loop can call the model API. Then compose the harness and a model with createAgent() and pass that agent to a dispatch.
Anthropic requires maxOutputTokens, hence the object form of model. Each model request is an HTTP call from your Node.js process; tools still run in the sandbox that the dispatch allocates.
Choose a protocol
Section titled “Choose a protocol”baseUrl is required for OpenAI and includes the version prefix. The protocol you choose is the only one used: an error never switches to another.
| Factory | api | Path added to baseUrl | Use it for |
|---|---|---|---|
createOpenAIModelProvider | "chat-completions" (default) | /chat/completions | OpenAI and servers that expose Chat Completions. |
createOpenAIModelProvider | "responses" | /responses | The OpenAI Responses API. |
createAnthropicModelProvider | none | /messages | The Anthropic Messages API; baseUrl defaults to https://api.anthropic.com/v1. |
createCodexHarness({ modelProvider }) is a different setting: it points the Codex CLI, inside the sandbox, at a Responses-compatible service (Codex).
Use a local endpoint
Section titled “Use a local endpoint”Set apiKey: false for a server without authentication. The address is resolved from your host, not from the sandbox.
Keep the key on the host
Section titled “Keep the key on the host”Pass the model API key explicitly to the provider. These providers do not load account logins or read environment variables automatically. The key stays in your Node.js process; Authentication explains how this differs from CLI agents.
Set the model and its reasoning
Section titled “Set the model and its reasoning”The agent’s model is a name or { name, reasoning, maxOutputTokens }. createAgent() rejects settings the provider does not support.
API reference: AgentModel.
The service still checks the model name and levels on each request.
Stream text as it arrives
Section titled “Stream text as it arrives”Both providers stream. The harness emits text-delta events while the model writes, and a reasoning event when a response contains readable reasoning. Print them from observe with if (event.kind === "text-delta") process.stdout.write(event.text) (Follow progress).
Bound each request
Section titled “Bound each request”API reference: OpenAIModelProviderOptions and AnthropicModelProviderOptions.
A timeout fails with code timeout. The harness streams with both providers, so a long answer that keeps arriving never times out: bound the whole turn with limits.
Cache the prompt prefix
Section titled “Cache the prompt prefix”The harness’s cache option, on by default, asks the provider to reuse the conversation prefix between steps.
- Anthropic
cachemarks the request for caching;cacheSystem: trueadds a breakpoint on the harness instructions, which must then exist. - OpenAIOutpost sends no cache field; OpenAI caches stable prefixes on its side.
- UsageCache reads appear in
usage.cached, and Anthropic cache writes inusage.cacheCreated.
A cache hit is never guaranteed.
Retry after rate limits and outages
Section titled “Retry after rate limits and outages”A provider sends each request once. When the service answers HTTP 429 with Retry-After, the error keeps that delay: a task retry waits at least that long, and a quota pause resumes at that time.
Rate limits fail with code quota; overloads, 5xx and connection failures are marked unavailable for fallback agents. Quota pauses list what counts as a quota.
Connect another API
Section titled “Connect another API”Implement ModelProvider: request() returns a result, stream() and validate() are optional.
Limits
Section titled “Limits”- Reasoning is replayed only to the same provider, endpoint and model. Changing one drops it from the history.
baseUrlcannot contain credentials, a query or a fragment. Redirects are refused.- Anthropic responses with content other than text, tool calls and thinking fail with
response.
API: createOpenAIModelProvider · createAnthropicModelProvider · OpenAIModelProviderOptions · AnthropicModelProviderOptions · ModelProvider · AgentModel.