Table of contents
This notebook demonstrates how to use Haijun 3.7 Sonnet's extended thinking feature with various examples and edge cases.
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):
"""Count tokens for a given message list."""
result = client.messages.count_tokens(
model="haijun-sonnet-4-6",
messages=messages
)
return result.input_tokens
Basic example
a":
print(event.delta.text, end="", flush=True)
current_content += event.delta.text
elif event.type == "content_block_stop":
if current_block_type == "thinking":
Just show a summary for thinking
print(f"\n[Completed thinking block, {len(current_content)} characters]")
elif current_block_type == "redacted_thinking":
print("\n[Redacted thinking block]")
print(f"--- Finished {current_block_type} block ---\n")
current_block_type = None
elif event.type == "message_stop":
print("\n--- Message complete ---")
streaming_with_thinking()
ī
--- Starting thinking block --- This is a classic mathematical puzzle that contains a misdirection in how the calculations are presented. Let's break it down step by step: Initial situation: - Three people each pay $10, for a total of $30 given to the manager. - The room actually costs $25. - The manager gives $5 to the bellboy to return to the customers. - The bellboy keeps $2 and returns $1 to each person (total of $3 returned). Now, let's analyze the accounting: What actually happened: - The three people originally paid $30. - They got back $3 in total ($1 each). - So they actually paid $30 - $3 = $27 in total. - Of this $27, $25 went to the hotel for the room. - The remaining $2 went to the bellboy. - $25 + $2 = $27, which matches what the guests paid. Everything balances. The error in the puzzle is in how it frames the question. The puzzle states "each person paid $10 and got back $1, so they paid $9 each, totaling $27. The bellboy kept $2, which makes $29." This is mixing up different accounting methods. The $27 that the guests paid in total should be divided as: - $25 for the room - $2 for the bellboy When we add the bellboy's $2 to the guests' $27, we're double-counting the $2, which creates the illusion of a missing dollar. The $2 is already included in the $27, so we shouldn't add it again. Another way to think about it: Out of the original $30, $25 went to the hotel, $3 went back to the guests, and $2 went to the bellboy. That's $25 + $3 + $2 = $30, so everything is accounted for. [Completed thinking block, 1492 characters] --- Finished thinking block --- --- Starting text block --- # The Missing $1 Puzzle Solution This puzzle uses a misleading way of accounting that creates confusion. Let's clarify what actually happened: ## The correct accounting: - Three people paid $30 total initially - The room cost $25 - The bellboy kept $2 - The guests received $3 back ($1 each) So where did all the money go? - $25 went to the hotel - $2 went to the bellboy - $3 went back to the guests - $25 + $2 + $3 = $30 ā ## The error in the puzzle: The puzzle incorrectly adds the $27 paid by the guests (after refunds) to the $2 kept by the bellboy. This is a mistake because the $2 kept by the bellboy is already part of the $27. The puzzle creates the illusion of a missing dollar by mixing two different perspectives: 1. How much the guests paid ($27 total) 2. Where the original $30 went (hotel + bellboy + refunds) There is no missing dollar - it's just an accounting trick!--- Finished text block --- --- Message complete --- Token counting and context window management This example demonstrates how to track token usage with extended thinking:
tyle="padding-left:16ch;text-indent:-16ch"> "role": "user",
"content": "Explain quantum computing."
}]
)
except Exception as e:
print(f"\nError with too small thinking budget: {e}")
2. Error from using temperature with thinking
try:
response = client.messages.create(
model="haijun-sonnet-4-6",
max_tokens=4000,
temperature=0.7, # Not compatible with thinking
thinking={
"type": "enabled",
"budget_tokens": 2000
},
messages=[{
"role": "user",
"content": "Write a creative story."
}]
)
except Exception as e:
print(f"\nError with temperature and thinking: {e}")
3. Error from exceeding context window
try:
Create a very large prompt
long_content = "Please analyze this text. " + "This is sample text. " * 150000
response = client.messages.create(
model="haijun-sonnet-4-6",
max_tokens=20000, # This plus the long prompt will exceed context window
thinking={
"type": "enabled",
"budget_tokens": 10000
},
messages=[{
"role": "user",
"content": long_content
}]
)
except Exception as e:
print(f"\nError from exceeding context window: {e}")
Run the common error examples
demonstrate_common_errors()
ī
Error with too small thinking budget: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'thinking.enabled.budget_tokens: Input should be greater than or equal to 1024'}} Error with temperature and thinking: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'temperature may only be set to 1 when thinking is enabled. Please consult our documentation at https://raw.haijun.my.id/docs/'}} Error from exceeding context window: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'prompt is too long: 214315 tokens > 204798 maximum'}}