Anthropic SDK

The exchange serves Anthropic Messages, so the official Python and TypeScript clients work without changes. Set the origin of the exchange and your key.

Use this SDK for direct Messages API calls. To run Claude Code and its tools from your application, use the Claude Agent SDK.

The Liquid CLI prints the setup for the Anthropic SDK with your model filled in. To run your own program with the exchange in its environment, use liquid exec. It sets OPENAI_BASE_URL, ANTHROPIC_BASE_URL and the keys for that process only.

curl -fsSL https://inference.ai.exchange/cli/install.sh | bash
liquid login
liquid connect anthropic-sdk
liquid exec -- python app.py

To set up the Anthropic SDK yourself, follow the steps below.

Before you start

1. Point the client at the exchange

Use the origin without /v1, because this client adds /v1/messages itself. If the URL ends in /v1, the client requests /v1/v1/messages.

Python

import os
from anthropic import Anthropic

client = Anthropic(
    base_url="https://router.inference.ai.exchange",
    api_key=os.environ["LIQUID_API_KEY"],
)

client.messages.create(
    model="liquid.auto",
    max_tokens=1024,
    messages=[{"role": "user", "content": "hello"}],
)

TypeScript

import Anthropic from '@anthropic-ai/sdk'

const client = new Anthropic({
  baseURL: 'https://router.inference.ai.exchange',
  apiKey: process.env.LIQUID_API_KEY,
})

const message = await client.messages.create({
  model: 'liquid.auto',
  max_tokens: 1024,
  messages: [{ role: 'user', content: 'hello' }],
})

The client sends the key in the x-api-key header, which the exchange accepts. Use any model, router model or preset on the book. GET /v1/anthropic/models lists the names. See Market data.

2. Read the cap before the first token

The x-liquid-job-id and x-liquid-cap-usd headers arrive before any token. cap_usd is the maximum charge for the job.

Read the headers from the raw response: with_raw_response (Python) or .asResponse() (TypeScript).

3. Cache a prompt prefix

cache_control works as it does with Anthropic. Mark the last block of the prefix to cache. The exchange changes your markers into the cache plan of the job, for the winning provider.

client.messages.create(
    model="liquid.auto",
    max_tokens=1024,
    system=[
        {"type": "text", "text": "You are terse."},
        {
            "type": "text",
            "text": long_reference_document,
            "cache_control": {"type": "ephemeral", "ttl": "1h"},
        },
    ],
    messages=[{"role": "user", "content": "what changed in section 4?"}],
)
Marked block Becomes
A block of system A breakpoint at the end of the system prompt
A tool definition A breakpoint after the last tool
A block inside a turn A breakpoint at the end of that turn
Top-level cache_control A breakpoint after the last message

Rules for breakpoints:

The usage block reports cached input separately from fresh input:

{
  "input_tokens": 412,
  "cache_read_input_tokens": 10000,
  "cache_creation_input_tokens": 0,
  "output_tokens": 128
}

input_tokens counts only the input that the cache did not read or write, so the sum of the three does not count cached input two times.

If a request has no markers, the exchange places its own breakpoints. It does this only if both conditions are true:

To keep only your own markers, or no markers, send exchange.cache.mode as buyer or off:

client.messages.create(
    model="liquid.auto",
    max_tokens=1024,
    messages=[{"role": "user", "content": "hello"}],
    extra_body={"exchange": {"cache": {"mode": "buyer"}}},
)

See Prompt caching for how the exchange places breakpoints, prices cached input, and keeps your cache separate from other buyers.

Limits and details

Output limit

The Anthropic format requires max_tokens.

Hosted tools

The exchange does not run hosted tools.

Reasoning blocks

The reasoning of a model comes back as thinking blocks before the answer. This is also true for models that are not Claude. Encrypted reasoning comes back as redacted_thinking.

Cost control

Next steps