from pydantic import SecretStr
from openhands.sdk import Agent, Conversation, LLM
from openhands.sdk.llm.exceptions import (
LLMError,
LLMAuthenticationError,
LLMRateLimitError,
LLMTimeoutError,
LLMServiceUnavailableError,
LLMBadRequestError,
LLMContextWindowExceedError,
)
llm = LLM(model="claude-sonnet-4-20250514", api_key=SecretStr("your-key"))
agent = Agent(llm=llm, tools=[])
conversation = Conversation(agent=agent, persistence_dir="./.conversations", workspace=".")
try:
conversation.send_message("Continue the long analysis we started earlier…")
conversation.run()
except LLMContextWindowExceedError:
# Conversation is longer than the model’s context window
# Options:
# 1) Enable a condenser (recommended for long sessions)
# 2) Shorten inputs or reset conversation
print("Hit the context limit. Consider enabling a condenser.")
except LLMAuthenticationError:
print("Invalid or missing API credentials. Check your API key or auth setup.")
except LLMRateLimitError:
print("Rate limit exceeded. Back off and retry later.")
except LLMTimeoutError:
print("Request timed out. Consider increasing timeout or retrying.")
except LLMServiceUnavailableError:
print("Service unavailable or connectivity issue. Retry with backoff.")
except LLMBadRequestError:
print("Bad request to provider. Validate inputs and arguments.")
except LLMError as e:
# Fallback for other SDK LLM errors (parsing/validation, etc.)
print(f"Unhandled LLM error: {e}")