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.

feat: enable claude pro max plan for letta code (#7663)

jnjpnga1fb295e6922
diff --git a/letta/llm_api/anthropic_client.py b/letta/llm_api/anthropic_client.py
index 25bf2ddd0..8c6c93049 100644
--- a/letta/llm_api/anthropic_client.py
+++ b/letta/llm_api/anthropic_client.py
@@ -94,7 +94,6 @@ class AnthropicClient(LLMClientBase):
@trace_method
async def request_async(self, request_data: dict, llm_config: LLMConfig) -> dict:
client = await self._get_anthropic_client_async(llm_config, async_client=True)
-
betas: list[str] = []
# interleaved thinking for reasoner
if llm_config.enable_reasoner:
@@ -234,17 +233,35 @@ class AnthropicClient(LLMClientBase):
) -> Union[anthropic.AsyncAnthropic, anthropic.Anthropic]:
api_key, _, _ = self.get_byok_overrides(llm_config)
+ # For claude-pro-max provider, use OAuth Bearer token instead of api_key
+ is_oauth_provider = llm_config.provider_name == "claude-pro-max"
+
if async_client:
- return (
- anthropic.AsyncAnthropic(api_key=api_key, max_retries=model_settings.anthropic_max_retries)
- if api_key
- else anthropic.AsyncAnthropic(max_retries=model_settings.anthropic_max_retries)
- )
- return (
- anthropic.Anthropic(api_key=api_key, max_retries=model_settings.anthropic_max_retries)
- if api_key
- else anthropic.Anthropic(max_retries=model_settings.anthropic_max_retries)
- )
+ if api_key:
+ if is_oauth_provider:
+ return anthropic.AsyncAnthropic(
+ max_retries=model_settings.anthropic_max_retries,
+ default_headers={
+ "Authorization": f"Bearer {api_key}",
+ "anthropic-version": "2023-06-01",
+ "anthropic-beta": "oauth-2025-04-20",
+ },
+ )
+ return anthropic.AsyncAnthropic(api_key=api_key, max_retries=model_settings.anthropic_max_retries)
+ return anthropic.AsyncAnthropic(max_retries=model_settings.anthropic_max_retries)
+
+ if api_key:
+ if is_oauth_provider:
+ return anthropic.Anthropic(
+ max_retries=model_settings.anthropic_max_retries,
+ default_headers={
+ "Authorization": f"Bearer {api_key}",
+ "anthropic-version": "2023-06-01",
+ "anthropic-beta": "oauth-2025-04-20",
+ },
+ )
+ return anthropic.Anthropic(api_key=api_key, max_retries=model_settings.anthropic_max_retries)
+ return anthropic.Anthropic(max_retries=model_settings.anthropic_max_retries)
@trace_method
async def _get_anthropic_client_async(
@@ -252,17 +269,35 @@ class AnthropicClient(LLMClientBase):
) -> Union[anthropic.AsyncAnthropic, anthropic.Anthropic]:
api_key, _, _ = await self.get_byok_overrides_async(llm_config)
+ # For claude-pro-max provider, use OAuth Bearer token instead of api_key
+ is_oauth_provider = llm_config.provider_name == "claude-pro-max"
+
if async_client:
- return (
- anthropic.AsyncAnthropic(api_key=api_key, max_retries=model_settings.anthropic_max_retries)
- if api_key
- else anthropic.AsyncAnthropic(max_retries=model_settings.anthropic_max_retries)
- )
- return (
- anthropic.Anthropic(api_key=api_key, max_retries=model_settings.anthropic_max_retries)
- if api_key
- else anthropic.Anthropic(max_retries=model_settings.anthropic_max_retries)
- )
+ if api_key:
+ if is_oauth_provider:
+ return anthropic.AsyncAnthropic(
+ max_retries=model_settings.anthropic_max_retries,
+ default_headers={
+ "Authorization": f"Bearer {api_key}",
+ "anthropic-version": "2023-06-01",
+ "anthropic-beta": "oauth-2025-04-20",
+ },
+ )
+ return anthropic.AsyncAnthropic(api_key=api_key, max_retries=model_settings.anthropic_max_retries)
+ return anthropic.AsyncAnthropic(max_retries=model_settings.anthropic_max_retries)
+
+ if api_key:
+ if is_oauth_provider:
+ return anthropic.Anthropic(
+ max_retries=model_settings.anthropic_max_retries,
+ default_headers={
+ "Authorization": f"Bearer {api_key}",
+ "anthropic-version": "2023-06-01",
+ "anthropic-beta": "oauth-2025-04-20",
+ },
+ )
+ return anthropic.Anthropic(api_key=api_key, max_retries=model_settings.anthropic_max_retries)
+ return anthropic.Anthropic(max_retries=model_settings.anthropic_max_retries)
@trace_method
def build_request_data(
@@ -522,7 +557,6 @@ class AnthropicClient(LLMClientBase):
async def count_tokens(self, messages: List[dict] = None, model: str = None, tools: List[OpenAITool] = None) -> int:
logging.getLogger("httpx").setLevel(logging.WARNING)
-
# Use the default client; token counting is lightweight and does not require BYOK overrides
client = anthropic.AsyncAnthropic()
if messages and len(messages) == 0:
diff --git a/letta/schemas/providers/anthropic.py b/letta/schemas/providers/anthropic.py
index 632e3b042..ca4d4b847 100644
--- a/letta/schemas/providers/anthropic.py
+++ b/letta/schemas/providers/anthropic.py
@@ -138,8 +138,21 @@ class AnthropicProvider(Provider):
NOTE: currently there is no GET /models, so we need to hardcode
"""
api_key = await self.api_key_enc.get_plaintext_async() if self.api_key_enc else None
+
+ # For claude-pro-max provider, use OAuth Bearer token instead of api_key
+ is_oauth_provider = self.name == "claude-pro-max"
+
if api_key:
- anthropic_client = anthropic.AsyncAnthropic(api_key=api_key)
+ if is_oauth_provider:
+ anthropic_client = anthropic.AsyncAnthropic(
+ default_headers={
+ "Authorization": f"Bearer {api_key}",
+ "anthropic-version": "2023-06-01",
+ "anthropic-beta": "oauth-2025-04-20",
+ },
+ )
+ else:
+ anthropic_client = anthropic.AsyncAnthropic(api_key=api_key)
elif model_settings.anthropic_api_key:
anthropic_client = anthropic.AsyncAnthropic()
else:
diff --git a/letta/services/provider_manager.py b/letta/services/provider_manager.py
index 26384c4bc..aa0af1cdd 100644
--- a/letta/services/provider_manager.py
+++ b/letta/services/provider_manager.py
@@ -1,6 +1,9 @@
from typing import List, Optional, Tuple, Union
+from sqlalchemy import and_, select
+
from letta.log import get_logger
+from letta.orm.errors import UniqueConstraintViolationError
from letta.orm.provider import Provider as ProviderModel
from letta.orm.provider_model import ProviderModel as ProviderModelORM
from letta.otel.tracing import trace_method
@@ -30,8 +33,6 @@ class ProviderManager:
is_byok: If True, creates a BYOK provider (default). If False, creates a base provider.
"""
async with db_registry.async_session() as session:
- from letta.schemas.enums import ProviderCategory
-
# Check for name conflicts
if is_byok:
# BYOK providers cannot use the same name as base providers
@@ -57,6 +58,52 @@ class ProviderManager:
if existing_base_providers:
raise ValueError(f"Base provider name '{request.name}' already exists. Please choose a different name.")
+ # Check if there's a soft-deleted provider with the same name that we can restore
+ org_id = actor.organization_id if is_byok else None
+ if org_id is not None:
+ stmt = select(ProviderModel).where(
+ and_(
+ ProviderModel.name == request.name,
+ ProviderModel.organization_id == org_id,
+ ProviderModel.is_deleted == True,
+ )
+ )
+ else:
+ stmt = select(ProviderModel).where(
+ and_(
+ ProviderModel.name == request.name,
+ ProviderModel.organization_id.is_(None),
+ ProviderModel.is_deleted == True,
+ )
+ )
+ result = await session.execute(stmt)
+ deleted_provider = result.scalar_one_or_none()
+
+ if deleted_provider:
+ # Restore the soft-deleted provider and update its fields
+ logger.info(f"Restoring soft-deleted provider '{request.name}' with id: {deleted_provider.id}")
+ deleted_provider.is_deleted = False
+ deleted_provider.provider_type = request.provider_type
+ deleted_provider.provider_category = ProviderCategory.byok if is_byok else ProviderCategory.base
+ deleted_provider.base_url = request.base_url
+ deleted_provider.region = request.region
+ deleted_provider.api_version = request.api_version
+
+ # Update encrypted fields
+ if request.api_key is not None:
+ deleted_provider.api_key_enc = Secret.from_plaintext(request.api_key).get_encrypted()
+ if request.access_key is not None:
+ deleted_provider.access_key_enc = Secret.from_plaintext(request.access_key).get_encrypted()
+
+ await deleted_provider.update_async(session, actor=actor)
+ provider_pydantic = deleted_provider.to_pydantic()
+
+ # For BYOK providers, automatically sync available models
+ if is_byok:
+ await self._sync_default_models_for_provider(provider_pydantic, actor)
+
+ return provider_pydantic
+
# Create provider with the appropriate category
provider_data = request.model_dump()