Skip to content

fix(bedrock): route Kimi/Moonshot models via bedrock-mantle OpenAI-compatible endpoint#13803

Closed
gopinaath wants to merge 4 commits into
anomalyco:devfrom
gopinaath:fix/bedrock-kimi-tool-calling
Closed

fix(bedrock): route Kimi/Moonshot models via bedrock-mantle OpenAI-compatible endpoint#13803
gopinaath wants to merge 4 commits into
anomalyco:devfrom
gopinaath:fix/bedrock-kimi-tool-calling

Conversation

@gopinaath

@gopinaath gopinaath commented Feb 16, 2026

Copy link
Copy Markdown

Fixes #13807

Summary

  • Route Moonshot AI models (Kimi K2.5, Kimi K2 Thinking) through Bedrock's OpenAI-compatible endpoint (bedrock-mantle.<region>.api.aws) instead of the Converse API
  • Fixes tool call parsing failures that cause premature end_turn in multi-step agentic workflows

Problem

Kimi K2.5 on Bedrock via the Converse API exhibits premature stopping behavior — the model makes 2-3 tool calls then issues end_turn, requiring multiple opencode run invocations for tasks that should complete in one session. The same model via OpenAI-compatible endpoints chains 14+ tool calls autonomously.

Root cause: Bedrock's Converse API has a known tool call parsing bug for Kimi/Moonshot models (vercel/ai#11409) where internal model tokens leak into text output instead of being parsed as tool calls. Additionally, Moonshot models are not listed in AWS's official Converse API tool calling support table.

Solution

When a Bedrock model ID contains "kimi" or "moonshot", route through bedrock-mantle.<region>.api.aws/v1 using @ai-sdk/openai-compatible instead of the Converse API. This:

  • Bypasses the lossy Converse API translation layer
  • Sends requests in OpenAI format (which Kimi was trained on)
  • Keeps authentication and billing on Bedrock infrastructure
  • Uses the existing bearer token authentication

Validation

Multi-turn tool calling tests with 5 tools and a 4-step task:

Route Steps completed Tool calls Premature end_turn?
Converse API 2-3 then stops 2-3 per session Yes
bedrock-mantle (this PR) All steps 8+ in one session No

Tested with both curl (15 rounds, never stopped) and AI SDK generateText (6 steps, 8 tool calls, task fully completed).

Test plan

  • Run existing Bedrock provider tests
  • Test amazon-bedrock/moonshotai.kimi-k2.5 with multi-step tool calling task
  • Verify non-Moonshot Bedrock models (Claude, Nova) still use Converse API
  • Test with both bearer token and IAM credential authentication

…mpatible endpoint

Kimi K2.5 on Bedrock via the Converse API has known tool call parsing
bugs (vercel/ai#11409) where tool call markers leak into text output,
causing premature end_turn and lost tool calls in multi-step agentic
workflows. The same model works correctly through OpenAI-compatible
endpoints.

Route Moonshot AI models through Bedrock's OpenAI-compatible endpoint
(bedrock-mantle.<region>.api.aws) instead of the Converse API. This
bypasses the lossy Converse API translation layer entirely while
keeping authentication and billing on Bedrock infrastructure.

Validated with multi-turn tool calling tests:
- Converse API: 2-3 tool calls then premature end_turn
- bedrock-mantle: 15+ tool calls, no premature stopping

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

gopinaath and others added 3 commits February 15, 2026 23:04
When no bearer token is available, use aws4fetch to sign requests to
the bedrock-mantle endpoint with IAM credentials from the existing
credential provider chain (env vars, CLI profiles, IAM roles, SSO).

Bearer token still takes precedence when AWS_BEARER_TOKEN_BEDROCK is
set. The SigV4 path reuses the same pattern as @ai-sdk/amazon-bedrock
and the same credentialProvider already configured by OpenCode.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The bedrock-mantle endpoint requires credentials scoped to the
'bedrock-mantle' service, not 'bedrock'. Verified with live AWS
endpoint which returns explicit error: "Credential should be scoped
to correct service: 'bedrock-mantle'."

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Without this flag, the OpenAI-compatible SDK omits
stream_options: {include_usage: true} from streaming requests,
causing bedrock-mantle to never return token usage data in the
stream. The auto-fix at getSDK doesn't help because Kimi's
api.npm is @ai-sdk/amazon-bedrock, not @ai-sdk/openai-compatible.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@rekram1-node

Copy link
Copy Markdown
Collaborator

If we wanted to go this route we'd do it in models.dev instead, thanks for trying to help tho

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Kimi K2.5 on Bedrock: premature end_turn due to Converse API tool call parsing bugs

2 participants