From 23eb0877596fd54d00576d7c13e1b59cfde13070 Mon Sep 17 00:00:00 2001 From: Kevin Lin Date: Mon, 25 Aug 2025 13:36:55 -0700 Subject: [PATCH] fix: pass thinking to anthropic client (#4150) --- letta/llm_api/anthropic_client.py | 24 +++++++++++++++++++----- letta/schemas/message.py | 2 +- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/letta/llm_api/anthropic_client.py b/letta/llm_api/anthropic_client.py index b2e90889..ef54ff62 100644 --- a/letta/llm_api/anthropic_client.py +++ b/letta/llm_api/anthropic_client.py @@ -287,12 +287,26 @@ class AnthropicClient(LLMClientBase): else: anthropic_tools = None + thinking_enabled = False + if messages: + for message in messages: + if isinstance(message.get("content"), list): + for content_block in message["content"]: + if content_block.get("type") == "thinking": + thinking_enabled = True + break + try: - result = await client.beta.messages.count_tokens( - model=model or "claude-3-7-sonnet-20250219", - messages=messages or [{"role": "user", "content": "hi"}], - tools=anthropic_tools or [], - ) + count_params = { + "model": model or "claude-3-7-sonnet-20250219", + "messages": messages or [{"role": "user", "content": "hi"}], + "tools": anthropic_tools or [], + } + + if thinking_enabled: + count_params["thinking"] = {"type": "enabled", "budget_tokens": 16000} + + result = await client.beta.messages.count_tokens(**count_params) except: raise diff --git a/letta/schemas/message.py b/letta/schemas/message.py index 4fd0a8ea..5376cd5f 100644 --- a/letta/schemas/message.py +++ b/letta/schemas/message.py @@ -846,7 +846,7 @@ class Message(BaseMessage): } content = [] # COT / reasoning / thinking - if self.content is not None and len(self.content) > 1: + if self.content is not None and len(self.content) >= 1: for content_part in self.content: if isinstance(content_part, ReasoningContent): content.append(