{"record":{"id":"ed70d260e5ba588f","repo":"affaan-m/ECC","slug":"contextlengtherror-msg-provider-providertype-openai-from-e","errorCode":null,"errorMessage":"ContextLengthError(msg, provider=ProviderType.OPENAI) from e","messagePattern":"ContextLengthError\\(msg, provider=ProviderType\\.OPENAI\\) from e","errorType":"exception","errorClass":"ContextLengthError","httpStatus":null,"severity":"error","filePath":"src/llm/providers/openai.py","lineNumber":121,"sourceCode":"                    \"completion_tokens\": response.usage.completion_tokens,\n                    \"total_tokens\": response.usage.total_tokens,\n                }\n\n            return LLMOutput(\n                content=choice.message.content or \"\",\n                tool_calls=tool_calls,\n                model=response.model,\n                usage=usage,\n                stop_reason=choice.finish_reason,\n            )\n        except Exception as e:\n            msg = str(e)\n            if \"401\" in msg or \"authentication\" in msg.lower():\n                raise AuthenticationError(msg, provider=ProviderType.OPENAI) from e\n            if \"429\" in msg or \"rate_limit\" in msg.lower():\n                raise RateLimitError(msg, provider=ProviderType.OPENAI) from e\n            if \"context\" in msg.lower() and \"length\" in msg.lower():\n                raise ContextLengthError(msg, provider=ProviderType.OPENAI) from e\n            raise\n\n    def list_models(self) -> list[ModelInfo]:\n        return self._models.copy()\n\n    def validate_config(self) -> bool:\n        return bool(self.client.api_key)\n\n    def get_default_model(self) -> str:\n        return \"gpt-4o-mini\"\n","sourceCodeStart":103,"sourceCodeEnd":132,"githubUrl":"https://github.com/affaan-m/ECC/blob/8321021c54d670126ce3b2969d5deb880b4b0c2a/src/llm/providers/openai.py#L103-L132","documentation":"The OpenAI provider raises ContextLengthError when the API error message contains both 'context' and 'length', i.e. the request exceeded the model's maximum context length (prompt tokens + max_tokens). The original SDK exception is chained via 'from e'.","triggerScenarios":"Calling generate() where the tokenized prompt + max_tokens exceeds the model limit (e.g. 8191 for gpt-3.5-turbo, 128k for gpt-4o); error text like 'This model's maximum context length is ...'.","commonSituations":"Sending large documents/RAG dumps without truncation; long multi-turn chats without history trimming; requesting large max_tokens on top of a near-limit prompt; downgrading to a smaller-context model.","solutions":["Truncate or summarize the prompt/history to fit the model window","Lower input.max_tokens to leave room for the prompt","Switch to a larger-context model (e.g. gpt-4o 128k)","Count tokens before sending with tiktoken and reject oversized requests early"],"exampleFix":"// before\nmessages=[{\"role\":\"user\",\"content\":big_doc}]\nresp = provider.generate(LLMInput(messages=messages, max_tokens=4096))\n// after\nmessages=[{\"role\":\"user\",\"content\":big_doc[:60000]}]\nresp = provider.generate(LLMInput(messages=messages, max_tokens=1024))","handlingStrategy":"validation","validationCode":"import tiktoken\ndef fits(prompt: str, model: str, max_tokens_out: int) -> bool:\n    enc = tiktoken.encoding_for_model(model)\n    return len(enc.encode(prompt)) + max_tokens_out < MODEL_LIMITS[model]","typeGuard":null,"tryCatchPattern":"try:\n    resp = provider.generate(inp)\nexcept ContextLengthError:\n    inp = replace(inp, prompt=summarize(inp.prompt))\n    resp = provider.generate(inp)","preventionTips":["Count tokens with tiktoken before sending","Trim history to the last N turns","Bound max_tokens relative to prompt size","Map each model to its exact context limit in config"],"tags":["openai","context-window","tokens"],"backgroundTag":"context-length-exceeded","analyzedSha":"8321021c54d670126ce3b2969d5deb880b4b0c2a","analyzedAt":"2026-09-16T10:08:13.343Z","contentChangedAt":"2026-09-16T10:08:13.343Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}