【Bug已解决】Why do I get an error saying ‘Input is too long for requested model‘ with my Claude API…

📅 2026/8/21 0:26:07
【Bug已解决】Why do I get an error saying ‘Input is too long for requested model‘ with my Claude API…
【Bug已解决】Why do I get an error saying Input is too long for requested model with my Claude API call on AWS Bedrock? 解决方案一、现象长什么样你在 AWS Bedrock 上用 Claude API 传了一段长输入收到Input is too long for requested model输入超出模型允许的最大长度或 Bedrock 侧的ValidationException: Input is too long for requested model你传的可能只是看起来不长的一段文档但加上 system prompt、历史对话、工具定义后总 token 超了同样的调用在上下文短时成功长文档就失败报错发生在请求被接受之前Bedrock 直接校验输入长度你不确定到底超了多少、卡在哪一段。一句话你发给 Bedrock 上 Claude 的总输入 token 数超过了该模型支持的最大上下文窗口max input tokens于是被直接拒绝。二、背景每个 Claude 模型都有固定的上下文窗口上限输入 输出。在 Bedrock 上调用时Bedrock 网关会在把请求转给模型前先校验总输入长度输入长度 system prompt 全部 messages含历史 tool 定义 文档内容注意是全部加起来不是单条消息不同模型上限不同如部分模型 200K部分更长一旦超出Bedrock 返回ValidationException: Input is too long for requested model请求根本到不了模型推理。常见误区用户以为我只发了一段 30K 字的文档没问题却忘了 system 历史 tools 已经占了大头叠加后超窗。三、根因根因是总输入 token 超过模型上下文上限构建请求 system (5K) history (20K) tools (10K) document (180K) 215K 模型 max_input (如 200K) - Bedrock 校验失败 - Input is too long这是硬性上限不是 bug。解决方向只有几条减少输入、拆分、或换更大窗口的模型。四、最小可运行复现下面用 Python 模拟总输入超窗被拒from dataclasses import dataclass dataclass class _BedrockClaude: max_input_tokens: int 200_000 def invoke(self, system: int, history: int, tools: int, document: int) - str: total system history tools document if total self.max_input_tokens: raise ValueError( fInput is too long for requested model: ftotal{total} max{self.max_input_tokens} ) return ok def main(): model _BedrockClaude() try: model.invoke(system5_000, history20_000, tools10_000, document180_000) except ValueError as e: print(ERR:, e) if __name__ __main__: main()运行后叠加超窗即抛Input is too long与真实 Bedrock 校验一致。五、解决方案第一层最小直接修复最小修复是先量再砍估算总 token把输入压到窗口内。import os from anthropic import AnthropicBedrock def count_approx(text: str) - int: return len(text) // 4 # 粗略估算英文约 4 字符/token def trim_to_budget(messages, budget: int) - list: # 从最早的消息开始删直到总输入 budget out list(messages) while sum(count_approx(str(m.content)) for m in out) budget and len(out) 1: out.pop(0) return out client AnthropicBedrock( project_idos.environ[AWS_PROJECT], regionos.environ[AWS_REGION] ) # 先估算再决定要不要截断历史 messages trim_to_budget(messages, budget180_000) resp client.messages.create( modelanthropic.claude-3-5-sonnet-20241022-v2:0, max_tokens1024, messagesmessages, )更彻底对超长文档做分块检索RAG只把相关片段送进上下文而非整篇。六、解决方案第二层结构化改进把输入预算治理抽成策略作为唯一事实来源from dataclasses import dataclass, field from typing import List dataclass(frozenTrue) class ClaudeBedrockTooLongPolicy: Bedrock 输入长度策略按模型窗口预算裁剪输入。 规则 - 已知模型 max_input_tokens - 总输入 system history tools document - 超出则从最早 history 删起保留 system/最新上下文 max_input_tokens: int 200_000 def budget(self, *, system: int, tools: int, reserve: int 4_000) - int: # 留给生成 system tools 的固定开销 return self.max_input_tokens - system - tools - reserve def fit(self, history_tokens: List[int], budget: int) - List[int]: kept, used [], 0 for t in reversed(history_tokens): # 从最新保留 if used t budget: kept.insert(0, t) used t else: break return kept def demo() - None: policy ClaudeBedrockTooLongPolicy(max_input_tokens200_000) b policy.budget(system5_000, tools10_000) kept policy.fit([20_000] * 10, b) print(保留历史条数:, len(kept)) if __name__ __main__: demo()对超长文档建议结合向量检索只取 top-k 片段从根上避免超窗。七、解决方案第三层断言 / CI 守护import pytest from your_module import ClaudeBedrockTooLongPolicy def test_budget_subtracts_overhead(): policy ClaudeBedrockTooLongPolicy(max_input_tokens200_000) b policy.budget(system5_000, tools10_000, reserve4_000) assert b 200_000 - 5_000 - 10_000 - 4_000 def test_fit_keeps_newest(): policy ClaudeBedrockTooLongPolicy(max_input_tokens200_000) b policy.budget(system0, tools0, reserve0) kept policy.fit([10_000] * 5, b) # 最多留 20 条 assert len(kept) 20 def test_fit_respects_budget(): policy ClaudeBedrockTooLongPolicy(max_input_tokens50_000) kept policy.fit([10_000] * 10, 25_000) assert sum(kept) 25_000 def test_overlong_raises(): model ClaudeBedrockTooLongPolicy(max_input_tokens10) with pytest.raises(ValueError): model # 占位真实场景由 Bedrock 网关抛CI 里加一条对任意请求构造先跑fit估算断言总输入 ≤ max_input_tokens避免线上Input is too long。八、排查清单你算过总输入吗system history tools 文档全加起来。报错是 Bedrock 网关校验不是模型推理错——请求没到模型。是否从最早的历史消息开始裁剪保留 system 与最新上下文。超长文档是否用了 RAG/分块而非整篇塞入是否考虑换更大窗口的模型是否用count_approx在发送前预估避免试错式超窗九、小结Bedrock 上 Claude 报Input is too long for requested model根因是 system 历史 工具 文档的总输入 token 超过了该模型的上下文窗口上限Bedrock 在网关层直接校验拒绝。这不是 bug是硬限制。最小修复是先估算总 token、从最早历史裁起、对长文档用 RAG 分块结构化做法是抽成ClaudeBedrockTooLongPolicy按模型窗口预算治理输入最后用 pytest 守护发送前总输入 ≤ max_input_tokens避免线上被拒。