Skip to content
Merged
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 @@ -130,6 +130,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 @@ -224,6 +228,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 models support extended thinking, where Claude shows its reasoning process before providing a final answer.
Expand Down
23 changes: 23 additions & 0 deletions examples/ai-functions/src/generate-text/anthropic/inference-geo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {
anthropic,
type 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);
});
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 @@ -4389,6 +4389,47 @@ describe('AnthropicMessagesLanguageModel', () => {
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,
},
});

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 () => {
prepareJsonFixtureResponse('anthropic-text');

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 @@ -387,6 +387,9 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
...(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 @@ -190,6 +190,16 @@ export const anthropicLanguageModelOptions = 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(),

/**
* A set of beta features to enable.
* Allow a provider to receive the full `betas` set if it needs it.
Expand Down
Loading