Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/red-colts-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ai-sdk/anthropic": patch
---

feat (provider/anthropic): add support for inference_geo provider option
25 changes: 25 additions & 0 deletions content/providers/01-ai-sdk-providers/05-anthropic.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ The following optional provider options are available for Anthropic models:

Optional. See [Fast Mode section](#fast-mode) for more details.

- `inferenceGeo` _"us" | "global"_

Optional. See [Data Residency section](#data-residency) for more details.

- `thinking` _object_

Optional. See [Reasoning section](#reasoning) for more details.
Expand Down Expand Up @@ -249,6 +253,27 @@ const { text } = await generateText({

The `speed` option accepts `'fast'` or `'standard'` (default behavior).

### Data Residency

Anthropic supports an [`inferenceGeo` option](https://platform.claude.com/docs/en/build-with-claude/data-residency) that controls where model inference runs for a request.

```ts highlight="8-10"
import { anthropic, AnthropicLanguageModelOptions } from '@ai-sdk/anthropic';
import { generateText } from 'ai';

const { text } = await generateText({
model: anthropic('claude-opus-4-6'),
prompt: 'Summarize the key points of this document.',
providerOptions: {
anthropic: {
inferenceGeo: 'us',
} satisfies AnthropicLanguageModelOptions,
},
});
```

The `inferenceGeo` option accepts `'us'` (US-only infrastructure) or `'global'` (default, any available geography).

### Reasoning

Anthropic has reasoning support for `claude-opus-4-20250514`, `claude-sonnet-4-20250514`, and `claude-3-7-sonnet-20250219` models.
Expand Down
24 changes: 24 additions & 0 deletions examples/ai-core/src/generate-text/anthropic-inference-geo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {
anthropic,
type AnthropicLanguageModelOptions,

Check failure on line 3 in examples/ai-core/src/generate-text/anthropic-inference-geo.ts

View workflow job for this annotation

GitHub Actions / TypeScript

Module '"@ai-sdk/anthropic"' has no exported member 'AnthropicLanguageModelOptions'.
} from '@ai-sdk/anthropic';
import { generateText } from 'ai';
import { run } from '../lib/run';
import { print } from '../lib/print';

run(async () => {
const result = await generateText({
model: anthropic('claude-opus-4-6'),
prompt: 'Summarize the key points of data residency.',
providerOptions: {
anthropic: {
inferenceGeo: 'us',
} satisfies AnthropicLanguageModelOptions,
},
});

print('Content:', result.content);
print('Usage:', result.usage);
print('Finish reason:', result.finishReason);
print('Provider metadata:', result.providerMetadata);
});
41 changes: 41 additions & 0 deletions packages/anthropic/src/anthropic-messages-language-model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3077,6 +3077,47 @@
expect(result.warnings).toStrictEqual([]);
});

it('should set inference_geo in request body', async () => {
prepareJsonFixtureResponse('anthropic-text');

const result = await model.doGenerate({
prompt: TEST_PROMPT,
providerOptions: {
anthropic: {
inferenceGeo: 'us',
} satisfies AnthropicLanguageModelOptions,

Check failure on line 3088 in packages/anthropic/src/anthropic-messages-language-model.test.ts

View workflow job for this annotation

GitHub Actions / TypeScript

Cannot find name 'AnthropicLanguageModelOptions'.
},
});

expect(await server.calls[0].requestBodyJson).toMatchInlineSnapshot(`
{
"inference_geo": "us",
"max_tokens": 4096,
"messages": [
{
"content": [
{
"text": "Hello",
"type": "text",
},
],
"role": "user",
},
],
"model": "claude-3-haiku-20240307",
}
`);
expect(await server.calls[0].requestHeaders).toMatchInlineSnapshot(`
{
"anthropic-version": "2023-06-01",
"content-type": "application/json",
"x-api-key": "test-api-key",
}
`);

expect(result.warnings).toStrictEqual([]);
});

it('should pass cache_control to request body', async () => {
prepareJsonResponse({});

Expand Down
3 changes: 3 additions & 0 deletions packages/anthropic/src/anthropic-messages-language-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV2 {
...(anthropicOptions?.speed && {
speed: anthropicOptions.speed,
}),
...(anthropicOptions?.inferenceGeo && {
inference_geo: anthropicOptions.inferenceGeo,
}),
...(anthropicOptions?.cacheControl && {
cache_control: anthropicOptions.cacheControl,
}),
Expand Down
10 changes: 10 additions & 0 deletions packages/anthropic/src/anthropic-messages-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,16 @@ export const anthropicProviderOptions = z.object({
*/
speed: z.enum(['fast', 'standard']).optional(),

/**
* Controls where model inference runs for this request.
*
* - `"global"`: Inference may run in any available geography (default).
* - `"us"`: Inference runs only in US-based infrastructure.
*
* See https://platform.claude.com/docs/en/build-with-claude/data-residency
*/
inferenceGeo: z.enum(['us', 'global']).optional(),

/**
* Context management configuration for automatic context window management.
* Enables features like automatic compaction and clearing of tool uses/thinking blocks.
Expand Down
Loading