> For the complete documentation index, see [llms.txt](https://infronai.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://infronai.gitbook.io/docs/frameworks-and-integrations/anthropic-sdk-compatibility.md).

# Anthropic SDK

Use Anthropic SDK with Infron AI models

Infron supports Anthropic-compatible `/v1/messages` requests through the official `@anthropic-ai/sdk`.

Use this integration when your application already uses the Anthropic SDK or needs Anthropic-native request fields such as `tools`, `thinking`, `cache_control`, `defer_loading`, or `eager_input_streaming`.

### Install

```bash
npm install @anthropic-ai/sdk
```

### Environment

```bash
export INFRON_API_KEY="your-infron-api-key"
```

Do not expose this key in browser code. Use it from a server route, API route, or backend service.

### Basic Text Generation

```ts
import Anthropic from '@anthropic-ai/sdk';

const client = new Anthropic({
  apiKey: process.env.INFRON_API_KEY,
  baseURL: 'https://llm.onerouter.pro',
});

const response = await client.messages.create({
  model: 'anthropic/claude-sonnet-4.6',
  max_tokens: 128,
  messages: [
    {
      role: 'user',
      content: 'Reply with one short synthetic status sentence.',
    },
  ],
});

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

### Streaming

```ts
import Anthropic from '@anthropic-ai/sdk';

const client = new Anthropic({
  apiKey: process.env.INFRON_API_KEY,
  baseURL: 'https://llm.onerouter.pro',
});

const stream = await client.messages.create({
  model: 'anthropic/claude-sonnet-4.6',
  max_tokens: 256,
  stream: true,
  messages: [
    {
      role: 'user',
      content: 'Write three short bullets about a synthetic test workflow.',
    },
  ],
});

for await (const event of stream) {
  if (event.type === 'content_block_delta' && event.delta.type === 'text_delta') {
    process.stdout.write(event.delta.text);
  }
}
```

### Tool Calling

```ts
import Anthropic from '@anthropic-ai/sdk';

const client = new Anthropic({
  apiKey: process.env.INFRON_API_KEY,
  baseURL: 'https://llm.onerouter.pro',
});

const response = await client.messages.create({
  model: 'anthropic/claude-sonnet-4.6',
  max_tokens: 256,
  messages: [
    {
      role: 'user',
      content: 'Use the mock lookup tool only if needed, then return a short status sentence.',
    },
  ],
  tools: [
    {
      name: 'lookup_mock_record',
      description: 'Return a synthetic record for compatibility testing.',
      input_schema: {
        type: 'object',
        properties: {
          record_id: {
            type: 'string',
            description: 'Synthetic record id.',
          },
        },
        required: ['record_id'],
      },
    },
  ],
  tool_choice: { type: 'auto' },
});

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

### Debugging Requests

Infron supports `debug_request` and `debug_response` on Anthropic-compatible requests.

Use these fields during integration testing to inspect how Infron maps your SDK request to the upstream provider request and response.

```ts
import Anthropic from '@anthropic-ai/sdk';

const client = new Anthropic({
  apiKey: process.env.INFRON_API_KEY,
  baseURL: 'https://llm.onerouter.pro',
});

const response = await client.messages.create({
  model: 'anthropic/claude-sonnet-4.6',
  max_tokens: 128,
  debug_request: true,
  debug_response: true,
  messages: [
    {
      role: 'user',
      content: 'Reply with one short synthetic status sentence.',
    },
  ],
});

console.log(response);
```

When enabled, the response may include:

* `debug_info.request`
* `debug_info.response`

Use these fields for development and validation only. Avoid logging debug payloads in production if prompts or payloads may contain sensitive data.

### Thinking

```ts
import Anthropic from '@anthropic-ai/sdk';

const client = new Anthropic({
  apiKey: process.env.INFRON_API_KEY,
  baseURL: 'https://llm.onerouter.pro',
});

const response = await client.messages.create({
  model: 'anthropic/claude-sonnet-4.6',
  max_tokens: 256,
  thinking: {
    type: 'enabled',
    budget_tokens: 1024,
  },
  messages: [
    {
      role: 'user',
      content: 'Return one short synthetic explanation.',
    },
  ],
});

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

### Prompt Caching

Use `cache_control` on content blocks or tool definitions when applicable.

```ts
import Anthropic from '@anthropic-ai/sdk';

const client = new Anthropic({
  apiKey: process.env.INFRON_API_KEY,
  baseURL: 'https://llm.onerouter.pro',
});

const response = await client.messages.create({
  model: 'anthropic/claude-sonnet-4.6',
  max_tokens: 128,
  messages: [
    {
      role: 'user',
      content: [
        {
          type: 'text',
          text: 'Synthetic reusable context for prompt cache validation.',
          cache_control: { type: 'ephemeral' },
        },
        {
          type: 'text',
          text: 'Reply with one short synthetic status sentence.',
        },
      ],
    },
  ],
});

console.log(response.usage);
```

### Anthropic Tool Options

Anthropic supports tool-level options such as:

* `defer_loading`
* `eager_input_streaming`
* `cache_control`

Use Anthropic-native snake\_case field names.

```ts
import Anthropic from '@anthropic-ai/sdk';

const client = new Anthropic({
  apiKey: process.env.INFRON_API_KEY,
  baseURL: 'https://llm.onerouter.pro',
});

const response = await client.messages.create({
  model: 'anthropic/claude-sonnet-4.6',
  max_tokens: 256,
  debug_request: true,
  debug_response: true,
  messages: [
    {
      role: 'user',
      content: 'Use mock tools only if needed, then return one short synthetic status sentence.',
    },
  ],
  tools: [
    {
      name: 'lookup_mock_deferred',
      description: 'Return a synthetic deferred record.',
      input_schema: {
        type: 'object',
        properties: {
          record_id: {
            type: 'string',
            description: 'Synthetic record id.',
          },
        },
        required: ['record_id'],
      },
      defer_loading: true,
      eager_input_streaming: true,
    },
    {
      name: 'lookup_mock_regular',
      description: 'Return a synthetic regular record.',
      input_schema: {
        type: 'object',
        properties: {
          record_id: {
            type: 'string',
            description: 'Synthetic record id.',
          },
        },
        required: ['record_id'],
      },
      defer_loading: false,
      eager_input_streaming: true,
      cache_control: { type: 'ephemeral' },
    },
  ],
  tool_choice: { type: 'auto' },
  thinking: {
    type: 'enabled',
    budget_tokens: 1024,
  },
});

console.log(response);
```

{% hint style="info" %}
Infron routes requests across available providers based on model availability, reliability, latency, and provider capability.

When a request is naturally routed to a provider that does not support a given Anthropic-specific option, that option may not appear in the final upstream request body.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://infronai.gitbook.io/docs/frameworks-and-integrations/anthropic-sdk-compatibility.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
