Skip to content

armgit / letta

Public Imported from GitHub
0

Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.

fix: catch context window exceeded error thrown from Anthropic (#1470)

Charles Packer081847a61b98
diff --git a/letta/llm_api/anthropic_client.py b/letta/llm_api/anthropic_client.py
index 16bb2e211..86a1f09f2 100644
--- a/letta/llm_api/anthropic_client.py
+++ b/letta/llm_api/anthropic_client.py
@@ -6,6 +6,7 @@ import anthropic
from anthropic.types import Message as AnthropicMessage
from letta.errors import (
+ ContextWindowExceededError,
ErrorCode,
LLMAuthenticationError,
LLMBadRequestError,
@@ -164,10 +165,17 @@ class AnthropicClient(LLMClientBase):
if isinstance(e, anthropic.BadRequestError):
logger.warning(f"[Anthropic] Bad request: {str(e)}")
- return LLMBadRequestError(
- message=f"Bad request to Anthropic: {str(e)}",
- code=ErrorCode.INTERNAL_SERVER_ERROR,
- )
+ if "prompt is too long" in str(e).lower():
+ # If the context window is too large, we expect to receive:
+ # 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'prompt is too long: 200758 tokens > 200000 maximum'}}
+ return ContextWindowExceededError(
+ message=f"Bad request to Anthropic (context window exceeded): {str(e)}",
+ )
+ else:
+ return LLMBadRequestError(
+ message=f"Bad request to Anthropic: {str(e)}",
+ code=ErrorCode.INTERNAL_SERVER_ERROR,
+ )
if isinstance(e, anthropic.AuthenticationError):
logger.warning(f"[Anthropic] Authentication error: {str(e)}")