Skip to content

Commit bdb86c3

Browse files
committed
Configure max_tokens for Anthropic provider to prevent early streaming response truncation
1 parent 1c7698d commit bdb86c3

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

lib/agent/agent_loop.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ def _get_database_id(indicator_code: str) -> str:
138138
"model": "claude-sonnet-4-6",
139139
"extra_headers": {"anthropic-version": "2023-06-01"},
140140
"thinking": {"type": "enabled", "budget_tokens": 8000},
141+
"max_tokens": 16000,
141142
"temperature": 1,
142143
},
143144
"gemini-user": {
@@ -1229,6 +1230,8 @@ def _create_chat_completion(client, config, messages):
12291230
extra_kwargs: dict = {}
12301231
if "thinking" in config:
12311232
extra_kwargs["extra_body"] = {"thinking": config["thinking"]}
1233+
if "max_tokens" in config:
1234+
extra_kwargs["max_tokens"] = config["max_tokens"]
12321235
return client.chat.completions.create(
12331236
model=config["model"],
12341237
messages=messages,
@@ -1923,6 +1926,8 @@ def worker():
19231926
extra_kwargs: dict = {}
19241927
if "thinking" in config:
19251928
extra_kwargs["extra_body"] = {"thinking": config["thinking"]}
1929+
if "max_tokens" in config:
1930+
extra_kwargs["max_tokens"] = config["max_tokens"]
19261931

19271932
# Strip source_seal from tool results before streaming re-fetch.
19281933
# The LLM does not need seal hashes to write its response; including them

tests/test_agent_loop.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,13 @@ def test_run_agent_anthropic_thinking(mock_openai_class):
178178

179179
run_agent("test", [], "anthropic", user_api_key="sk-ant-key")
180180

181-
# The completions.create call should include extra_body with thinking
181+
# The completions.create call should include extra_body with thinking and max_tokens
182182
create_call = mock_client.chat.completions.create.call_args
183183
assert create_call.kwargs.get("temperature") == 1
184184
extra_body = create_call.kwargs.get("extra_body")
185185
assert extra_body is not None
186186
assert extra_body.get("thinking", {}).get("type") == "enabled"
187+
assert create_call.kwargs.get("max_tokens") == 16000
187188

188189

189190
# ── UserKeyError exception propagation ───────────────────────────────────

0 commit comments

Comments
 (0)