Skip to main content
POST
Create an Anthropic-compatible message
/v1/messages accepts compressed request bodies (Content-Encoding: gzip, deflate, or br) on authenticated JSON requests — useful for long conversations, where compressed uploads cut time-to-first-token. See Compressed Request Bodies.

Overview

The /v1/messages endpoint provides full Anthropic API compatibility. Clients using the Anthropic SDK can use NanoGPT by simply changing the base URL — no code changes required. NanoGPT accepts requests in the Anthropic Messages format, routes them to the requested NanoGPT model, and returns responses back in the Anthropic Messages shape. For non‑Anthropic models, NanoGPT transparently translates the request to an OpenAI-style chat format internally and then converts the response back to Anthropic Messages format. This endpoint supports:
  • Text generation (streaming and non-streaming)
  • Multi-turn conversations
  • Tool use (function calling)
  • Vision (images), video understanding, and document/PDF processing
  • Extended thinking (reasoning models)
  • Prompt caching
  • Token estimates via POST /api/v1/messages/count_tokens

Endpoint

Authentication

Use either header:
  • Authorization: Bearer YOUR_API_KEY
  • x-api-key: YOUR_API_KEY

Request Format

Required Fields

Optional Fields

Message Format

Messages must have a role (user or assistant) and content:
Or with structured content blocks:

Content Block Types

Text Block

Image Block (for vision-capable models)

Or with URL:
Supported media types: image/jpeg, image/png, image/gif, image/webp

Document Block (for PDF-capable models)

Video Block (for video-capable models)

Use type: "video" for new integrations. Inline video bytes use an Anthropic base64 source:
For URL transport, use an HTTPS URL and declare the video MIME type:
A document compatibility block with a video/* source is normalized as video. A document with application/pdf remains a document. Public remote sources must use HTTPS; inline base64 must be valid. See the Video Input guide for model discovery, limits, errors, segment behavior, and the equivalent Chat Completions and Responses shapes.

Tool Use Block (in assistant messages)

Tool Result Block (in user messages)

Tool Definitions

Tool Choice Options

Extended Thinking

For models that support extended thinking (reasoning):
Requirements:
  • budget_tokens must be >= 1024
  • budget_tokens must be < max_tokens
  • Model must support thinking for the exact model ID you send (check GET /api/v1/models)
:thinking is model-specific and only works when that exact ID (or a documented alias) exists. -thinking is a legacy alias pattern for some model families only, not universal. Do not assume -thinking works for arbitrary model IDs. Always check GET /api/v1/models for exact valid IDs. If the requested model does not support thinking, NanoGPT automatically ignores/strips the thinking parameter and routes the request to the base model. For Chat Completions compatibility controls, :reasoning-exclude (or reasoning.exclude) only hides reasoning output; it does not force reasoning compute off. Use reasoning_effort / reasoning.effort to control reasoning depth, and set none to disable reasoning behavior.

Response Format

Non-Streaming Response

Stop Reasons

Streaming Response (SSE)

See also: Streaming Protocol (SSE). When stream: true, the response is Server-Sent Events with named event types:

Event: message_start

Event: content_block_start

Event: content_block_delta

Event: content_block_stop

Event: message_delta

Event: message_stop

Streaming Tool Use

When the model uses tools during streaming:

Supported Models

Claude Models (Full Support)

*Use only exact thinking-capable model IDs from GET /api/v1/models.

Other Models (Via Compatibility Layer)

The v1/messages endpoint also works with non-Anthropic models: See the Models documentation for the full list.

Prompt Caching

For the full guide (supported models, thresholds, pricing, and usage fields), see Prompt Caching. NanoGPT automatically applies implicit caching on providers/models that support it (including OpenAI, Gemini, and many open-source provider/model routes), with no extra request flags. Use explicit prompt-caching controls on Claude when you need deterministic cache boundaries, TTL selection, or stickyProvider consistency control.

Enable via Header

TTL Options

  • Default: 5-minute cache TTL
  • Extended: Add extended-cache-ttl-2025-04-11 to request 1-hour TTL on Anthropic-native Claude flows

Cache Control in Content (Explicit Claude Controls)

Add cache_control to content blocks for explicit Claude caching:

Cache Usage in Response

Error Handling

For a general guide across NanoGPT APIs, see Error Handling.

Error Response Format

Error Types

All error responses include an X-Request-ID header for support requests.

Headers

Request Headers

*One of Authorization or x-api-key is required.

BYOK Headers

For Bring Your Own Key:

Examples

Basic Request (cURL)

Streaming Request (cURL)

With Tools (cURL)

Anthropic SDK (Node.js)

Anthropic SDK with Streaming (Node.js)

Anthropic SDK with Prompt Caching (Node.js)

Vision Example (Node.js)

Python SDK

Limits

Limitations

  • GPU-TEE models do not support streaming through POST /api/v1/messages. Use POST /api/v1/chat/completions if you need streaming with GPU-TEE models.

Migration from Anthropic

To migrate from Anthropic’s API to NanoGPT:
  1. Change the base URL:
    • From: https://api.anthropic.com
    • To: https://nano-gpt.com/api
    The full endpoint will be: https://nano-gpt.com/api/v1/messages
  2. Use your NanoGPT API key instead of your Anthropic key
  3. No other code changes required — the API is fully compatible

Service tier compatibility

Anthropic-style service tiers are normalized when routing to providers that support service tiers:
  • standarddefault
  • defaultdefault
  • flexflex
  • prioritypriority
  • batch → ignored for service-tier routing
Flex and priority availability is model- and provider-specific. If you explicitly force a provider that does not support service tiers, the requested tier may be ignored by the upstream provider, or routing and pricing may differ from the default route.

Notes

  • The anthropic-version header is accepted but not required
  • Token usage numbers use NanoGPT’s token accounting (may differ slightly from Anthropic’s exact counts)
  • All Anthropic SDK features are supported, including streaming, tools, and caching

Body

application/json
model
string
required
messages
object[]
required
max_tokens
integer

Response

Anthropic message response

id
string
type
string
role
string
model
string
content
object[]
stop_reason
string | null
usage
object