Skip to content

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

@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\n\n## Reproduction\n\n1. Start a Cloud Agent session that uses Anthropic Messages API routing.\n2. Complete a few interactions that include tool calls.\n3. Continue the conversation until the prior assistant response with tool_use blocks is re-included or duplicated in the message history.\n4. The next request to the gateway fails before reaching Anthropic successfully.\n\n## Expected behavior\n\nThe gateway should repair malformed message history before forwarding it upstream, matching the existing OpenAI-format repairTools() behavior.\n\n## Actual behavior\n\nDuplicate Anthropic-format tool_use content blocks are forwarded to Anthropic, which rejects the request with tool_use ids must be unique.\n\n## Root cause\n\napps/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.\n\n## Proposed fix\n\nAdd 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