Code (Agentic)

Agentic tool-use endpoint for coding assistants. Functions identically to /chat but additionally supports tool_use events in the SSE stream, enabling the client to execute tools on behalf of the model.

POST /code

This endpoint is the primary interface for the VSCode extension and other agentic coding clients. The model may request tool calls (file reads, searches, terminal commands, etc.), and the client is responsible for executing those tools and returning results in subsequent requests.

Note: This differs from the internal /internal/agent/code endpoint, where the server executes tools autonomously. On /code, tool execution is always delegated to the client.


Authentication

Requires one of:

  • X-Api-Key header with a valid API key

  • Authorization and X-Firebase-AppCheck headers for browser-based auth


Request Body

Parameter
Type
Required
Default
Description

model

string

No

Auto-routed

Model ID to use. Must be a model that supports tool use.

messages

array

Yes

--

Array of message objects, including tool results from prior turns.

stream

boolean

No

true

Whether to stream the response via SSE.

temperature

float

No

0.7

Sampling temperature. Range: 0.0 to 2.0.

max_tokens

integer

No

Model default

Maximum tokens to generate. Clamped to the model's maximum output limit.

session_id

string

No

--

Resume an existing conversation session.

tools

array

No

--

Tool definitions available for the model to call.

Message Object

Messages follow the Anthropic tool-use conversation format:

Field
Type
Required
Description

role

string

Yes

One of system, user, assistant, or tool.

content

string or array

Yes

Text content or array of content blocks (for tool results).

tool_use_id

string

Conditional

Required when role is tool. The ID of the tool call being responded to.

Tool Definition Object

Field
Type
Required
Description

name

string

Yes

Unique tool name (e.g., read_file, run_command).

description

string

Yes

Description of what the tool does.

input_schema

object

Yes

JSON Schema defining the tool's input parameters.


Request Example

Initial request with tool definitions:

Follow-up request with tool result:


Response (Streaming)

The SSE stream may include the following event types:

chunk -- Incremental text content

tool_use -- Model requests a tool call

When the client receives a tool_use event, it should:

  1. Execute the requested tool locally.

  2. Send a new request to /code with the tool result appended to the messages array.

  3. Continue until the model produces a final text response without further tool calls.

done -- Stream complete

error -- Error during generation


Agentic Conversation Loop

A typical agentic session follows this pattern:

Each round-trip within the same session is billed separately. The session_id returned in the done event should be passed in subsequent requests to maintain conversation context.


Credit Billing

Credit billing follows the same reservation-and-reconciliation model as /chat. Each round-trip (request/response pair) is billed independently based on the tokens consumed in that exchange.


Error Responses

Status
Description

400

Invalid request body, parameters, or tool definitions

401

Missing or invalid authentication

403

Insufficient credits or selected model does not support tool use

404

Specified model not found

429

Rate limit exceeded

500

Provider or internal server error

Last updated