Skip to navigation

Use the Anthropic Messages API

An LLM route accepts the Anthropic Messages API, so the Anthropic SDK, Claude Code, and other Anthropic clients work with the gateway by changing their base URL. The gateway reads your request to learn the model and whether you asked for a stream, sends the same request to the provider, and relays the provider’s answer. It doesn’t translate between APIs: an Anthropic-format request goes only to a provider endpoint in the Anthropic format. This topic lists the endpoints, shows how to point an SDK at the gateway, and explains streaming, headers, and errors. To send your first request, see Authenticate and send a request.

Prerequisites

Before you begin, make sure you have the following:

Find the endpoints

Append the endpoint path to the route’s base URL. The following endpoints use the Anthropic format:

Method and pathWhat it does
POST /v1/messagesMessages.
POST /v1/messages/count_tokensToken counting.
GET /v1/modelsThe model list. On a model router route, the gateway answers it in the Anthropic shape when the request has an anthropic-version header.
GET /v1/models/<model>One model from the list.

A route serves an endpoint when its provider does. On a model router route, a POST endpoint is available when at least one of the route’s providers serves it, and GET /v1/models is always available. A path that the route doesn’t serve gets 404 with unknown LLM endpoint.

Point the Anthropic SDK at the gateway

Set the SDK’s base URL to the route’s base URL, without /v1, and send your gateway key in the fg-api-key header. For a complete example in Python and JavaScript, see Use the Anthropic SDK with Fabric AI Gateway.

Some model router routes read the gateway key from the SDK’s own header instead of fg-api-key. On those routes, pass the gateway key as api_key and leave out default_headers. The SDK then sends it as x-api-key. Ask your admin which header your route reads. For details, see Accept the gateway key from either SDK. To use Claude Code, see Use Claude Code with Fabric AI Gateway.

Stream a response

To stream, set "stream": true in the request. The gateway relays the provider’s server-sent events as they arrive, unchanged. It holds the first event until it’s complete, so that if the provider fails before that event, the gateway can still try another connection.

  • Before the stream starts. If the request fails before the provider answers, you get a JSON error, not a stream, as you would from the provider.
  • After the first event. The gateway doesn’t retry or fail over. If the connection breaks, the stream ends where it stopped, without a message_stop event.

Send headers

Send the anthropic-version header, as the Anthropic SDK does. The gateway passes anthropic-version, anthropic-beta, and your other headers to the provider unchanged, with these exceptions:

  • It removes the header that carried your gateway key, and adds the provider’s credential.
  • It removes Accept-Encoding, so responses are never compressed.

The response has the provider’s status and headers, plus X-Request-Id. On a model router route, it also has x-fabric-model, which names the <provider>/<model> that answered.

Understand errors

Errors that the gateway writes on an Anthropic-format endpoint use the Anthropic error envelope, so the SDK can parse them:

{
"type": "error",
"error": {
"type": "not_found_error",
"message": "model `anthropic/claude-9` not found"
}
}

The response has Content-Type: application/json. Errors from the provider come back exactly as the provider sent them.

StatusWhen
400, 403, 404, 413The gateway can’t serve the model you named.
404The route doesn’t serve the endpoint. The message is unknown LLM endpoint.
502The last connection the gateway tried failed. The type is api_error.
503No connection was available. The type is api_error.
504The last connection timed out. The type is timeout_error.

On a model router route, the type follows Anthropic’s names: invalid_request_error for 400, permission_error for 403, not_found_error for 404, and request_too_large for 413.

Some errors from other plugins aren’t in this envelope. A 401 from Key Auth is plain text, Unauthorized or conflicting credentials, and other plugin errors, such as a 429 from a rate limit, can be plain text too. Check the status code before you parse the body. For authentication errors, see Troubleshoot authentication errors.

Next steps