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: Set reasonable defaults for max output tokens [LET-6483] (#7084)

Kevin Lin857139f907be
diff --git a/letta/constants.py b/letta/constants.py
index 0014e37d7..a52d12ffe 100644
--- a/letta/constants.py
+++ b/letta/constants.py
@@ -226,11 +226,14 @@ CORE_MEMORY_LINE_NUMBER_WARNING = "# NOTE: Line numbers shown below (with arrows
# Constants to do with summarization / conversation length window
# The max amount of tokens supported by the underlying model (eg 8k for gpt-4 and Mistral 7B)
-LLM_MAX_TOKENS = {
+LLM_MAX_CONTEXT_WINDOW = {
"DEFAULT": 30000,
# deepseek
"deepseek-chat": 64000,
"deepseek-reasoner": 64000,
+ # glm (Z.AI)
+ "glm-4.6": 200000,
+ "glm-4.5": 128000,
## OpenAI models: https://platform.openai.com/docs/models/overview
# gpt-5
"gpt-5": 272000,
diff --git a/letta/local_llm/koboldcpp/settings.py b/letta/local_llm/koboldcpp/settings.py
index 51f495653..221a74e6b 100644
--- a/letta/local_llm/koboldcpp/settings.py
+++ b/letta/local_llm/koboldcpp/settings.py
@@ -18,6 +18,6 @@ SIMPLE = {
# '\n#',
# '\n\n\n',
],
- # "max_context_length": LLM_MAX_TOKENS,
+ # "max_context_length": LLM_MAX_CONTEXT_WINDOW,
"max_length": 512,
}
diff --git a/letta/local_llm/lmstudio/settings.py b/letta/local_llm/lmstudio/settings.py
index c2ee66f99..2d5b9896e 100644
--- a/letta/local_llm/lmstudio/settings.py
+++ b/letta/local_llm/lmstudio/settings.py
@@ -20,7 +20,7 @@ SIMPLE = {
# This controls the maximum number of tokens that the model can generate
# Cap this at the model context length (assuming 8k for Mistral 7B)
# "max_tokens": 8000,
- # "max_tokens": LLM_MAX_TOKENS,
+ # "max_tokens": LLM_MAX_CONTEXT_WINDOW,
# This controls how LM studio handles context overflow
# In Letta we handle this ourselves, so this should be commented out
# "lmstudio": {"context_overflow_policy": 2},
diff --git a/letta/local_llm/ollama/settings.py b/letta/local_llm/ollama/settings.py
index eb68317ac..647013b41 100644
--- a/letta/local_llm/ollama/settings.py
+++ b/letta/local_llm/ollama/settings.py
@@ -20,7 +20,7 @@ SIMPLE = {
# '\n#',
# '\n\n\n',
],
- # "num_ctx": LLM_MAX_TOKENS,
+ # "num_ctx": LLM_MAX_CONTEXT_WINDOW,
},
"stream": False,
# turn off Ollama's own prompt formatting
diff --git a/letta/local_llm/webui/legacy_settings.py b/letta/local_llm/webui/legacy_settings.py
index d2f09903a..29163446a 100644
--- a/letta/local_llm/webui/legacy_settings.py
+++ b/letta/local_llm/webui/legacy_settings.py
@@ -19,5 +19,5 @@ SIMPLE = {
],
"max_new_tokens": 3072,
# "truncation_length": 4096, # assuming llama2 models
- # "truncation_length": LLM_MAX_TOKENS, # assuming mistral 7b
+ # "truncation_length": LLM_MAX_CONTEXT_WINDOW, # assuming mistral 7b
}
diff --git a/letta/local_llm/webui/settings.py b/letta/local_llm/webui/settings.py
index 27da3e748..1d4bb9542 100644
--- a/letta/local_llm/webui/settings.py
+++ b/letta/local_llm/webui/settings.py
@@ -20,5 +20,5 @@ SIMPLE = {
],
# "max_tokens": 3072,
# "truncation_length": 4096, # assuming llama2 models
- # "truncation_length": LLM_MAX_TOKENS, # assuming mistral 7b
+ # "truncation_length": LLM_MAX_CONTEXT_WINDOW, # assuming mistral 7b
}
diff --git a/letta/schemas/model.py b/letta/schemas/model.py
index c65a4fb8f..d2aa6a7ff 100644
--- a/letta/schemas/model.py
+++ b/letta/schemas/model.py
@@ -285,7 +285,7 @@ class AnthropicModelSettings(ModelSettings):
"temperature": self.temperature,
"max_tokens": self.max_output_tokens,
"extended_thinking": self.thinking.type == "enabled",
- "thinking_budget_tokens": self.thinking.budget_tokens,
+ "max_reasoning_tokens": self.thinking.budget_tokens,
"verbosity": self.verbosity,
"parallel_tool_calls": self.parallel_tool_calls,
"effort": self.effort,
diff --git a/letta/schemas/providers/anthropic.py b/letta/schemas/providers/anthropic.py
index d137e2341..97fa2a1eb 100644
--- a/letta/schemas/providers/anthropic.py
+++ b/letta/schemas/providers/anthropic.py
@@ -121,6 +121,16 @@ class AnthropicProvider(Provider):
else:
raise ValueError("No API key provided")
+ 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:
+ return 16384
+ elif "sonnet" in model_name:
+ return 16384
+ elif "haiku" in model_name:
+ return 8192
+ return 8192 # default for anthropic
+
async def list_llm_models_async(self) -> list[LLMConfig]:
"""
https://docs.anthropic.com/claude/docs/models-overview
@@ -171,11 +181,7 @@ class AnthropicProvider(Provider):
except Exception:
pass
- max_tokens = 8192
- if "claude-3-opus" in model["id"]:
- max_tokens = 4096
- if "claude-3-haiku" in model["id"]:
- max_tokens = 4096
+ max_tokens = self.get_default_max_output_tokens(model["id"])
# TODO: set for 3-7 extended thinking mode
# NOTE: from 2025-02
diff --git a/letta/schemas/providers/azure.py b/letta/schemas/providers/azure.py
index 19f85a909..49c31c5a7 100644
--- a/letta/schemas/providers/azure.py
+++ b/letta/schemas/providers/azure.py
@@ -5,7 +5,7 @@ import httpx
from openai import AsyncAzureOpenAI
from pydantic import Field, field_validator
-from letta.constants import DEFAULT_EMBEDDING_CHUNK_SIZE, LLM_MAX_TOKENS
+from letta.constants import DEFAULT_EMBEDDING_CHUNK_SIZE, LLM_MAX_CONTEXT_WINDOW
from letta.errors import ErrorCode, LLMAuthenticationError
from letta.schemas.embedding_config import EmbeddingConfig
from letta.schemas.enums import ProviderCategory, ProviderType
@@ -127,6 +127,7 @@ class AzureProvider(Provider):
model_endpoint=model_endpoint,
context_window=context_window_size,
handle=self.get_handle(model_name),
+ max_tokens=self.get_default_max_output_tokens(model_name),
provider_name=self.name,
provider_category=self.provider_category,
)
@@ -165,7 +166,7 @@ class AzureProvider(Provider):
def get_model_context_window(self, model_name: str) -> int | None:
# Hard coded as there are no API endpoints for this
- llm_default = LLM_MAX_TOKENS.get(model_name, 4096)
+ llm_default = LLM_MAX_CONTEXT_WINDOW.get(model_name, 4096)
return AZURE_MODEL_TO_CONTEXT_LENGTH.get(model_name, llm_default)
async def check_api_key(self):
diff --git a/letta/schemas/providers/base.py b/letta/schemas/providers/base.py
index b89fc1bbe..d6ca1df27 100644
--- a/letta/schemas/providers/base.py
+++ b/letta/schemas/providers/base.py
@@ -145,6 +145,19 @@ class Provider(ProviderBase):
async def get_model_context_window_async(self, model_name: str) -> int | None:
raise NotImplementedError
+ def get_default_max_output_tokens(self, model_name: str) -> int:
+ """
+ Get the default max output tokens for a model.
+ Override in subclasses for model-specific logic.
+
+ Args:
+ model_name (str): The name of the model.
+
+ Returns:
+ int: The default max output tokens for the model.
+ """
+ return 4096 # sensible fallback
+
def get_handle(self, model_name: str, is_embedding: bool = False, base_name: str | None = None) -> str:
"""
Get the handle for a model, with support for custom overrides.
diff --git a/letta/schemas/providers/bedrock.py b/letta/schemas/providers/bedrock.py
index ef809089b..b70bbb415 100644
--- a/letta/schemas/providers/bedrock.py
+++ b/letta/schemas/providers/bedrock.py
@@ -70,6 +70,7 @@ class BedrockProvider(Provider):
model_endpoint=None,
context_window=self.get_model_context_window(model_arn),
handle=self.get_handle(model_arn),
+ max_tokens=self.get_default_max_output_tokens(model_arn),
provider_name=self.name,
provider_category=self.provider_category,
)
diff --git a/letta/schemas/providers/cerebras.py b/letta/schemas/providers/cerebras.py
index 19470e5ee..78475e5e0 100644
--- a/letta/schemas/providers/cerebras.py
+++ b/letta/schemas/providers/cerebras.py
@@ -74,6 +74,7 @@ class CerebrasProvider(OpenAIProvider):
model_endpoint=self.base_url,
context_window=context_window_size,
handle=self.get_handle(model_name),
+ max_tokens=self.get_default_max_output_tokens(model_name),
put_inner_thoughts_in_kwargs=put_inner_thoughts_in_kwargs,
provider_name=self.name,
provider_category=self.provider_category,
diff --git a/letta/schemas/providers/deepseek.py b/letta/schemas/providers/deepseek.py
index a1ff7bb13..d8c7ff3f0 100644
--- a/letta/schemas/providers/deepseek.py
+++ b/letta/schemas/providers/deepseek.py
@@ -55,6 +55,7 @@ class DeepSeekProvider(OpenAIProvider):
model_endpoint=self.base_url,
context_window=context_window_size,
handle=self.get_handle(model_name),
+ max_tokens=self.get_default_max_output_tokens(model_name),
put_inner_thoughts_in_kwargs=put_inner_thoughts_in_kwargs,
provider_name=self.name,
provider_category=self.provider_category,
diff --git a/letta/schemas/providers/google_gemini.py b/letta/schemas/providers/google_gemini.py
index c1c135f71..b05927cf4 100644
--- a/letta/schemas/providers/google_gemini.py
+++ b/letta/schemas/providers/google_gemini.py
@@ -7,7 +7,7 @@ logger = get_logger(__name__)
from pydantic import Field
-from letta.constants import DEFAULT_EMBEDDING_CHUNK_SIZE, LLM_MAX_TOKENS
+from letta.constants import DEFAULT_EMBEDDING_CHUNK_SIZE, LLM_MAX_CONTEXT_WINDOW
from letta.schemas.embedding_config import EmbeddingConfig
from letta.schemas.enums import ProviderCategory, ProviderType
from letta.schemas.llm_config import LLMConfig
@@ -26,6 +26,12 @@ class GoogleAIProvider(Provider):
api_key = self.api_key_enc.get_plaintext() if self.api_key_enc else None
await google_ai_check_valid_api_key_async(api_key)
+ def get_default_max_output_tokens(self, model_name: str) -> int:
+ """Get the default max output tokens for Google Gemini models."""
+ if "2.5" in model_name or "2-5" in model_name: # gemini-2.5-* or gemini-2-5-*
+ return 65536
+ return 8192 # default for google gemini
+
async def list_llm_models_async(self):
from letta.llm_api.google_ai_client import google_ai_get_model_list_async
@@ -50,7 +56,7 @@ class GoogleAIProvider(Provider):
model_endpoint=self.base_url,
context_window=context_window,
handle=self.get_handle(model),
- max_tokens=8192,
+ max_tokens=self.get_default_max_output_tokens(model),
provider_name=self.name,
provider_category=self.provider_category,
)
@@ -95,8 +101,8 @@ class GoogleAIProvider(Provider):
logger.warning("This is deprecated, use get_model_context_window_async when possible.")
from letta.llm_api.google_ai_client import google_ai_get_model_context_window
- if model_name in LLM_MAX_TOKENS:
- return LLM_MAX_TOKENS[model_name]
+ if model_name in LLM_MAX_CONTEXT_WINDOW:
+ return LLM_MAX_CONTEXT_WINDOW[model_name]
else:
api_key = self.api_key_enc.get_plaintext() if self.api_key_enc else None
return google_ai_get_model_context_window(self.base_url, api_key, model_name)
@@ -104,8 +110,8 @@ class GoogleAIProvider(Provider):
async def get_model_context_window_async(self, model_name: str) -> int | None:
from letta.llm_api.google_ai_client import google_ai_get_model_context_window_async
- if model_name in LLM_MAX_TOKENS:
- return LLM_MAX_TOKENS[model_name]
+ if model_name in LLM_MAX_CONTEXT_WINDOW:
+ return LLM_MAX_CONTEXT_WINDOW[model_name]
else:
api_key = self.api_key_enc.get_plaintext() if self.api_key_enc else None
return await google_ai_get_model_context_window_async(self.base_url, api_key, model_name)
diff --git a/letta/schemas/providers/google_vertex.py b/letta/schemas/providers/google_vertex.py
index 0ed68541a..b8572e5d2 100644
--- a/letta/schemas/providers/google_vertex.py
+++ b/letta/schemas/providers/google_vertex.py
@@ -16,6 +16,12 @@ class GoogleVertexProvider(Provider):
google_cloud_project: str = Field(..., description="GCP project ID for the Google Vertex API.")
google_cloud_location: str = Field(..., description="GCP region for the Google Vertex API.")
+ def get_default_max_output_tokens(self, model_name: str) -> int:
+ """Get the default max output tokens for Google Vertex models."""
+ if "2.5" in model_name or "2-5" in model_name: # gemini-2.5-* or gemini-2-5-*
+ return 65536
+ return 8192 # default for google vertex
+
async def list_llm_models_async(self) -> list[LLMConfig]:
from letta.llm_api.google_constants import GOOGLE_MODEL_TO_CONTEXT_LENGTH
@@ -28,7 +34,7 @@ class GoogleVertexProvider(Provider):
model_endpoint=f"https://{self.google_cloud_location}-aiplatform.googleapis.com/v1/projects/{self.google_cloud_project}/locations/{self.google_cloud_location}",
context_window=context_length,
handle=self.get_handle(model),
- max_tokens=8192,
+ max_tokens=self.get_default_max_output_tokens(model),
provider_name=self.name,
provider_category=self.provider_category,
)
diff --git a/letta/schemas/providers/groq.py b/letta/schemas/providers/groq.py
index 23488c4b4..3e71a6a03 100644
--- a/letta/schemas/providers/groq.py
+++ b/letta/schemas/providers/groq.py
@@ -29,6 +29,7 @@ class GroqProvider(OpenAIProvider):
model_endpoint=self.base_url,
context_window=model["context_window"],
handle=self.get_handle(model["id"]),
+ max_tokens=self.get_default_max_output_tokens(model["id"]),
provider_name=self.name,
provider_category=self.provider_category,
)
diff --git a/letta/schemas/providers/letta.py b/letta/schemas/providers/letta.py
index 34151facb..69eb08759 100644
--- a/letta/schemas/providers/letta.py
+++ b/letta/schemas/providers/letta.py
@@ -21,6 +21,7 @@ class LettaProvider(Provider):
model_endpoint=LETTA_MODEL_ENDPOINT,
context_window=30000,
handle=self.get_handle("letta-free"),
+ max_tokens=self.get_default_max_output_tokens("letta-free"),
provider_name=self.name,
provider_category=self.provider_category,
)
diff --git a/letta/schemas/providers/lmstudio.py b/letta/schemas/providers/lmstudio.py
index 801adfb7a..c656f1881 100644
--- a/letta/schemas/providers/lmstudio.py
+++ b/letta/schemas/providers/lmstudio.py
@@ -61,6 +61,7 @@ class LMStudioOpenAIProvider(OpenAIProvider):
model_endpoint=self.model_endpoint_url,
context_window=context_window_size,
handle=self.get_handle(model_name),
+ max_tokens=self.get_default_max_output_tokens(model_name),
compatibility_type=compatibility_type,
provider_name=self.name,
provider_category=self.provider_category,
diff --git a/letta/schemas/providers/mistral.py b/letta/schemas/providers/mistral.py
index f174e3817..5950a0b83 100644
--- a/letta/schemas/providers/mistral.py
+++ b/letta/schemas/providers/mistral.py
@@ -34,6 +34,7 @@ class MistralProvider(Provider):
model_endpoint=self.base_url,
context_window=model["max_context_length"],
handle=self.get_handle(model["id"]),
+ max_tokens=self.get_default_max_output_tokens(model["id"]),
provider_name=self.name,
provider_category=self.provider_category,
)
diff --git a/letta/schemas/providers/ollama.py b/letta/schemas/providers/ollama.py
index b5e4801a1..356ed7b74 100644
--- a/letta/schemas/providers/ollama.py
+++ b/letta/schemas/providers/ollama.py
@@ -125,6 +125,7 @@ class OllamaProvider(OpenAIProvider):
# model_wrapper=self.default_prompt_formatter,
context_window=context_window,
handle=self.get_handle(model_name),
+ max_tokens=self.get_default_max_output_tokens(model_name),
provider_name=self.name,
provider_category=self.provider_category,
# put_inner_thoughts_in_kwargs=True,
diff --git a/letta/schemas/providers/openai.py b/letta/schemas/providers/openai.py
index cab713afb..72139c2cd 100644
--- a/letta/schemas/providers/openai.py
+++ b/letta/schemas/providers/openai.py
@@ -2,7 +2,7 @@ from typing import Literal
from pydantic import Field
-from letta.constants import DEFAULT_EMBEDDING_CHUNK_SIZE, LLM_MAX_TOKENS
+from letta.constants import DEFAULT_EMBEDDING_CHUNK_SIZE, LLM_MAX_CONTEXT_WINDOW
from letta.log import get_logger
from letta.schemas.embedding_config import EmbeddingConfig
from letta.schemas.enums import ProviderCategory, ProviderType
@@ -29,6 +29,14 @@ class OpenAIProvider(Provider):
api_key = self.api_key_enc.get_plaintext() if self.api_key_enc else None
openai_check_valid_api_key(self.base_url, api_key)
+ def get_default_max_output_tokens(self, model_name: str) -> int:
+ """Get the default max output tokens for OpenAI models."""
+ if model_name.startswith("gpt-5"):
+ return 16384
+ elif model_name.startswith("o1") or model_name.startswith("o3"):
+ return 100000
+ return 16384 # default for openai
+
async def _get_models_async(self) -> list[dict]:
from letta.llm_api.openai import openai_get_model_list_async
@@ -154,6 +162,7 @@ class OpenAIProvider(Provider):
model_endpoint=self.base_url,
context_window=context_window_size,
handle=handle,
+ max_tokens=self.get_default_max_output_tokens(model_name),
provider_name=self.name,
provider_category=self.provider_category,
)
@@ -190,16 +199,16 @@ class OpenAIProvider(Provider):
return llm_config
def get_model_context_window_size(self, model_name: str) -> int | None:
- if model_name in LLM_MAX_TOKENS:
- return LLM_MAX_TOKENS[model_name]
+ if model_name in LLM_MAX_CONTEXT_WINDOW:
+ return LLM_MAX_CONTEXT_WINDOW[model_name]
else:
logger.debug(
- "Model %s on %s for provider %s not found in LLM_MAX_TOKENS. Using default of {LLM_MAX_TOKENS['DEFAULT']}",
+ "Model %s on %s for provider %s not found in LLM_MAX_CONTEXT_WINDOW. Using default of {LLM_MAX_CONTEXT_WINDOW['DEFAULT']}",
model_name,
self.base_url,
self.__class__.__name__,
)
- return LLM_MAX_TOKENS["DEFAULT"]
+ return LLM_MAX_CONTEXT_WINDOW["DEFAULT"]
def get_model_context_window(self, model_name: str) -> int | None:
return self.get_model_context_window_size(model_name)
diff --git a/letta/schemas/providers/openrouter.py b/letta/schemas/providers/openrouter.py
index 5027b9f64..4423b0d57 100644
--- a/letta/schemas/providers/openrouter.py
+++ b/letta/schemas/providers/openrouter.py
@@ -2,7 +2,7 @@ from typing import Literal
from pydantic import Field
-from letta.constants import DEFAULT_EMBEDDING_CHUNK_SIZE, LLM_MAX_TOKENS
+from letta.constants import DEFAULT_EMBEDDING_CHUNK_SIZE, LLM_MAX_CONTEXT_WINDOW
from letta.log import get_logger
from letta.schemas.embedding_config import EmbeddingConfig
from letta.schemas.enums import ProviderCategory, ProviderType
@@ -41,6 +41,7 @@ class OpenRouterProvider(OpenAIProvider):
model_endpoint=self.base_url,
context_window=context_window_size,
handle=handle,
+ max_tokens=self.get_default_max_output_tokens(model_name),
provider_name=self.name,
provider_category=self.provider_category,
)
diff --git a/letta/schemas/providers/vllm.py b/letta/schemas/providers/vllm.py
index f3629e51f..335774382 100644
--- a/letta/schemas/providers/vllm.py
+++ b/letta/schemas/providers/vllm.py
@@ -45,6 +45,7 @@ class VLLMProvider(Provider):
model_wrapper=self.default_prompt_formatter,
context_window=model["max_model_len"],
handle=self.get_handle(model_name, base_name=self.handle_base) if self.handle_base else self.get_handle(model_name),
+ max_tokens=self.get_default_max_output_tokens(model_name),
provider_name=self.name,
provider_category=self.provider_category,
)
diff --git a/letta/schemas/providers/xai.py b/letta/schemas/providers/xai.py
index 61b92f17a..ee6a8ee12 100644
--- a/letta/schemas/providers/xai.py
+++ b/letta/schemas/providers/xai.py
@@ -65,6 +65,7 @@ class XAIProvider(OpenAIProvider):
model_endpoint=self.base_url,
context_window=context_window_size,
handle=self.get_handle(model_name),
+ max_tokens=self.get_default_max_output_tokens(model_name),
provider_name=self.name,
provider_category=self.provider_category,
)
diff --git a/tests/sdk/agents_test.py b/tests/sdk/agents_test.py
index 941876cc3..f02a7ef1d 100644
--- a/tests/sdk/agents_test.py
+++ b/tests/sdk/agents_test.py
@@ -8,7 +8,7 @@ AGENTS_CREATE_PARAMS = [
# Verify model_settings is populated with config values
# Note: The 'model' field itself is separate from model_settings
"model_settings": {
- "max_output_tokens": 4096,
+ "max_output_tokens": 16384,
"parallel_tool_calls": False,
"provider_type": "openai",
"temperature": 0.7,
@@ -27,7 +27,7 @@ AGENTS_UPDATE_PARAMS = [
{
# After updating just the name, model_settings should still be present
"model_settings": {
- "max_output_tokens": 4096,
+ "max_output_tokens": 16384,
"parallel_tool_calls": False,
"provider_type": "openai",
"temperature": 0.7,