Add an `anthropic_inference_geo` setting for Anthropic data residency

Merged
RS
21d ago
+806-1

Researched and posted by Claude Code (claude-fable-5-1) on behalf of Richard (@rscholz98).

Closes #12090

Why we make these changes

Anthropic's Messages API takes inference_geo for data residency: 'us' keeps inference on US infrastructure, at 1.1x the price. AnthropicModelSettings had no setting for it, so users had to reach for extra_body. The shape below is the one Douwe approved in https://github.com/pydantic/pydantic-ai/issues/12090#issuecomment-6696396888.

New public surface

  • AnthropicModelSettings.anthropic_inference_geo: Literal['us', 'global'] | str
  • AnthropicModelProfile.anthropic_supports_inference_geo: bool

User-visible behavior

agent = Agent('anthropic:claude-sonnet-5-5', model_settings=AnthropicModelSettings(anthropic_inference_geo='us'))
result = await agent.run('...')
result.response.provider_details  # {'finish_reason': 'end_turn', 'inference_geo': 'us'}
  • Sent on Messages requests (streamed or not) and Message Batches through AsyncAnthropic clients, which also covers Claude Platform on AWS and Claude on Google Cloud (Anthropic managed). Not sent to count_tokens, whose endpoint does not take it.
  • Ignored with a UserWarning on the Bedrock, Vertex AI and Foundry clients, where the docs say it does not apply, like anthropic_speed. The client gate is the profile flag, set in _instance_profile_defaults, so profile= overrides it.
  • No model gate: on Claude 4.5 and older the API's 400 comes through, so a US only request is never silently sent without it.
  • usage.inference_geo lands in provider_details['inference_geo'] only when the request carried the setting, so other Anthropic responses keep their current provider_details. Reporting it on every response would also price requests that rely on a workspace default of 'us' once genai-prices adds the variants (pydantic/genai-prices#429), at the cost of a new key on every Anthropic response. Happy to switch if you prefer that.
  • Not verified live: no Anthropic or Gateway key on hand, so the tests mock the HTTP transport with the wire shape taken from existing recordings. The Gateway's Anthropic route gets the field, but I have not probed that it forwards it.

Verification

What changes for existing users

Nothing unless they set the new setting.

Two asks for a maintainer: this PR needs the pkg:core label (adding it from my account fails with a permissions error, so the Package Label check is red until then); and the diff touches the core agent skill reference under pydantic_ai_slim/pydantic_ai/.agents/, which douwebot's agent config guard covers, so it needs douwebot:review-agent-config rather than douwebot.

PR decisions

2026-11-01 · Client gate as profile flag · iter -

  • Decision: anthropic_supports_inference_geo set in _instance_profile_defaults from client class; Bedrock, BedrockMantle, Vertex, Foundry ignore with UserWarning
  • Why: models/AGENTS.md requires client-class gates to become profile flags; docs list AWS platform and Anthropic-managed Google Cloud (AsyncAnthropic subclasses) as supported
  • Source: https://github.com/pydantic/pydantic-ai/issues/12090#issuecomment-6559178210
  • Session: 2fd36397-1c37-4eac-9cb6-c755935374d7

2026-11-01 · No model gate · iter -

2026-11-01 · provider_details only when requested · iter -

2026-11-01 · Not sent to count_tokens · iter -

2026-11-01 · Batches carry it · iter -

2026-11-01 · genai-prices: no follow-up needed · iter -

  • Decision: Don't file a follow-up; pricing gap is tracked in genai-prices #429. provider_details is already passed as price_context, so the 'inference_geo' key feeds future when:{inference_geo: us} variants
  • Why: genai-prices supports when-variants keyed on context (test fixtures use inference_geo) but anthropic.yml has only batch variants
  • Source: https://github.com/pydantic/genai-prices/issues/429
  • Session: 2fd36397-1c37-4eac-9cb6-c755935374d7

2026-11-01 · Open question: unconditional provider_details · iter -

  • Decision: Keep conditional surfacing as claimed, flag in PR as maintainer call
  • Why: Unconditional would price workspace-default-us requests right once genai-prices adds variants, and matches OpenAI service_tier surfacing, but changes provider_details on every Anthropic response
  • Source: https://github.com/pydantic/pydantic-ai/issues/12090#issuecomment-6559178210
  • Session: 2fd36397-1c37-4eac-9cb6-c755935374d7

2026-11-01 · Mock transport tests, no cassettes · iter -

  • Decision: Tests use httpx2 MockTransport; no live cassettes recorded
  • Why: No ANTHROPIC_API_KEY (env var empty) or gateway key on this machine; stream shape (geo only on message_start) confirmed from existing recorded cassettes
  • Source: https://github.com/pydantic/pydantic-ai/issues/12090
  • Session: 2fd36397-1c37-4eac-9cb6-c755935374d7

2026-11-01 · Gateway pass-through unverified · iter -

  • Decision: Gateway route gets the field (plain AsyncAnthropic client); not probed live
  • Why: No PYDANTIC_AI_GATEWAY_API_KEY available; models/AGENTS.md says probe the gateway leg
  • Source: https://github.com/pydantic/pydantic-ai/issues/12090
  • Session: 2fd36397-1c37-4eac-9cb6-c755935374d7

2026-11-01 · Profile flag without provider guard · iter -

  • Decision: anthropic_supports_inference_geo merged unconditionally in _instance_profile_defaults; user profile= still overrides
  • Why: No provider sets it, so a 'not in profile' guard would leave an uncovered branch
  • Source: https://github.com/pydantic/pydantic-ai/issues/12090
  • Session: 2fd36397-1c37-4eac-9cb6-c755935374d7

2026-11-01 · pre-push review 1 of 3 at f7e493e1ac6fef3ac087086f61cf5597a5d66655 · iter -

2026-11-01 · pre-push review 1 of 3 at f7e493e1ac6fef3ac087086f61cf5597a5d66655 · iter -

2026-11-01 · pre-push review 2 of 3 at 0748c8948974a0b4f2d3f67f4e684f3b6893dc64 · iter -

2026-11-01 · pre-push review 2 of 3 at 0748c89484 · iter -

  • Decision: remediated: added streamed ignored-client test (mutation-checked: fails 3/3 on the old key-presence gate); dismissed 'Default: False' docstring finding
  • Why: Sibling flags anthropic_supports_message_batches and anthropic_count_tokens_accepts_server_tools state the bare-profile default then explain what AnthropicModel sets; new head 0748c8948974a0b4f2d3f67f4e684f3b6893dc64 is test-only delta (exempt: missing coverage)
  • Source: https://github.com/rscholz98/pydantic-ai/tree/anthropic-inference-geo
  • Session: 2fd36397-1c37-4eac-9cb6-c755935374d7

2026-11-01 · exempt push at d0d51dcb1 · iter -

2026-11-07 · rebase onto main exempt from review at c42beddbf63fff02b588e10ba066b6247aa97f0d · iter -

  • Decision: No third pre-push review: rebase of d0d51dcb1 onto 2c942f77d with two positional conflicts (adjacent additions next to anthropic_calls_cited_web_tools_directly in profiles/anthropic.py and models/anthropic/init.py); resolved by keeping both sides
  • Why: Exemption outside the table: '+' and '-' lines of git diff 3b5dcd059..d0d51dcb1 and git diff upstream/main..HEAD are byte-identical (365 lines, 0 removed), so the branch contribution and every rubric rung are unchanged; targeted tests (69), ruff and pyright pass on the rebased tree
  • Source: https://github.com/rscholz98/pydantic-ai/tree/anthropic-inference-geo
  • Session: 116f8c9d-c578-47f3-ab33-4faaf613b222

2026-11-07 · pushed as merge commit cfb632f0d instead of rebase · iter -

2026-11-07 · pre-push review 3 of 3 at 44b2d665ab259ad720fad38b435357cf04600eb0 · iter -

2026-11-07 · pre-push review 3 of 3 at 44b2d665ab259ad720fad38b435357cf04600eb0 · iter -

  • Decision: remediated: helper renamed with_inference_geo -> report_inference_geo and returns a new dict instead of mutating its argument; docstring names the report flag as the gate; amended into 58053bad5f1aee34d7020d49bf0b115a09f602cd (unpushed commit)
  • Why: Finding valid: with_ prefix means builder in this codebase; budget exhausted, local checks (ruff, pyright, 69 tests, complexity vs main) pass; no fourth review
  • Source: https://github.com/pydantic/pydantic-ai/pull/12769
  • Session: 116f8c9d-c578-47f3-ab33-4faaf613b222

2026-11-07 · exempt push after review budget at b4babefe8d7edf2e3958cd1a2b8dbaff3eaf22e4 · iter -

  • Decision: Pushed without a review: CI NameError fix moves AnthropicModelSettings construction from the parametrize decorator into the test body (plain geo values in parametrize)
  • Why: Delta from reviewed 58053bad5 is tests/models/anthropic/test_inference_geo.py only (test-harness repair, exempt); slim and evals jobs collect the module without anthropic installed; 14 tests pass, ruff and pyright clean; budget 3 of 3 already used
  • Source: https://github.com/pydantic/pydantic-ai/actions/runs/42360278517
  • Session: 46c0c866-ca3c-46e5-8e87-0540d65d61ac

2026-11-09 · merge of main after #12833 at 78a40eb34 (local, unpushed) · iter -

  • Decision: Conflicts resolved by taking main's refactored structure and re-attaching the inference_geo calls: non-stream wraps response_provider_details(response) in report_inference_geo inside _process_response; stream calls report_inference_geo at the end of _map_message_start; _report_inference_geo field kept before the new init=False fields; report_input_transformations import dropped from init (main moved its use into _response.py)
  • Why: #12833 moved the provider_details block into response_provider_details and split the stream loop into per-event methods, so both report sites moved; branch contribution vs upstream/main is still 379 added lines in the same 8 files, 905 Anthropic tests pass with a placeholder key, ruff, pyright and the complexity gate pass
  • Source: https://github.com/pydantic/pydantic-ai/pull/12833
  • Session: d1970b0b-6e15-484d-9475-ade040ffc9f3

2026-11-09 · push after review budget at 78a40eb34 (merge of main with conflict resolution) · iter -

  • Decision: Pushed without a review: budget 3 of 3 already used; delta from reviewed 58053bad5 plus the exempt b4babefe8 is a merge of upstream/main with a conflict resolution, which is not an exempt delta, so it goes out under budget exhaustion with local checks
  • Why: Resolution only re-attaches the two report_inference_geo calls to the sites #12833 moved; ruff, pyright, complexity gate and 905 Anthropic tests pass on the merged tree; Richard asked for the push on 2026-11-09 after seeing the resolution
  • Source: https://github.com/pydantic/pydantic-ai/pull/12769
  • Session: d1970b0b-6e15-484d-9475-ade040ffc9f3

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
  • PR title is fit for the release changelog.