# Anthropic SDK (https://inference.net/docs/api/anthropic-sdk)

The Inference.net API supports the Anthropic Messages format at `POST https://api.inference.net/v1/messages`. This means you can use the Anthropic SDK to call any serverless model hosted on Inference.net, such as `glm-5.2`. Only your Inference.net API key is needed.

Requests are converted to and from the model's native format for you. Both non-streaming and streaming responses are supported.

<Callout tone="info">

This page covers calling Inference.net models with the Anthropic SDK. To route requests to Anthropic's own Claude models using your Anthropic API key, proxy them through the [Inference.net Gateway](/docs/api/quickstart/#2-proxy-through-the-inferencenet-gateway) with the `anthropic` provider instead.

</Callout>

## Authentication

The Inference.net API accepts your Inference.net API key in either `x-api-key` (which the Anthropic SDK sends via `apiKey` / `ANTHROPIC_API_KEY`) or `Authorization: Bearer <key>` (`authToken` / `auth_token` / `ANTHROPIC_AUTH_TOKEN`). If both headers are sent, `Authorization` takes precedence for Inference.net authentication; `x-api-key` is stripped on Inference.net-hosted requests, and on external-provider (BYOK) gateway requests it is forwarded unchanged unless a provider credential replaces it.

```bash
export INFERENCE_API_KEY=<your-api-key>
```

## Non-streaming

<CodeTabs>

```typescript
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic({
  baseURL: "https://api.inference.net",
  apiKey: null,
  authToken: process.env.INFERENCE_API_KEY,
});

const message = await client.messages.create({
  model: "glm-5.2",
  max_tokens: 1024,
  messages: [{ role: "user", content: "What is the meaning of life?" }],
});

console.log(message.content);
```

```python
import os
from anthropic import Anthropic

client = Anthropic(
    base_url="https://api.inference.net",
    auth_token=os.environ["INFERENCE_API_KEY"],
)

message = client.messages.create(
    model="glm-5.2",
    max_tokens=1024,
    messages=[{"role": "user", "content": "What is the meaning of life?"}],
)

print(message.content)
```

```bash
curl https://api.inference.net/v1/messages \
  -H "Authorization: Bearer $INFERENCE_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{
    "model": "glm-5.2",
    "max_tokens": 1024,
    "messages": [{"role": "user", "content": "What is the meaning of life?"}]
  }'
```

</CodeTabs>

## Streaming

<CodeTabs>

```typescript
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic({
  baseURL: "https://api.inference.net",
  apiKey: null,
  authToken: process.env.INFERENCE_API_KEY,
});

const stream = client.messages.stream({
  model: "glm-5.2",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Count from 1 to 5." }],
});

stream.on("text", (text) => {
  process.stdout.write(text);
});

const message = await stream.finalMessage();
console.log("\n", message.usage);
```

```python
import os
from anthropic import Anthropic

client = Anthropic(
    base_url="https://api.inference.net",
    auth_token=os.environ["INFERENCE_API_KEY"],
)

with client.messages.stream(
    model="glm-5.2",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Count from 1 to 5."}],
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)
```

```bash
curl -N https://api.inference.net/v1/messages \
  -H "Authorization: Bearer $INFERENCE_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{
    "model": "glm-5.2",
    "max_tokens": 1024,
    "stream": true,
    "messages": [{"role": "user", "content": "Count from 1 to 5."}]
  }'
```

</CodeTabs>

<Callout tone="info">

The Messages format requires `max_tokens`. Reasoning models such as `glm-5.2` spend part of the token budget on reasoning before producing text, so set `max_tokens` high enough for both. If the budget runs out during reasoning, the response can come back with empty content.

</Callout>

The same code works for every serverless model. Set `model` to the model ID you want. Browse available models in the [model catalog](/models/).