Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

improvement: configurable ai.max_tokens, bump default max tokens (#4062)

Myles Scolnickb845f5d3e33b
diff --git a/marimo/_ai/llm/_impl.py b/marimo/_ai/llm/_impl.py
index 70d411a58a..1d4fdb1b88 100644
--- a/marimo/_ai/llm/_impl.py
+++ b/marimo/_ai/llm/_impl.py
@@ -235,7 +235,7 @@ class anthropic(ChatModel):
response = client.messages.create(
model=self.model,
system=self.system_message,
- max_tokens=config.max_tokens or 1000,
+ max_tokens=config.max_tokens or 4096,
messages=cast(list[MessageParam], anthropic_messages),
top_p=config.top_p if config.top_p is not None else NOT_GIVEN,
top_k=config.top_k if config.top_k is not None else NOT_GIVEN,
diff --git a/marimo/_config/config.py b/marimo/_config/config.py
index 30f147a348..77a5856977 100644
--- a/marimo/_config/config.py
+++ b/marimo/_config/config.py
@@ -197,12 +197,14 @@ class AiConfig(TypedDict, total=False):
**Keys.**
- `rules`: custom rules to include in all AI completion prompts
+ - `max_tokens`: the maximum number of tokens to use in AI completions
- `open_ai`: the OpenAI config
- `anthropic`: the Anthropic config
- `google`: the Google AI config
"""
rules: NotRequired[str]
+ max_tokens: NotRequired[int]
open_ai: OpenAiConfig
anthropic: AnthropicConfig
google: GoogleAiConfig
diff --git a/marimo/_server/api/endpoints/ai.py b/marimo/_server/api/endpoints/ai.py
index a8335a1c45..06cf15daad 100644
--- a/marimo/_server/api/endpoints/ai.py
+++ b/marimo/_server/api/endpoints/ai.py
@@ -55,6 +55,8 @@ LOGGER = _loggers.marimo_logger()
# Router for file ai
router = APIRouter()
+DEFAULT_MAX_TOKENS = 4096
+
def get_openai_client(config: MarimoConfig) -> OpenAI:
try:
@@ -166,6 +168,14 @@ def get_model(config: MarimoConfig) -> str:
return model
+def get_max_tokens(config: MarimoConfig) -> int:
+ if "ai" not in config:
+ return DEFAULT_MAX_TOKENS
+ if "max_tokens" not in config["ai"]:
+ return DEFAULT_MAX_TOKENS
+ return config["ai"]["max_tokens"]
+
+
def get_content(
response: (
RawMessageStreamEvent | ChatCompletionChunk | GenerateContentResponse
@@ -362,7 +372,7 @@ async def ai_completion(
anthropic_client = get_anthropic_client(config)
anthropic_response = anthropic_client.messages.create(
model=model,
- max_tokens=1000,
+ max_tokens=get_max_tokens(config),
messages=[
{
"role": "user",
@@ -448,7 +458,7 @@ async def ai_chat(
anthropic_client = get_anthropic_client(config)
response = anthropic_client.messages.create(
model=model,
- max_tokens=1000,
+ max_tokens=get_max_tokens(config),
messages=cast(Any, convert_to_anthropic_messages(messages)),
system=system_prompt,
stream=True,
diff --git a/openapi/api.yaml b/openapi/api.yaml
index e814fd382d..4eccee26fa 100644
--- a/openapi/api.yaml
+++ b/openapi/api.yaml
@@ -1242,6 +1242,8 @@ components:
api_key:
type: string
type: object
+ max_tokens:
+ type: integer
open_ai:
properties:
api_key:
diff --git a/openapi/src/api.ts b/openapi/src/api.ts
index 3206f54560..1241dfcaf3 100644
--- a/openapi/src/api.ts
+++ b/openapi/src/api.ts
@@ -2678,6 +2678,7 @@ export interface components {
google?: {
api_key?: string;
};
+ max_tokens?: number;
open_ai?: {
api_key?: string;
base_url?: string;