mirror of
https://github.com/jwadow/kiro-gateway.git
synced 2026-04-25 01:15:57 +03:00
[GH-ISSUE #73] Feature: Auto-truncate conversation history when payload exceeds Kiro API body size limit #51
Labels
No labels
bug
bug
enhancement
enhancement
fixed
fixed
invalid
needs-info
needs-testing
pull-request
question
upstream
wontfix
workaround
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/kiro-gateway-jwadow#51
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @kilhyeonjun on GitHub (Feb 9, 2026).
Original GitHub issue: https://github.com/jwadow/kiro-gateway/issues/73
Summary
When the conversation context grows large (e.g., 137k+ tokens with system prompt + tool definitions), the total HTTP payload sent to Kiro API can exceed ~1.2-1.3MB, causing a
400 Improperly formed requesterror.This is an upstream Kiro API limitation (no
max_tokensorinferenceConfigsupport), but the gateway could mitigate it by detecting oversized payloads before sending.Environment
Reproduction
400 Improperly formed request(noreasonfield)Verified payload size limit
Proposed Solution
Add a pre-flight check before sending to Kiro API:
MAX_PAYLOAD_SIZE_MB=1.2), automatically truncate oldest conversation history messages until under the limitThis would prevent cryptic 400 errors for long conversations and complement the existing
CONTENT_LENGTH_EXCEEDS_THRESHOLDhandling inkiro_errors.py.Workaround
Currently mitigated client-side by setting
contextWindow: 128000(instead of 200000) in the model config, so the client triggers compaction earlier.@bhaskoro-muthohar commented on GitHub (Feb 9, 2026):
Additional finding: exact payload size limit is ~615KB, not 1.2-1.3MB
I independently hit this same issue and binary-searched the exact boundary:
Methodology: Took a known-good Kiro payload (~302KB) and padded a history entry's content with dummy text in 256-byte increments. The limit is consistent and deterministic — fails 5/5 above threshold, succeeds 5/5 below.
The error response is
{"message":"Improperly formed request.","reason":null}— very misleading for a size limit.Context: Claude Code with 41 tools and 168 messages. The Kiro JSON payload was 627KB, just over the limit. The error appears "intermittent" because conversations grow over time and cross the threshold.
A safe truncation target would be ~580-590KB to leave headroom.
@bhaskoro-muthohar commented on GitHub (Feb 9, 2026):
@jwadow I'd like to make a case for why this should be handled by the gateway, not the client.
In issue #60, the design philosophy was stated as:
This ~615KB payload size limit is a Kiro shortcoming, not a client issue. The same payload works fine against the real Anthropic API, which accepts payloads well over 1MB. A client sending a valid Anthropic API request should not get a
400 Improperly formed requestfrom a gateway that claims Anthropic API compatibility.The gateway already handles several Kiro-specific quirks:
ensure_first_message_is_user()— Kiro requires user-first, Anthropic doesn'tensure_alternating_roles()— Kiro requires strict alternation, Anthropic is more flexiblesanitize_json_schema()— strips fields Kiro rejects but Anthropic acceptsstrip_all_tool_content()— handles Kiro's toolResults-without-tools rejectionPayload size truncation fits the same pattern: compensating for a Kiro limitation that doesn't exist in the Anthropic API.
Additionally, the error message
"Improperly formed request."withreason: nullgives the client zero information that this is a size issue. Without the gateway handling it, every client has to independently discover and work around this undocumented limit.@Hitesh-Sisara commented on GitHub (Feb 12, 2026):
hey @kilhyeonjun how can i set custom contextWindow for claude code
@sametakofficial commented on GitHub (Feb 12, 2026):
hey, ı used your payload size research and implemented fixes to this fork : github.com/sametakofficial/kiro-gateway
ı vibe coded it, but it works very well
@kilhyeonjun commented on GitHub (Feb 15, 2026):
@Hitesh-Sisara
Claude Code doesn't have a direct
contextWindowsetting as far as I know.One possible workaround is the
CLAUDE_AUTOCOMPACT_PCT_OVERRIDEenvironment variable, which may control when auto-compaction triggers:This would set compaction at ~64% context usage (128K / 200K), helping avoid the ~615KB payload limit. But I'm not 100% sure this is officially supported — you may need to verify on your end.
Alternatively, you can set it in
~/.claude/settings.json:If anyone has confirmed this works with Claude Code + kiro-gateway, please share!
@Chumbayoumba commented on GitHub (Feb 18, 2026):
dummy