Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
fix: remove sync api calls for byok (#8031)
cthomas6f1ead8ba939
diff --git a/letta/schemas/providers/anthropic.py b/letta/schemas/providers/anthropic.pyindex ca4d4b847..a6e267c1d 100644--- a/letta/schemas/providers/anthropic.py+++ b/letta/schemas/providers/anthropic.py@@ -109,18 +109,19 @@ class AnthropicProvider(Provider):async def check_api_key(self):api_key = await self.api_key_enc.get_plaintext_async() if self.api_key_enc else None- if api_key:- anthropic_client = anthropic.Anthropic(api_key=api_key)- try:- # just use a cheap model to count some tokens - as of 5/7/2025 this is faster than fetching the list of models- anthropic_client.messages.count_tokens(model=MODEL_LIST[-1]["name"], messages=[{"role": "user", "content": "a"}])- except anthropic.AuthenticationError as e:- raise LLMAuthenticationError(message=f"Failed to authenticate with Anthropic: {e}", code=ErrorCode.UNAUTHENTICATED)- except Exception as e:- raise LLMError(message=f"{e}", code=ErrorCode.INTERNAL_SERVER_ERROR)- else:+ if not api_key:raise ValueError("No API key provided")+ try:+ # Use async Anthropic client+ anthropic_client = anthropic.AsyncAnthropic(api_key=api_key)+ # just use a cheap model to count some tokens - as of 5/7/2025 this is faster than fetching the list of models+ await anthropic_client.messages.count_tokens(model=MODEL_LIST[-1]["name"], messages=[{"role": "user", "content": "a"}])+ except anthropic.AuthenticationError as e:+ raise LLMAuthenticationError(message=f"Failed to authenticate with Anthropic: {e}", code=ErrorCode.UNAUTHENTICATED)+ except Exception as e:+ raise LLMError(message=f"{e}", code=ErrorCode.INTERNAL_SERVER_ERROR)+def get_default_max_output_tokens(self, model_name: str) -> int:"""Get the default max output tokens for Anthropic models."""if "opus" in model_name:diff --git a/letta/schemas/providers/openai.py b/letta/schemas/providers/openai.pyindex c9ecf0a14..1bafb34f6 100644--- a/letta/schemas/providers/openai.py+++ b/letta/schemas/providers/openai.py@@ -1,8 +1,10 @@from typing import Literal+from openai import AsyncOpenAI, AuthenticationErrorfrom pydantic import Fieldfrom letta.constants import DEFAULT_EMBEDDING_CHUNK_SIZE, LLM_MAX_CONTEXT_WINDOW+from letta.errors import ErrorCode, LLMAuthenticationError, LLMErrorfrom letta.log import get_loggerfrom letta.schemas.embedding_config import EmbeddingConfigfrom letta.schemas.enums import ProviderCategory, ProviderType@@ -23,11 +25,21 @@ class OpenAIProvider(Provider):base_url: str = Field("https://api.openai.com/v1", description="Base URL for the OpenAI API.")async def check_api_key(self):- from letta.llm_api.openai import openai_check_valid_api_key # TODO: DO NOT USE THIS - old code path-# Decrypt API key before usingapi_key = await self.api_key_enc.get_plaintext_async() if self.api_key_enc else None- openai_check_valid_api_key(self.base_url, api_key)++ if not api_key:+ raise ValueError("No API key provided")++ try:+ # Use async OpenAI client to check API key validity+ client = AsyncOpenAI(api_key=api_key, base_url=self.base_url)+ # Just list models to verify API key works+ await client.models.list()+ except AuthenticationError as e:+ raise LLMAuthenticationError(message=f"Failed to authenticate with OpenAI: {e}", code=ErrorCode.UNAUTHENTICATED)+ except Exception as e:+ raise LLMError(message=f"{e}", code=ErrorCode.INTERNAL_SERVER_ERROR)def get_default_max_output_tokens(self, model_name: str) -> int:"""Get the default max output tokens for OpenAI models."""