Repository navigation
feat(core): add Amazon Bedrock provider with AWS credential chain auth - #4895
Conversation
🦋 Changeset detectedLatest commit: 41611f6 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
commit: |
|
❌ Nix build failed Hash mismatch in
Please update |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6d23ef3d0b
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| } | ||
| return { apiKey: cached.token }; | ||
| }, | ||
| canRecover: (error) => errorStatusCode(error) === 401, |
There was a problem hiding this comment.
Handle Bedrock's 403 token-expiry response
When the ambient AWS credentials are temporary, such as SSO, an assumed role, or an instance role, AWS limits the generated key to the underlying session lifetime even though this provider caches it for 11 hours 45 minutes. After that shorter lifetime expires, Bedrock returns ExpiredTokenException with HTTP 403, not 401 (AWS Common Errors); this predicate therefore prevents credentialRecovery from invalidating the cached token, and every request continues failing until the local TTL elapses or the process restarts. Recognize the expired-token 403 specifically, or derive the cache lifetime from the source credential expiration.
Useful? React with 👍 / 👎.
| const env = { ...process.env, ...provider.env }; | ||
| const baseUrl = resolveBedrockBaseUrl(protocol, env); |
There was a problem hiding this comment.
Use the same region for the endpoint and token
When [providers.<id>.env] sets only AWS_DEFAULT_REGION while the process already has AWS_REGION, auth resolution selects the configured default region for token generation, but this per-key merge retains the shell's AWS_REGION; bedrockRegionOf then prefers that value when constructing the endpoint. The resulting token and endpoint belong to different regions, and Bedrock API keys are region-scoped (AWS API keys), so every request fails with a credential-scope error. Resolve the effective region once with the provider table taking precedence over both shell region variables, and use it for both token generation and endpoint construction.
Useful? React with 👍 / 👎.
|
|
||
| export type BedrockWireProtocol = 'anthropic' | 'openai' | 'openai_responses'; | ||
|
|
||
| const GEO_INFERENCE_PROFILE_PREFIX_RE = /^(?:[a-z]{2}|apac|global)$/; |
There was a problem hiding this comment.
Recognize GovCloud inference-profile prefixes
For Bedrock models addressed through a GovCloud inference profile such as us-gov.openai..., this expression does not recognize the us-gov prefix, so it is never stripped and protocol inference falls through to the default Chat Completions route. AWS documents us-gov. profile IDs for OpenAI GPT models on the Responses API (Bedrock Responses API); these models are consequently sent to the wrong API and fail unless every alias manually supplies a protocol override. Include us-gov among the recognized geographic prefixes.
Useful? React with 👍 / 👎.
6d23ef3 to
41611f6
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 41611f6bb9
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| 'openai', | ||
| 'openai_responses', | ||
| 'anthropic', | ||
| 'bedrock', |
There was a problem hiding this comment.
Allow REST clients to supply the Bedrock region
When desktop or web clients create a bedrock provider through POST /api/v1/providers, this enum accepts the type, but the create/replace request schemas expose no provider env, region, or profile field, and the route persists only the type, key, and base URL. Unless the server process already has AWS_REGION, these clients cannot configure the documented ambient AWS credential flow, and model construction fails when resolveEndpointBaseUrl cannot derive a regional endpoint; named-profile SSO is similarly unavailable unless AWS_PROFILE was set before launching the server. Add REST fields for the Bedrock region/profile and persist them into the provider environment.
AGENTS.md reference: AGENTS.md:L123-L125
Useful? React with 👍 / 👎.
| alias.displayName = entry.display_name !== undefined ? entry.display_name : undefined; | ||
| alias.capabilities = | ||
| entry.capabilities !== undefined ? [...entry.capabilities] : undefined; | ||
| alias.protocol = entry.protocol; |
There was a problem hiding this comment.
Preserve protocol overrides across provider replacement
When a Bedrock model needs an explicit protocol override, such as the documented Kimi Responses configuration, neither modelCatalogItemSchema nor the provider response exposes that protocol to desktop/web clients. A client therefore cannot round-trip it when editing the provider, and this assignment clears the existing alias.protocol whenever the replacement payload omits the unknowable field, silently rerouting the model to the inferred API. Expose protocol in the model read contract or preserve the previous value when it is omitted.
AGENTS.md reference: AGENTS.md:L123-L125
Useful? React with 👍 / 👎.
| case 'openai': | ||
| return getOpenAILegacyModelCapability(vendorModel); | ||
| case 'openai_responses': | ||
| return getOpenAIResponsesModelCapability(vendorModel); |
There was a problem hiding this comment.
Add Bedrock capability metadata for Kimi models
For the documented global.moonshotai.kimi-k3 configuration, vendorModel becomes kimi-k3, but getOpenAILegacyModelCapability recognizes only OpenAI model-name families and returns undefined. The catalog consequently records image_in and thinking as false; image attachments are replaced as unsupported and the thinking controls are disabled even though AWS documents Kimi K3 as supporting vision, configurable reasoning, and client-side tools. Add Bedrock-specific capability/profile metadata for this advertised family rather than delegating it to the generic OpenAI name table. AWS Kimi K3 model card
Useful? React with 👍 / 👎.
Requirement or Bug
Resolve #3965. Users who authenticate to AWS through the standard credential chain (SSO, shared config, instance roles) should be able to use Amazon Bedrock models without configuring a long-lived Bedrock API key.
This PR adds a
bedrockprovider that generates short-lived bearer tokens from the ambient AWS credential chain. It also isolates Bedrock's Anthropic client construction inside the Bedrock provider instead of adding Bedrock-specific bearer detection to the shared Anthropic protocol.Bug Reproduction Steps
N/A (feature PR)
Root Cause
N/A (feature PR)
Code Changes
Core idea. When no
api_key,api_key_env, orAWS_BEARER_TOKEN_BEDROCKis configured, the CLI uses@aws/bedrock-token-generatorand the ambient AWS credential chain to mint a short-lived bearer token. The token is cached, refreshed before expiry, and regenerated after a 401. Explicit credentials retain their existing precedence.Key decisions
bedrockAnthropicProtocolderives fromanthropicProtocol.with({ client }). Only the Bedrock requester maps the credential to the Anthropic SDK'sauthToken; the shared Anthropic client keeps its existingx-api-keyand custom-header behavior. There is noBearer == apiKeyheuristic.{ apiKey }credential shape. No parallel Bedrock credential header convention is introduced.anthropic.*uses Anthropic Messages,openai.gpt-oss*uses Chat Completions, otheropenai.*uses Responses, andmoonshotai.*/moonshot.*uses Chat Completions. Unknown vendors default to Chat Completions, with an explicit per-modelprotocoloverride.AWS_REGION/AWS_DEFAULT_REGION. Missing explicit region configuration fails with a configuration error instead of allowing a credential to be sent to a protocol client's default public host.bedrock. REST model writes also accept the optionalprotocolfield. Existing single-protocol providers retain their endpoint-env fallback when a model overrides the protocol.Reading guide
packages/core/src/llm/builtin/provider/bedrock/credentials.ts— token caching, coalescing, expiry, 401 recovery, and credential-chain error propagation.packages/core/src/llm/builtin/provider/bedrock/routing.ts— protocol inference, inference-profile prefix handling, capabilities, and region endpoints.packages/core/src/llm/builtin/provider/bedrock/anthropic.ts— Bedrock-only Anthropic client and requester.packages/agent-core-v2/src/llm-adapter/model/model-auth.ts— auth-material, protocol, and endpoint resolution.packages/agent-core-v2/src/llm-adapter/provider/provider-definition.ts— the three Bedrock registrations.Behavior Changes and Affected Users
type = "bedrock"; no long-lived key is requiredprotocolon the model aliasAWS_REGIONorAWS_DEFAULT_REGIONbedrockin Node SDK, kap-server, and migrationprotocolfieldanthropic/openai/openai_responsesoverride is persistedprotocolto use inferenceconnectedwhen an ambient credential route is configuredhas_api_key: false+type: bedrockto distinguish it from stored-key providersapiKeymaps tox-api-key; independent custom Authorization headers are preservedAffected modules and coverage
Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.