Haijun Platform Docs
ID

Table of contents

This notebook demonstrates how to use Haijun 3.7 Sonnet's extended thinking feature with tools. The extended thinking feature allows you to see Haijun's step-by-step thinking before it provides a final answer, providing transparency into how it decides which tools to use and how it interprets tool results.

import juglow

import os

import json

Global variables for model and token budgets

MODEL_NAME = "haijun-sonnet-4-6"

MAX_TOKENS = 4000

THINKING_BUDGET_TOKENS = 2000

Set your API key as an environment variable or directly

os.environ["JUGLOW_API_KEY"] = "your_api_key_here"

Initialize the client

client = juglow.Juglow()

Helper functions

def print_thinking_response(response):

"""Pretty print a message response with thinking blocks."""

print("\n==== FULL RESPONSE ====")

for block in response.content:

if block.type == "thinking":

print("\n🧠 THINKING BLOCK:")

Show truncated thinking for readability

print(block.thinking[:500] + "..." if len(block.thinking) > 500 else block.thinking)

print(f"\n[Signature available: {bool(getattr(block, 'signature', None))}]")

if hasattr(block, 'signature') and block.signature:

print(f"[Signature (first 50 chars): {block.signature[:50]}...]")

elif block.type == "redacted_thinking":

print("\n🔒 REDACTED THINKING BLOCK:")

print(f"[Data length: {len(block.data) if hasattr(block, 'data') else 'N/A'}]")

elif block.type == "text":

print("\n✓ FINAL ANSWER:")

print(block.text)

print("\n==== END RESPONSE ====")

def count_tokens(messages, tools=None):

"""Count tokens for a given message list with optional tools."""

if tools:

response = client.messages.count_tokens(

model=MODEL_NAME,

messages=messages,

tools=tools

)

else:

response = client.messages.count_tokens(

model=MODEL_NAME,

messages=messages

)

return response.input_tokens

Single tool calls with thinking

eft:12ch;text-indent:-12ch"> assistant_blocks.append(block)

Handle tool use if required

full_conversation = [{

"role": "user",

"content": "What's the weather like in Paris today?"

}]

if response.stop_reason == "tool_use":

Add entire assistant response with thinking blocks and tool use

full_conversation.append({

"role": "assistant",

"content": assistant_blocks

})

Find the tool_use block

tool_use_block = next((block for block in response.content if block.type == "tool_use"), None)

if tool_use_block:

Execute the tool

print(f"\n=== EXECUTING TOOL ===")

print(f"Tool name: {tool_use_block.name}")

print(f"Location to check: {tool_use_block.input['location']}")

tool_result = weather(tool_use_block.input["location"])

print(f"Result: {tool_result}")

print("=== TOOL EXECUTION COMPLETE ===\n")

Add tool result to conversation

full_conversation.append({

"role": "user",

"content": [{

"type": "tool_result",

"tool_use_id": tool_use_block.id,

"content": json.dumps(tool_result)

}]

})

Continue the conversation with the same thinking configuration

print("\n=== SENDING FOLLOW-UP REQUEST WITH TOOL RESULT ===")

response = client.messages.create(

model=MODEL_NAME,

max_tokens=MAX_TOKENS,

thinking={

"type": "enabled",

"budget_tokens": THINKING_BUDGET_TOKENS

},

tools=tools,

messages=full_conversation

)

print(f"Follow-up response received. Stop reason: {response.stop_reason}")

print_thinking_response(response)

Run the example

tool_use_with_thinking()

=== INITIAL RESPONSE === Response ID: msg_01NhR4vE9nVh2sHs5fXbzji8 Stop reason: tool_use Model: haijun-sonnet-4-6 Content blocks: 3 blocks Block 1: Type = thinking Thinking content: The user is asking about the current weather in Paris. I can use the weather function to get this information. The weather function requires a "l... Signature available: True Block 2: Type = text Text content: I'll check the current weather in Paris for you. Block 3: Type = tool_use Tool: weather Tool input: {'location': 'Paris'} Tool ID: toolu_01WaeSyitUGJFaaPe68cJuEv === END INITIAL RESPONSE === === EXECUTING TOOL === Tool name: weather Location to check: Paris Result: {'temperature': 65, 'condition': 'Rainy'} === TOOL EXECUTION COMPLETE === === SENDING FOLLOW-UP REQUEST WITH TOOL RESULT === Follow-up response received. Stop reason: end_turn ==== FULL RESPONSE ==== ✓ FINAL ANSWER: Currently in Paris, it's 65°F (18°C) and rainy. You might want to bring an umbrella if you're heading out! ==== END RESPONSE ==== Multiple tool calls with thinking This example demonstrates how to handle multiple tool calls, such as a mock news and weather service, while observing the thinking process.

ation to check: London Result: {'temperature': 62, 'condition': 'Cloudy'} === TOOL EXECUTION COMPLETE === === SENDING FOLLOW-UP REQUEST WITH TOOL RESULT === === FOLLOW-UP RESPONSE (ITERATION 1) === Response ID: msg_01EhR96Z2Z2t5EDhuWeodUod Stop reason: tool_use Content blocks: 1 blocks Block 1: Type = tool_use Tool: news Tool input preview: {'topic': 'technology'} === END FOLLOW-UP RESPONSE (ITERATION 1) === === TOOL USE ITERATION 2 === === EXECUTING TOOL === Tool name: news Topic to check: technology Result: {'headlines': ['New AI breakthrough announced by research lab', 'Tech company releases latest smartphone model', 'Quantum computing reaches milestone achievement']} === TOOL EXECUTION COMPLETE === === SENDING FOLLOW-UP REQUEST WITH TOOL RESULT === === FOLLOW-UP RESPONSE (ITERATION 2) === Response ID: msg_01WUEfC4UxPFaJaktjVDMJEN Stop reason: end_turn Content blocks: 1 blocks Block 1: Type = text Text content preview: Here's the information you requested: ## Weather in London Currently, it's 62°F and cloudy in Londo... === END FOLLOW-UP RESPONSE (ITERATION 2) === === FINAL RESPONSE === ==== FULL RESPONSE ==== ✓ FINAL ANSWER: Here's the information you requested: ## Weather in London Currently, it's 62°F and cloudy in London. ## Latest Technology News Headlines - New AI breakthrough announced by research lab - Tech company releases latest smartphone model - Quantum computing reaches milestone achievement ==== END RESPONSE ==== === END FINAL RESPONSE ===

Preserving thinking blocks

On this page
Single tool calls with thinkingPreserving thinking blocks