Skip to content

Commit 09bd27b

Browse files
authored
feat (provider/anthropic): add support for inference_geo provider option (#14339)
## Background Anthropic supports data residency controls through an `inference_geo` parameter that allows developers to specify where model inference should run for compliance and data governance requirements. See https://platform.claude.com/docs/en/build-with-claude/data-residency ## Changes Added support for the `inferenceGeo` provider option to the Anthropic provider, allowing users to control where model inference runs by specifying either `'us'` (US-only infrastructure) or `'global'` (any available geography, default). The implementation includes: - New `inferenceGeo` option in `AnthropicLanguageModelOptions` with proper validation - Request body mapping to Anthropic's `inference_geo` parameter - Documentation with usage examples - Test coverage for the new functionality - Example implementation demonstrating the feature ## Manual Verification Tested the feature by running the provided example with `inferenceGeo: 'us'` and verified that the parameter is correctly passed to Anthropic's API in the request body as `inference_geo`. ## Checklist - [x] Tests have been added / updated (for bug fixes / features) - [x] Documentation has been added / updated (for bug fixes / features) - [x] A _patch_ changeset for relevant packages has been added (for bug fixes / features - run `pnpm changeset` in the project root) - [x] I have reviewed this pull request (self-review)
1 parent 6b0a40d commit 09bd27b

File tree

6 files changed

+107
-0
lines changed

6 files changed

+107
-0
lines changed

.changeset/red-colts-complain.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ai-sdk/anthropic": patch
3+
---
4+
5+
feat (provider/anthropic): add support for inference_geo provider option

content/providers/01-ai-sdk-providers/05-anthropic.mdx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ The following optional provider options are available for Anthropic models:
130130

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

133+
- `inferenceGeo` _"us" | "global"_
134+
135+
Optional. See [Data Residency section](#data-residency) for more details.
136+
133137
- `thinking` _object_
134138

135139
Optional. See [Reasoning section](#reasoning) for more details.
@@ -224,6 +228,27 @@ const { text } = await generateText({
224228

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

231+
### Data Residency
232+
233+
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.
234+
235+
```ts highlight="8-10"
236+
import { anthropic, AnthropicLanguageModelOptions } from '@ai-sdk/anthropic';
237+
import { generateText } from 'ai';
238+
239+
const { text } = await generateText({
240+
model: anthropic('claude-opus-4-6'),
241+
prompt: 'Summarize the key points of this document.',
242+
providerOptions: {
243+
anthropic: {
244+
inferenceGeo: 'us',
245+
} satisfies AnthropicLanguageModelOptions,
246+
},
247+
});
248+
```
249+
250+
The `inferenceGeo` option accepts `'us'` (US-only infrastructure) or `'global'` (default, any available geography).
251+
227252
### Reasoning
228253

229254
Anthropic models support extended thinking, where Claude shows its reasoning process before providing a final answer.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {
2+
anthropic,
3+
type AnthropicLanguageModelOptions,
4+
} from '@ai-sdk/anthropic';
5+
import { generateText } from 'ai';
6+
import { run } from '../../lib/run';
7+
import { print } from '../../lib/print';
8+
9+
run(async () => {
10+
const result = await generateText({
11+
model: anthropic('claude-opus-4-6'),
12+
prompt: 'Summarize the key points of data residency.',
13+
providerOptions: {
14+
anthropic: {
15+
inferenceGeo: 'us',
16+
} satisfies AnthropicLanguageModelOptions,
17+
},
18+
});
19+
20+
print('Content:', result.content);
21+
print('Usage:', result.usage);
22+
print('Finish reason:', result.finishReason);
23+
});

packages/anthropic/src/anthropic-messages-language-model.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4841,6 +4841,47 @@ describe('AnthropicMessagesLanguageModel', () => {
48414841
expect(result.warnings).toStrictEqual([]);
48424842
});
48434843

4844+
it('should set inference_geo in request body', async () => {
4845+
prepareJsonFixtureResponse('anthropic-text');
4846+
4847+
const result = await model.doGenerate({
4848+
prompt: TEST_PROMPT,
4849+
providerOptions: {
4850+
anthropic: {
4851+
inferenceGeo: 'us',
4852+
} satisfies AnthropicLanguageModelOptions,
4853+
},
4854+
});
4855+
4856+
expect(await server.calls[0].requestBodyJson).toMatchInlineSnapshot(`
4857+
{
4858+
"inference_geo": "us",
4859+
"max_tokens": 4096,
4860+
"messages": [
4861+
{
4862+
"content": [
4863+
{
4864+
"text": "Hello",
4865+
"type": "text",
4866+
},
4867+
],
4868+
"role": "user",
4869+
},
4870+
],
4871+
"model": "claude-3-haiku-20240307",
4872+
}
4873+
`);
4874+
expect(await server.calls[0].requestHeaders).toMatchInlineSnapshot(`
4875+
{
4876+
"anthropic-version": "2023-06-01",
4877+
"content-type": "application/json",
4878+
"x-api-key": "test-api-key",
4879+
}
4880+
`);
4881+
4882+
expect(result.warnings).toStrictEqual([]);
4883+
});
4884+
48444885
it('should pass cache_control to request body', async () => {
48454886
prepareJsonFixtureResponse('anthropic-text');
48464887

packages/anthropic/src/anthropic-messages-language-model.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,9 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV4 {
417417
...(anthropicOptions?.speed && {
418418
speed: anthropicOptions.speed,
419419
}),
420+
...(anthropicOptions?.inferenceGeo && {
421+
inference_geo: anthropicOptions.inferenceGeo,
422+
}),
420423
...(anthropicOptions?.cacheControl && {
421424
cache_control: anthropicOptions.cacheControl,
422425
}),

packages/anthropic/src/anthropic-messages-options.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,16 @@ export const anthropicLanguageModelOptions = z.object({
197197
*/
198198
speed: z.enum(['fast', 'standard']).optional(),
199199

200+
/**
201+
* Controls where model inference runs for this request.
202+
*
203+
* - `"global"`: Inference may run in any available geography (default).
204+
* - `"us"`: Inference runs only in US-based infrastructure.
205+
*
206+
* See https://platform.claude.com/docs/en/build-with-claude/data-residency
207+
*/
208+
inferenceGeo: z.enum(['us', 'global']).optional(),
209+
200210
/**
201211
* A set of beta features to enable.
202212
* Allow a provider to receive the full `betas` set if it needs it.

0 commit comments

Comments
 (0)