Skip to content

Cloud Agent fails after repeated turns with duplicate tool_use IDs #4027

@kilo-code-bot

Description

@kilo-code-bot

Summary

Cloud Agent sessions in the Kilo Cloud session and Kilo Android app can fail after one or two interactions with an upstream Anthropic validation error like:

messages.N.content.M: 'tool_use' ids must be unique

Reproduction

  1. Start a Cloud Agent session that uses Anthropic Messages API routing.
  2. Complete a few interactions that include tool calls.
  3. Continue the conversation until the prior assistant response with tool_use blocks is re-included or duplicated in the message history.
  4. The next request to the gateway fails before reaching Anthropic successfully.

Expected behavior

The gateway should repair malformed message history before forwarding it upstream, matching the existing OpenAI-format repairTools() behavior.

Actual behavior

Duplicate Anthropic-format tool_use content blocks are forwarded to Anthropic, which rejects the request with tool_use ids must be unique.

Root cause

apps/web/src/lib/ai-gateway/tool-calling.ts already deduplicates duplicate tool_calls for kind: 'chat_completions' requests, but the Anthropic Messages API path (kind: 'messages') stores tool calls inline as content blocks. Those inline tool_use blocks were not deduplicated before provider transforms.

Proposed fix

Add repairMessagesTools() for the Anthropic Messages API format, run it for kind: 'messages' gateway requests, and add regression tests for duplicate tool_use blocks and orphan tool_result blocks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions