> ## Documentation Index
> Fetch the complete documentation index at: https://docs.inference.net/llms.txt
> Use this file to discover all available pages before exploring further.

# Anthropic SDK

> Call models on Inference.net using the Anthropic SDK and the Messages API.

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 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.

<Info>
  This page covers calling Inference-hosted models with the Anthropic SDK. To route requests to Anthropic's own Claude models using your Anthropic API key, see the [Anthropic integration](/integrations/model-providers/anthropic) instead.
</Info>

## Authentication

The Anthropic SDK sends its `apiKey` as the `x-api-key` header, but the Inference API expects your key as a Bearer token in the `Authorization` header. Pass your Inference API key as `authToken` (TypeScript) or `auth_token` (Python), which sends `Authorization: Bearer <key>`. In TypeScript, also set `apiKey: null` so the SDK does not look for an Anthropic key.

<Metadata text="api/anthropic-sdk/env-vars" />

```bash theme={"system"}
export INFERENCE_API_KEY=<your-api-key>
```

## Non-Streaming

<CodeGroup>
  <Metadata text="api/anthropic-sdk/non-streaming" />

  ```typescript TypeScript theme={"system"}
  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);
  ```

  <Metadata text="api/anthropic-sdk/non-streaming" />

  ```python Python theme={"system"}
  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)
  ```

  <Metadata text="api/anthropic-sdk/non-streaming-curl" />

  ```bash cURL theme={"system"}
  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?"}]
    }'
  ```
</CodeGroup>

## Streaming

<CodeGroup>
  <Metadata text="api/anthropic-sdk/streaming" />

  ```typescript TypeScript theme={"system"}
  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);
  ```

  <Metadata text="api/anthropic-sdk/streaming" />

  ```python Python theme={"system"}
  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)
  ```

  <Metadata text="api/anthropic-sdk/streaming-curl" />

  ```bash cURL theme={"system"}
  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."}]
    }'
  ```
</CodeGroup>

<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.
</Info>

The same code works for every serverless model. Set `model` to the model id you want. Browse available models at [inference.net/models](https://inference.net/models).
