import os
# os.environ['ANTHROPIC_LOG'] = 'debug'
Claudette’s source
This is the ‘literate’ source code for Claudette. You can view the fully rendered version of the notebook here, or you can clone the git repo and run the interactive notebook in Jupyter. The notebook is converted the Python module claudette/core.py using nbdev. The goal of this source code is to both create the Python module, and also to teach the reader how it is created, without assuming much existing knowledge about Claude’s API.
Most of the time you’ll see that we write some source code first, and then a description or discussion of it afterwards.
Setup
To print every HTTP request and response in full, uncomment the above line. This functionality is provided by Anthropic’s SDK.
from anthropic.types import Model
from claudette.text_editor import *
from typing import get_args
from datetime import datetime
from pprint import pprint
from IPython.display import Image
import warnings
"ignore", message="Pydantic serializer warnings") warnings.filterwarnings(
If you’re reading the rendered version of this notebook, you’ll see an “Exported source” collapsible widget below. If you’re reading the source notebook directly, you’ll see #| exports
at the top of the cell. These show that this piece of code will be exported into the python module that this notebook creates. No other code will be included – any other code in this notebook is just for demonstration, documentation, and testing.
You can toggle expanding/collapsing the source code of all exported sections by using the </> Code
menu in the top right of the rendered notebook page.
Exported source
= {
model_types # Anthropic
'claude-opus-4-20250514': 'opus',
'claude-sonnet-4-20250514': 'sonnet',
'claude-3-opus-20240229': 'opus-3',
'claude-3-7-sonnet-20250219': 'sonnet-3-7',
'claude-3-5-sonnet-20241022': 'sonnet-3-5',
'claude-3-haiku-20240307': 'haiku-3',
'claude-3-5-haiku-20241022': 'haiku-3-5',
# AWS
'anthropic.claude-3-opus-20240229-v1:0': 'opus',
'anthropic.claude-3-5-sonnet-20241022-v2:0': 'sonnet',
'anthropic.claude-3-sonnet-20240229-v1:0': 'sonnet',
'anthropic.claude-3-haiku-20240307-v1:0': 'haiku',
# Google
'claude-3-opus@20240229': 'opus',
'claude-3-5-sonnet-v2@20241022': 'sonnet',
'claude-3-sonnet@20240229': 'sonnet',
'claude-3-haiku@20240307': 'haiku',
}
= list(model_types) all_models
models
['claude-opus-4-20250514',
'claude-sonnet-4-20250514',
'claude-3-opus-20240229',
'claude-3-7-sonnet-20250219',
'claude-3-5-sonnet-20241022']
Exported source
= ('claude-3-5-haiku-20241022',) text_only_models
Exported source
= set(all_models)
has_streaming_models = set(all_models)
has_system_prompt_models = set(all_models)
has_temperature_models = {'claude-opus-4-20250514', 'claude-sonnet-4-20250514', 'claude-3-7-sonnet-20250219'} has_extended_thinking_models
has_extended_thinking_models
{'claude-3-7-sonnet-20250219',
'claude-opus-4-20250514',
'claude-sonnet-4-20250514'}
can_use_extended_thinking
can_use_extended_thinking (m)
Exported source
def can_stream(m): return m in has_streaming_models
def can_set_system_prompt(m): return m in has_system_prompt_models
def can_set_temperature(m): return m in has_temperature_models
def can_use_extended_thinking(m): return m in has_extended_thinking_models
can_set_temperature
can_set_temperature (m)
can_set_system_prompt
can_set_system_prompt (m)
can_stream
can_stream (m)
We include these functions to provide a uniform library interface with cosette since openai models such as o1 do not have many of these capabilities.
assert can_stream('claude-3-5-sonnet-20241022') and can_set_system_prompt('claude-3-5-sonnet-20241022') and can_set_temperature('claude-3-5-sonnet-20241022')
These are the current versions and prices of Anthropic’s models at the time of writing.
= models[1]; model model
'claude-sonnet-4-20250514'
For examples, we’ll use the latest Sonnet, since it’s awesome.
Antropic SDK
= Anthropic() cli
This is what Anthropic’s SDK provides for interacting with Python. To use it, pass it a list of messages, with content and a role. The roles should alternate between user and assistant.
After the code below you’ll see an indented section with an orange vertical line on the left. This is used to show the result of running the code above. Because the code is running in a Jupyter Notebook, we don’t have to use print
to display results, we can just type the expression directly, as we do with r
here.
= {'role': 'user', 'content': "I'm Jeremy"}
m = cli.messages.create(messages=[m], model=model, max_tokens=100)
r r
Hello Jeremy! Nice to meet you. How are you doing today?
- id:
msg_016TdhPFM7jpFuWLQqf3V47L
- content:
[{'citations': None, 'text': 'Hello Jeremy! Nice to meet you. How are you doing today?', 'type': 'text'}]
- model:
claude-sonnet-4-20250514
- role:
assistant
- stop_reason:
end_turn
- stop_sequence:
None
- type:
message
- usage:
{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 10, 'output_tokens': 17, 'server_tool_use': None, 'service_tier': 'standard'}
Formatting output
That output is pretty long and hard to read, so let’s clean it up. We’ll start by pulling out the Content
part of the message. To do that, we’re going to write our first function which will be included to the claudette/core.py
module.
This is the first exported public function or class we’re creating (the previous export was of a variable). In the rendered version of the notebook for these you’ll see 4 things, in this order (unless the symbol starts with a single _
, which indicates it’s private):
- The signature (with the symbol name as a heading, with a horizontal rule above)
- A table of paramater docs (if provided)
- The doc string (in italics).
- The source code (in a collapsible “Exported source” block)
After that, we generally provide a bit more detail on what we’ve created, and why, along with a sample usage.
find_block
find_block (r:collections.abc.Mapping, blk_type:type|str=<class 'anthropic.types.text_block.TextBlock'>)
Find the first block of type blk_type
in r.content
.
Type | Default | Details | |
---|---|---|---|
r | Mapping | The message to look in | |
blk_type | type | str | TextBlock | The type of block to find |
Exported source
def _type(x):
try: return x.type
except AttributeError: return x.get('type')
def find_block(r:abc.Mapping, # The message to look in
type|str=TextBlock # The type of block to find
blk_type:
):"Find the first block of type `blk_type` in `r.content`."
= (lambda x:_type(x)==blk_type) if isinstance(blk_type,str) else (lambda x:isinstance(x,blk_type))
f return first(o for o in r.content if f(o))
This makes it easier to grab the needed parts of Claude’s responses, which can include multiple pieces of content. By default, we look for the first text block. That will generally have the content we want to display.
find_block(r)
TextBlock(citations=None, text='Hello Jeremy! Nice to meet you. How are you doing today?', type='text')
def contents(r):
"Helper to get the contents from Claude response `r`."
= find_block(r)
blk if not blk and r.content: blk = r.content[0]
return blk.text.strip() if hasattr(blk,'text') else str(blk)
For display purposes, we often just want to show the text itself.
contents(r)
'Hello Jeremy! Nice to meet you. How are you doing today?'
Exported source
@patch
def _repr_markdown_(self:(Message)):
= '\n- '.join(f'{k}: `{v}`' for k,v in self.model_dump().items())
det = re.sub(r'\$', '$', contents(self)) # escape `$` for jupyter latex
cts return f"""{cts}
<details>
- {det}
</details>"""
Jupyter looks for a _repr_markdown_
method in displayed objects; we add this in order to display just the content text, and collapse full details into a hideable section. Note that patch
is from fastcore, and is used to add (or replace) functionality in an existing class. We pass the class(es) that we want to patch as type annotations to self
. In this case, _repr_markdown_
is being added to Anthropic’s Message
class, so when we display the message now we just see the contents, and the details are hidden away in a collapsible details block.
r
Hello Jeremy! Nice to meet you. How are you doing today?
- id:
msg_016TdhPFM7jpFuWLQqf3V47L
- content:
[{'citations': None, 'text': 'Hello Jeremy! Nice to meet you. How are you doing today?', 'type': 'text'}]
- model:
claude-sonnet-4-20250514
- role:
assistant
- stop_reason:
end_turn
- stop_sequence:
None
- type:
message
- usage:
{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 10, 'output_tokens': 17, 'server_tool_use': None, 'service_tier': 'standard'}
One key part of the response is the usage
key, which tells us how many tokens we used by returning a Usage
object.
We’ll add some helpers to make things a bit cleaner for creating and formatting these objects.
r.usage
In: 10; Out: 17; Cache create: 0; Cache read: 0; Total Tokens: 27; Search: 0
server_tool_usage
server_tool_usage (web_search_requests=0)
Little helper to create a server tool usage object
Exported source
def server_tool_usage(web_search_requests=0):
'Little helper to create a server tool usage object'
return ServerToolUsage(web_search_requests=web_search_requests)
usage
usage (inp=0, out=0, cache_create=0, cache_read=0, server_tool_use=ServerToolUsage(web_search_requests=0))
Slightly more concise version of Usage
.
Type | Default | Details | |
---|---|---|---|
inp | int | 0 | input tokens |
out | int | 0 | Output tokens |
cache_create | int | 0 | Cache creation tokens |
cache_read | int | 0 | Cache read tokens |
server_tool_use | ServerToolUsage | ServerToolUsage(web_search_requests=0) | server tool use |
Exported source
def usage(inp=0, # input tokens
=0, # Output tokens
out=0, # Cache creation tokens
cache_create=0, # Cache read tokens
cache_read=server_tool_usage() # server tool use
server_tool_use
):'Slightly more concise version of `Usage`.'
return Usage(input_tokens=inp, output_tokens=out, cache_creation_input_tokens=cache_create,
=cache_read, server_tool_use=server_tool_use) cache_read_input_tokens
The constructor provided by Anthropic is rather verbose, so we clean it up a bit, using a lowercase version of the name.
5) usage(
In: 5; Out: 0; Cache create: 0; Cache read: 0; Total Tokens: 5; Search: 0
Usage.total
Usage.total ()
Exported source
def _dgetattr(o,s,d):
"Like getattr, but returns the default if the result is None"
return getattr(o,s,d) or d
@patch(as_prop=True)
def total(self:Usage): return self.input_tokens+self.output_tokens+_dgetattr(self, "cache_creation_input_tokens",0)+_dgetattr(self, "cache_read_input_tokens",0)
Adding a total
property to Usage
makes it easier to see how many tokens we’ve used up altogether.
5,1).total usage(
6
Usage.__repr__
Usage.__repr__ ()
Return repr(self).
Exported source
@patch
def __repr__(self:Usage):
= f'In: {self.input_tokens}; Out: {self.output_tokens}'
io_toks = f'Cache create: {_dgetattr(self, "cache_creation_input_tokens",0)}; Cache read: {_dgetattr(self, "cache_read_input_tokens",0)}'
cache_toks = _dgetattr(self, "server_tool_use",server_tool_usage())
server_tool_use = f'Search: {server_tool_use.web_search_requests}'
server_tool_use_str = f'Total Tokens: {self.total}'
total_tok return f'{io_toks}; {cache_toks}; {total_tok}; {server_tool_use_str}'
In python, patching __repr__
lets us change how an object is displayed. (More generally, methods starting and ending in __
in Python are called dunder
methods, and have some magic
behavior – such as, in this case, changing how an object is displayed.) We won’t be directly displaying ServerToolUsage’s, so we can handle its display behavior in the same Usage __repr__
5) usage(
In: 5; Out: 0; Cache create: 0; Cache read: 0; Total Tokens: 5; Search: 0
ServerToolUsage.__add__
ServerToolUsage.__add__ (b)
Add together each of the server tool use counts
Exported source
@patch
def __add__(self:ServerToolUsage, b):
"Add together each of the server tool use counts"
return ServerToolUsage(web_search_requests=self.web_search_requests+b.web_search_requests)
And, patching __add__
lets +
work on a ServerToolUsage
as well as a Usage
object.
1) + server_tool_usage(2) server_tool_usage(
ServerToolUsage(web_search_requests=3)
Usage.__add__
Usage.__add__ (b)
Add together each of input_tokens
and output_tokens
Exported source
@patch
def __add__(self:Usage, b):
"Add together each of `input_tokens` and `output_tokens`"
return usage(self.input_tokens+b.input_tokens, self.output_tokens+b.output_tokens,
self,'cache_creation_input_tokens',0)+_dgetattr(b,'cache_creation_input_tokens',0),
_dgetattr(self,'cache_read_input_tokens',0)+_dgetattr(b,'cache_read_input_tokens',0),
_dgetattr(self,'server_tool_use',server_tool_usage())+_dgetattr(b,'server_tool_use',server_tool_usage())) _dgetattr(
+r.usage + usage(server_tool_use=server_tool_usage(1)) r.usage
In: 20; Out: 34; Cache create: 0; Cache read: 0; Total Tokens: 54; Search: 1
Creating messages
Creating correctly formatted dict
s from scratch every time isn’t very handy, so we’ll import a couple of helper functions from the msglm
library.
Let’s use mk_msg
to recreate our msg {'role': 'user', 'content': "I'm Jeremy"}
from earlier.
= "I'm Jeremy"
prompt = mk_msg(prompt)
m = cli.messages.create(messages=[m], model=model, max_tokens=100)
r r
Hello Jeremy! Nice to meet you. How are you doing today?
- id:
msg_01Q6mzzC94ZvCQ714b9ULJYw
- content:
[{'citations': None, 'text': 'Hello Jeremy! Nice to meet you. How are you doing today?', 'type': 'text'}]
- model:
claude-sonnet-4-20250514
- role:
assistant
- stop_reason:
end_turn
- stop_sequence:
None
- type:
message
- usage:
{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 10, 'output_tokens': 17, 'server_tool_use': None, 'service_tier': 'standard'}
We can pass more than just text messages to Claude. As we’ll see later we can also pass images, SDK objects, etc. To handle these different data types we need to pass the type along with our content to Claude.
Here’s an example of a multimodal message containing text and images.
{
'role': 'user',
'content': [
{'type':'text', 'text':'What is in the image?'},
{
'type':'image',
'source': {
'type':'base64', 'media_type':'media_type', 'data': 'data'
}
}
]
}
mk_msg
infers the type automatically and creates the appropriate data structure.
LLMs, don’t actually have state, but instead dialogs are created by passing back all previous prompts and responses every time. With Claude, they always alternate user and assistant. We’ll use mk_msgs
from msglm
to make it easier to build up these dialog lists.
= mk_msgs([prompt, r, "I forgot my name. Can you remind me please?"])
msgs msgs
[{'role': 'user', 'content': "I'm Jeremy"},
{'role': 'assistant',
'content': [TextBlock(citations=None, text='Hello Jeremy! Nice to meet you. How are you doing today?', type='text')]},
{'role': 'user', 'content': 'I forgot my name. Can you remind me please?'}]
=msgs, model=model, max_tokens=200) cli.messages.create(messages
Your name is Jeremy - you introduced yourself to me just a moment ago!
- id:
msg_015VWt5QqEMhiydE7PETbpzU
- content:
[{'citations': None, 'text': 'Your name is Jeremy - you introduced yourself to me just a moment ago!', 'type': 'text'}]
- model:
claude-sonnet-4-20250514
- role:
assistant
- stop_reason:
end_turn
- stop_sequence:
None
- type:
message
- usage:
{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 41, 'output_tokens': 18, 'server_tool_use': None, 'service_tier': 'standard'}
Client
Client
Client (model, cli=None, log=False, cache=False)
Basic Anthropic messages client.
Exported source
class Client:
def __init__(self, model, cli=None, log=False, cache=False):
"Basic Anthropic messages client."
self.model,self.use = model,usage()
self.text_only = model in text_only_models
self.log = [] if log else None
self.c = (cli or Anthropic(default_headers={'anthropic-beta': 'prompt-caching-2024-07-31'}))
self.cache = cache
We’ll create a simple Client
for Anthropic
which tracks usage stores the model to use. We don’t add any methods right away – instead we’ll use patch
for that so we can add and document them incrementally.
= Client(model)
c c.use
In: 0; Out: 0; Cache create: 0; Cache read: 0; Total Tokens: 0; Search: 0
Exported source
@patch
def _r(self:Client, r:Message, prefill=''):
"Store the result of the message and accrue total usage."
if prefill:
= find_block(r)
blk if blk: blk.text = prefill + (blk.text or '')
self.result = r
self.use += r.usage
self.stop_reason = r.stop_reason
self.stop_sequence = r.stop_sequence
return r
We use a _
prefix on private methods, but we document them here in the interests of literate source code.
_r
will be used each time we get a new result, to track usage and also to keep the result available for later.
c._r(r) c.use
In: 10; Out: 17; Cache create: 0; Cache read: 0; Total Tokens: 27; Search: 0
Whereas OpenAI’s models use a stream
parameter for streaming, Anthropic’s use a separate method. We implement Anthropic’s approach in a private method, and then use a stream
parameter in __call__
for consistency:
Exported source
@patch
def _log(self:Client, final, prefill, msgs, **kwargs):
self._r(final, prefill)
if self.log is not None: self.log.append({
"msgs": msgs, **kwargs,
"result": self.result, "use": self.use, "stop_reason": self.stop_reason, "stop_sequence": self.stop_sequence
})return self.result
Once streaming is complete, we need to store the final message and call any completion callback that’s needed.
get_types
get_types (msgs)
Exported source
@save_iter
def _stream(o, cm, prefill, cb):
with cm as s:
yield prefill
yield from s.text_stream
= s.get_final_message()
o.value cb(o.value)
get_types(msgs)
['text', 'text', 'text']
mk_tool_choice
mk_tool_choice (choose:Union[str,bool,NoneType])
Create a tool_choice
dict that’s ‘auto’ if choose
is None
, ‘any’ if it is True, or ‘tool’ otherwise
print(mk_tool_choice('sums'))
print(mk_tool_choice(True))
print(mk_tool_choice(None))
{'type': 'tool', 'name': 'sums'}
{'type': 'any'}
{'type': 'auto'}
Claude can be forced to use a particular tool, or select from a specific list of tools, or decide for itself when to use a tool. If you want to force a tool (or force choosing from a list), include a tool_choice
param with a dict from mk_tool_choice
.
Claude supports adding an extra assistant
message at the end, which contains the prefill – i.e. the text we want Claude to assume the response starts with. However Claude doesn’t actually repeat that in the response, so for convenience we add it.
Client.__call__
Client.__call__ (msgs:list, sp='', temp=0, maxtok=4096, maxthinktok=0, prefill='', stream:bool=False, stop=None, tools:Optional[list]=None, tool_choice:Optional[dict]=None, cb=None, metadata:MetadataParam|NotGiven=NOT_GIVEN, service_tier: "Literal['auto','standard_only']|NotGiven"=NOT_GIVEN, stop_sequences:List[str]|NotGiven=NOT_GIVEN, system:Unio n[str,Iterable[TextBlockParam]]|NotGiven=NOT_GIVEN, temperature:float|NotGiven=NOT_GIVEN, thinking:ThinkingConfigParam|NotGiven=NOT_GIVEN, top_k:int|NotGiven=NOT_GIVEN, top_p:float|NotGiven=NOT_GIVEN, extra_headers:Headers|None=None, extra_query:Query|None=None, extra_body:Body|None=None, timeout:float|httpx.Timeout|None|NotGiven=NOT_GIVEN)
Make a call to Claude.
Type | Default | Details | |
---|---|---|---|
msgs | list | List of messages in the dialog | |
sp | str | The system prompt | |
temp | int | 0 | Temperature |
maxtok | int | 4096 | Maximum tokens |
maxthinktok | int | 0 | Maximum thinking tokens |
prefill | str | Optional prefill to pass to Claude as start of its response | |
stream | bool | False | Stream response? |
stop | NoneType | None | Stop sequence |
tools | Optional | None | List of tools to make available to Claude |
tool_choice | Optional | None | Optionally force use of some tool |
cb | NoneType | None | Callback to pass result to when complete |
metadata | MetadataParam | NotGiven | NOT_GIVEN | |
service_tier | Literal[‘auto’, ‘standard_only’] | NotGiven | NOT_GIVEN | |
stop_sequences | List[str] | NotGiven | NOT_GIVEN | |
system | Union[str, Iterable[TextBlockParam]] | NotGiven | NOT_GIVEN | |
temperature | float | NotGiven | NOT_GIVEN | |
thinking | ThinkingConfigParam | NotGiven | NOT_GIVEN | |
top_k | int | NotGiven | NOT_GIVEN | |
top_p | float | NotGiven | NOT_GIVEN | |
extra_headers | Optional | None | Use the following arguments if you need to pass additional parameters to the API that aren’t available via kwargs. The extra values given here take precedence over values defined on the client or passed to this method. |
extra_query | Query | None | None | |
extra_body | Body | None | None | |
timeout | float | httpx.Timeout | None | NotGiven | NOT_GIVEN |
Exported source
@patch
def _precall(self:Client, msgs, prefill, sp, temp, maxtok, maxthinktok, stream,
stop, tools, tool_choice, kwargs):if tools: kwargs['tools'] = [get_schema(o) if callable(o) else o for o in listify(tools)]
if tool_choice: kwargs['tool_choice'] = mk_tool_choice(tool_choice)
if maxthinktok:
'thinking'] = {'type':'enabled', 'budget_tokens':maxthinktok}
kwargs[= 1,''
temp,prefill = [prefill.strip()] if prefill else []
pref if not isinstance(msgs,list): msgs = [msgs]
if stop is not None:
if not isinstance(stop, (list)): stop = [stop]
"stop_sequences"] = stop
kwargs[= mk_msgs(msgs+pref, cache=self.cache, cache_last_ckpt_only=self.cache)
msgs assert not ('image' in get_types(msgs) and self.text_only), f"Images not supported by: {self.model}"
|= dict(max_tokens=maxtok, system=sp, temperature=temp)
kwargs return msgs, kwargs
Exported source
@patch
@delegates(messages.Messages.create)
def __call__(self:Client,
list, # List of messages in the dialog
msgs:='', # The system prompt
sp=0, # Temperature
temp=4096, # Maximum tokens
maxtok=0, # Maximum thinking tokens
maxthinktok='', # Optional prefill to pass to Claude as start of its response
prefillbool=False, # Stream response?
stream:=None, # Stop sequence
stoplist]=None, # List of tools to make available to Claude
tools:Optional[dict]=None, # Optionally force use of some tool
tool_choice:Optional[=None, # Callback to pass result to when complete
cb**kwargs):
"Make a call to Claude."
= self._precall(msgs, prefill, sp, temp, maxtok, maxthinktok, stream,
msgs,kwargs
stop, tools, tool_choice, kwargs)= self.c.messages
m = m.stream if stream else m.create
f = f(model=self.model, messages=msgs, **kwargs)
res def _cb(v):
self._log(v, prefill=prefill, msgs=msgs, **kwargs)
if cb: cb(v)
if stream: return _stream(res, prefill, _cb)
try: return res
finally: _cb(res)
Defining __call__
let’s us use an object like a function (i.e it’s callable). We use it as a small wrapper over messages.create
.
= Client(model, log=True)
c c.use
In: 0; Out: 0; Cache create: 0; Cache read: 0; Total Tokens: 0; Search: 0
'Hi') c(
Hello! How are you doing today? Is there anything I can help you with?
- id:
msg_011Xc3Sfuwj6FcYHHMQ95Gyj
- content:
[{'citations': None, 'text': 'Hello! How are you doing today? Is there anything I can help you with?', 'type': 'text'}]
- model:
claude-sonnet-4-20250514
- role:
assistant
- stop_reason:
end_turn
- stop_sequence:
None
- type:
message
- usage:
{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 8, 'output_tokens': 20, 'server_tool_use': None, 'service_tier': 'standard'}
Usage details are automatically updated after each call:
c.use
In: 8; Out: 20; Cache create: 0; Cache read: 0; Total Tokens: 28; Search: 0
A log of all messages is kept if log=True
is passed:
pprint(c.log)
[{'max_tokens': 4096,
'msgs': [{'content': 'Hi', 'role': 'user'}],
'result': Message(id='msg_011Xc3Sfuwj6FcYHHMQ95Gyj', content=[TextBlock(citations=None, text='Hello! How are you doing today? Is there anything I can help you with?', type='text')], model='claude-sonnet-4-20250514', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 8; Out: 20; Cache create: 0; Cache read: 0; Total Tokens: 28; Search: 0),
'stop_reason': 'end_turn',
'stop_sequence': None,
'system': '',
'temperature': 0,
'use': In: 8; Out: 20; Cache create: 0; Cache read: 0; Total Tokens: 28; Search: 0}]
Let’s try out prefill:
= "Very concisely, what is the meaning of life?"
q = 'According to Douglas Adams, ' pref
=pref) c(q, prefill
According to Douglas Adams, 42. But seriously, there’s no universal answer - it’s deeply personal. Common themes include: finding purpose, connecting with others, growing as a person, and creating meaning through your choices and relationships.
- id:
msg_01KxZXEKz1ePJg6HWXvVgZJK
- content:
[{'citations': None, 'text': "According to Douglas Adams, 42. But seriously, there's no universal answer - it's deeply personal. Common themes include: finding purpose, connecting with others, growing as a person, and creating meaning through your choices and relationships.", 'type': 'text'}]
- model:
claude-sonnet-4-20250514
- role:
assistant
- stop_reason:
end_turn
- stop_sequence:
None
- type:
message
- usage:
{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 24, 'output_tokens': 44, 'server_tool_use': None, 'service_tier': 'standard'}
c.use
In: 32; Out: 64; Cache create: 0; Cache read: 0; Total Tokens: 96; Search: 0
We can pass stream=True
to stream the response back incrementally:
= c('Hi', stream=True)
r for o in r: print(o, end='')
Hello! How are you doing today? Is there anything I can help you with?
c.use
In: 40; Out: 84; Cache create: 0; Cache read: 0; Total Tokens: 124; Search: 0
The full final message after completion of streaming is in the value
attr of the response:
r.value
Hello! How are you doing today? Is there anything I can help you with?
- id:
msg_01VGsLna4jY8C2pqJGxgwVWT
- content:
[{'citations': None, 'text': 'Hello! How are you doing today? Is there anything I can help you with?', 'type': 'text'}]
- model:
claude-sonnet-4-20250514
- role:
assistant
- stop_reason:
end_turn
- stop_sequence:
None
- type:
message
- usage:
{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 8, 'output_tokens': 20, 'server_tool_use': None, 'service_tier': 'standard'}
for o in c(q, prefill=pref, stream=True): print(o, end='')
According to Douglas Adams, 42. But seriously, there's no universal answer - it's deeply personal. Common themes include: finding purpose, connecting with others, growing as a person, and creating meaning through your choices and relationships.
c.use
In: 64; Out: 128; Cache create: 0; Cache read: 0; Total Tokens: 192; Search: 0
Pass a stop sequence if you want claude to stop generating text when it encounters it.
"Count from 1 to 10", stop="5") c(
1, 2, 3, 4,
- id:
msg_01Tq6gw5nmJPjY71wKQGk4eF
- content:
[{'citations': None, 'text': '1, 2, 3, 4, ', 'type': 'text'}]
- model:
claude-sonnet-4-20250514
- role:
assistant
- stop_reason:
stop_sequence
- stop_sequence:
5
- type:
message
- usage:
{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 15, 'output_tokens': 14, 'server_tool_use': None, 'service_tier': 'standard'}
This also works with streaming, and you can pass more than one stop sequence:
for o in c("Count from 1 to 10", stop=["3", "yellow"], stream=True): print(o, end='')
print()
print(c.stop_reason, c.stop_sequence)
1, 2,
stop_sequence 3
We’ve shown the token usage but we really care about is pricing. Let’s extract the latest pricing from Anthropic into a pricing
dict.
get_pricing
get_pricing (m, u)
Exported source
def get_pricing(m, u):
return pricing[m][:3] if u.prompt_token_count < 128_000 else pricing[m][3:]
Similarly, let’s get the pricing for the latest server tools:
We’ll patch Usage
to enable it compute the cost given pricing.
Usage.cost
Usage.cost (costs:tuple)
Exported source
@patch
def cost(self:Usage, costs:tuple) -> float:
= _dgetattr(self, "cache_creation_input_tokens",0), _dgetattr(self, "cache_read_input_tokens",0)
cache_w, cache_r = sum([self.input_tokens * costs[0] + self.output_tokens * costs[1] + cache_w * costs[2] + cache_r * costs[3]]) / 1e6
tok_cost = _dgetattr(self, "server_tool_use",server_tool_usage())
server_tool_use = server_tool_use.web_search_requests * server_tool_pricing['web_search_requests'] / 1e3
server_tool_cost return tok_cost + server_tool_cost
Client.cost
Client.cost ()
Exported source
@patch(as_prop=True)
def cost(self: Client) -> float: return self.use.cost(pricing[model_types[self.model]])
get_costs
get_costs (c)
Exported source
def get_costs(c):
= pricing[model_types[c.model]]
costs
= c.use.input_tokens * costs[0] / 1e6
inp_cost = c.use.output_tokens * costs[1] / 1e6
out_cost
= c.use.cache_creation_input_tokens
cache_w = c.use.cache_read_input_tokens
cache_r = (cache_w * costs[2] + cache_r * costs[3]) / 1e6
cache_cost
= c.use.server_tool_use
server_tool_use = server_tool_use.web_search_requests * server_tool_pricing['web_search_requests'] / 1e3
server_tool_cost return inp_cost, out_cost, cache_cost, cache_w + cache_r, server_tool_cost
The markdown repr of the client itself will show the latest result, along with the usage so far.
Exported source
@patch
def _repr_markdown_(self:Client):
if not hasattr(self,'result'): return 'No results yet'
= contents(self.result)
msg = get_costs(self)
inp_cost, out_cost, cache_cost, cached_toks, server_tool_cost return f"""{msg}
| Metric | Count | Cost (USD) |
|--------|------:|-----:|
| Input tokens | {self.use.input_tokens:,} | {inp_cost:.6f} |
| Output tokens | {self.use.output_tokens:,} | {out_cost:.6f} |
| Cache tokens | {cached_toks:,} | {cache_cost:.6f} |
| Server tool use | {self.use.server_tool_use.web_search_requests:,} | {server_tool_cost:.6f} |
| **Total** | **{self.use.total:,}** | **${self.cost:.6f}** |"""
c
1, 2,
Metric | Count | Cost (USD) |
---|---|---|
Input tokens | 94 | 0.000282 |
Output tokens | 150 | 0.002250 |
Cache tokens | 0 | 0.000000 |
Server tool use | 0 | 0.000000 |
Total | 244 | $0.002532 |
Tool use
Let’s now look more at tool use (aka function calling).
For testing, we need a function that Claude can call; we’ll write a simple function that adds numbers together, and will tell us when it’s being called:
from dataclasses import dataclass
@dataclass
class MySum: val:int
def sums(
int, # First thing to sum
a:int=1 # Second thing to sum
b:-> int: # The sum of the inputs
) "Adds a + b."
print(f"Finding the sum of {a} and {b}")
return MySum(a + b)
= 604542,6458932
a,b = f"What is {a}+{b}?"
pr = "Always use tools when calculations are required." sp
Claudette can autogenerate a schema thanks to the toolslm
library. We’ll force the use of the tool using the function we created earlier.
=[get_schema(sums)]
tools= mk_tool_choice('sums') choice
We’ll start a dialog with Claude now. We’ll store the messages of our dialog in msgs
. The first message will be our prompt pr
, and we’ll pass our tools
schema.
= mk_msgs(pr)
msgs = c(msgs, sp=sp, tools=tools, tool_choice=choice)
r r
ToolUseBlock(id=‘toolu_01KcweRwZRjqgKidBaWPti38’, input={‘a’: 604542, ‘b’: 6458932}, name=‘sums’, type=‘tool_use’)
- id:
msg_014vRxDWsGhvdUYZiDsfaxYz
- content:
[{'id': 'toolu_01KcweRwZRjqgKidBaWPti38', 'input': {'a': 604542, 'b': 6458932}, 'name': 'sums', 'type': 'tool_use'}]
- model:
claude-sonnet-4-20250514
- role:
assistant
- stop_reason:
tool_use
- stop_sequence:
None
- type:
message
- usage:
{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 440, 'output_tokens': 57, 'server_tool_use': None, 'service_tier': 'standard'}
When Claude decides that it should use a tool, it passes back a ToolUseBlock
with the name of the tool to call, and the params to use.
We don’t want to allow it to call just any possible function (that would be a security disaster!) so we create a namespace – that is, a dictionary of allowable function names to call.
= mk_ns(sums)
ns ns
{'sums': <function __main__.sums(a: int, b: int = 1) -> int>}
mk_funcres
mk_funcres (fc, ns)
Given tool use block fc
, get tool result, and create a tool_result response.
Exported source
def mk_funcres(fc, ns):
"Given tool use block `fc`, get tool result, and create a tool_result response."
= call_func(fc.name, fc.input, ns=ns, raise_on_err=False)
res return dict(type="tool_result", tool_use_id=fc.id, content=str(res))
We can now use the function requested by Claude. We look it up in ns
, and pass in the provided parameters.
= [o for o in r.content if isinstance(o,ToolUseBlock)]
fcs fcs
[ToolUseBlock(id='toolu_01KcweRwZRjqgKidBaWPti38', input={'a': 604542, 'b': 6458932}, name='sums', type='tool_use')]
= [mk_funcres(fc, ns=ns) for fc in fcs]
res res
Finding the sum of 604542 and 6458932
[{'type': 'tool_result',
'tool_use_id': 'toolu_01KcweRwZRjqgKidBaWPti38',
'content': 'MySum(val=7063474)'}]
def contents(r):
"Helper to get the contents from Claude response `r`."
= find_block(r)
blk if not blk and r.content: blk = r.content[0]
if hasattr(blk,'text'): return blk.text.strip()
elif hasattr(blk,'content'): return blk.content.strip()
return str(blk)
mk_toolres
mk_toolres (r:collections.abc.Mapping, ns:Optional[collections.abc.Mapping]=None, obj:Optional=None)
Create a tool_result
message from response r
.
Type | Default | Details | |
---|---|---|---|
r | Mapping | Tool use request response from Claude | |
ns | Optional | None | Namespace to search for tools |
obj | Optional | None | Class to search for tools |
Exported source
def mk_toolres(
# Tool use request response from Claude
r:abc.Mapping, =None, # Namespace to search for tools
ns:Optional[abc.Mapping]=None # Class to search for tools
obj:Optional
):"Create a `tool_result` message from response `r`."
= getattr(r, 'content', [])
cts = [mk_msg(r.model_dump(), role='assistant')]
res if ns is None: ns=globals()
if obj is not None: ns = mk_ns(obj)
= [mk_funcres(o, ns) for o in cts if isinstance(o,ToolUseBlock)]
tcs if tcs: res.append(mk_msg(tcs))
return res
In order to tell Claude the result of the tool call, we pass back the tool use assistant request and the tool_result
response.
= mk_toolres(r, ns=ns)
tr tr
Finding the sum of 604542 and 6458932
[{'role': 'assistant',
'content': [{'id': 'toolu_01LWYaCNpbxTcvQrqozwBmmy',
'input': {'a': 604542, 'b': 6458932},
'name': 'sums',
'type': 'tool_use'}]},
{'role': 'user',
'content': [{'type': 'tool_result',
'tool_use_id': 'toolu_01LWYaCNpbxTcvQrqozwBmmy',
'content': 'MySum(val=7063474)'}]}]
msgs
[{'role': 'user', 'content': 'What is 604542+6458932?'}]
We add this to our dialog, and now Claude has all the information it needs to answer our question.
+= tr
msgs =sp, tools=tools)) contents(c(msgs, sp
'604542 + 6458932 = 7,063,474'
-1]) contents(msgs[
'MySum(val=7063474)'
msgs
[{'role': 'user', 'content': 'What is 604542+6458932?'},
{'role': 'assistant',
'content': [{'id': 'toolu_01LWYaCNpbxTcvQrqozwBmmy',
'input': {'a': 604542, 'b': 6458932},
'name': 'sums',
'type': 'tool_use'}]},
{'role': 'user',
'content': [{'type': 'tool_result',
'tool_use_id': 'toolu_01LWYaCNpbxTcvQrqozwBmmy',
'content': 'MySum(val=7063474)'}]}]
This works with methods as well – in this case, use the object itself for ns
:
class Dummy:
def sums(
self,
int, # First thing to sum
a:int=1 # Second thing to sum
b:-> int: # The sum of the inputs
) "Adds a + b."
print(f"Finding the sum of {a} and {b}")
return a + b
= [get_schema(Dummy.sums)]
tools = Dummy()
o = c(pr, sp=sp, tools=tools, tool_choice=choice)
r = mk_toolres(r, obj=o)
tr += tr
msgs =sp, tools=tools)) contents(c(msgs, sp
Finding the sum of 604542 and 6458932
'604542 + 6458932 = 7,063,474'
Text editing
Anthropic also has a special tool type specific to text editing.
= [text_editor_conf['sonnet']]
tools tools
[{'type': 'text_editor_20250429', 'name': 'str_replace_based_edit_tool'}]
= 'Could you please explain my _quarto.yml file?'
pr = [mk_msg(pr)]
msgs = c(msgs, sp=sp, tools=tools)
r find_block(r, ToolUseBlock)
ToolUseBlock(id='toolu_01Bspz19pw7VfRtEvU3V8TaD', input={'command': 'view', 'path': '_quarto.yml'}, name='str_replace_based_edit_tool', type='tool_use')
We’ve gone ahead and create a reference implementation that you can directly use from our text_editor
module. Or use as reference for creating your own.
= mk_ns(str_replace_based_edit_tool)
ns = mk_toolres(r, ns=ns)
tr += tr
msgs print(contents(c(msgs, sp=sp, tools=tools))[:128])
Great! Let me explain your `_quarto.yml` configuration file section by section:
## Project Configuration
```yaml
project:
typ
Structured data
= 604542,6458932
a,b = f"What is {a}+{b}?"
pr = "Always use your tools for calculations." sp
for tools in [sums, [get_schema(sums)]]:
= c(pr, tools=tools, tool_choice='sums')
r print(r)
Message(id='msg_01RgJQknYcS9EoqxqtYMaERQ', content=[ToolUseBlock(id='toolu_01D8Rdf8dtWDQjGtjk1BffVc', input={'a': 604542, 'b': 6458932}, name='sums', type='tool_use')], model='claude-sonnet-4-20250514', role='assistant', stop_reason='tool_use', stop_sequence=None, type='message', usage=In: 435; Out: 53; Cache create: 0; Cache read: 0; Total Tokens: 488; Search: 0)
Message(id='msg_014MmpmDr5q5RNmAv61pqhKn', content=[ToolUseBlock(id='toolu_01L97z6G6ZCThZNjGFRpkug8', input={'a': 604542, 'b': 6458932}, name='sums', type='tool_use')], model='claude-sonnet-4-20250514', role='assistant', stop_reason='tool_use', stop_sequence=None, type='message', usage=In: 435; Out: 53; Cache create: 0; Cache read: 0; Total Tokens: 488; Search: 0)
= mk_ns(sums)
ns = mk_toolres(r, ns=ns) tr
Finding the sum of 604542 and 6458932
Client.structured
Client.structured (msgs:list, tools:Optional[list]=None, obj:Optional=None, ns:Optional[collections.abc.Mapping]=None, sp='', temp=0, maxtok=4096, maxthinktok=0, prefill='', stream:bool=False, stop=None, tool_choice:Optional[dict]=None, cb=None, metadata:MetadataParam|NotGiven=NOT_GIVEN, service_tie r:"Literal['auto','standard_only']|NotGiven"=NOT_GIVEN , stop_sequences:List[str]|NotGiven=NOT_GIVEN, system: Union[str,Iterable[TextBlockParam]]|NotGiven=NOT_GIVEN , temperature:float|NotGiven=NOT_GIVEN, thinking:ThinkingConfigParam|NotGiven=NOT_GIVEN, top_k:int|NotGiven=NOT_GIVEN, top_p:float|NotGiven=NOT_GIVEN, extra_headers:Headers|None=None, extra_query:Query|None=None, extra_body:Body|None=None, timeout:float|httpx.Timeout|None|NotGiven=NOT_GIVEN)
Return the value of all tool calls (generally used for structured outputs)
Type | Default | Details | |
---|---|---|---|
msgs | list | List of messages in the dialog | |
tools | Optional | None | List of tools to make available to Claude |
obj | Optional | None | Class to search for tools |
ns | Optional | None | Namespace to search for tools |
sp | str | The system prompt | |
temp | int | 0 | Temperature |
maxtok | int | 4096 | Maximum tokens |
maxthinktok | int | 0 | Maximum thinking tokens |
prefill | str | Optional prefill to pass to Claude as start of its response | |
stream | bool | False | Stream response? |
stop | NoneType | None | Stop sequence |
tool_choice | Optional | None | Optionally force use of some tool |
cb | NoneType | None | Callback to pass result to when complete |
metadata | MetadataParam | NotGiven | NOT_GIVEN | |
service_tier | Literal[‘auto’, ‘standard_only’] | NotGiven | NOT_GIVEN | |
stop_sequences | List[str] | NotGiven | NOT_GIVEN | |
system | Union[str, Iterable[TextBlockParam]] | NotGiven | NOT_GIVEN | |
temperature | float | NotGiven | NOT_GIVEN | |
thinking | ThinkingConfigParam | NotGiven | NOT_GIVEN | |
top_k | int | NotGiven | NOT_GIVEN | |
top_p | float | NotGiven | NOT_GIVEN | |
extra_headers | Optional | None | Use the following arguments if you need to pass additional parameters to the API that aren’t available via kwargs. The extra values given here take precedence over values defined on the client or passed to this method. |
extra_query | Query | None | None | |
extra_body | Body | None | None | |
timeout | float | httpx.Timeout | None | NotGiven | NOT_GIVEN |
Exported source
@patch
@delegates(Client.__call__)
def structured(self:Client,
list, # List of messages in the dialog
msgs:list]=None, # List of tools to make available to Claude
tools:Optional[=None, # Class to search for tools
obj:Optional=None, # Namespace to search for tools
ns:Optional[abc.Mapping]**kwargs):
"Return the value of all tool calls (generally used for structured outputs)"
= listify(tools)
tools = self(msgs, tools=tools, tool_choice=tools, **kwargs)
res if ns is None: ns=mk_ns(*tools)
if obj is not None: ns = mk_ns(obj)
= getattr(res, 'content', [])
cts = [call_func(o.name, o.input, ns=ns) for o in cts if isinstance(o,ToolUseBlock)]
tcs return tcs
Anthropic’s API does not support response formats directly, so instead we provide a structured
method to use tool calling to achieve the same result. The result of the tool is not passed back to Claude in this case, but instead is returned directly to the user.
=[sums]) c.structured(pr, tools
Finding the sum of 604542 and 6458932
[MySum(val=7063474)]
c
ToolUseBlock(id=‘toolu_01NcbS7eJXHEAe3cr2pBEUd5’, input={‘a’: 604542, ‘b’: 6458932}, name=‘sums’, type=‘tool_use’)
Metric | Count | Cost (USD) |
---|---|---|
Input tokens | 5,978 | 0.017934 |
Output tokens | 1,283 | 0.019245 |
Cache tokens | 0 | 0.000000 |
Server tool use | 0 | 0.000000 |
Total | 7,261 | $0.037179 |
Custom Types with Tools Use
We need to add tool support for custom types too. Let’s test out custom types using a minimal example.
class Book(BasicRepr):
def __init__(self, title: str, pages: int): store_attr()
def __repr__(self):
return f"Book Title : {self.title}\nNumber of Pages : {self.pages}"
"War and Peace", 950) Book(
Book Title : War and Peace
Number of Pages : 950
def find_page(book: Book, # The book to find the halfway point of
int, # Percent of a book to read to, e.g. halfway == 50,
percent: -> int:
) "The page number corresponding to `percent` completion of a book"
return round(book.pages * (percent / 100.0))
get_schema(find_page)
{'name': 'find_page',
'description': 'The page number corresponding to `percent` completion of a book\n\nReturns:\n- type: integer',
'input_schema': {'type': 'object',
'properties': {'book': {'type': 'object',
'description': 'The book to find the halfway point of',
'$ref': '#/$defs/Book'},
'percent': {'type': 'integer',
'description': 'Percent of a book to read to, e.g. halfway == 50,'}},
'title': None,
'required': ['book', 'percent'],
'$defs': {'Book': {'type': 'object',
'properties': {'title': {'type': 'string', 'description': ''},
'pages': {'type': 'integer', 'description': ''}},
'title': 'Book',
'required': ['title', 'pages']}}}}
= mk_tool_choice('find_page')
choice choice
{'type': 'tool', 'name': 'find_page'}
Claudette will pack objects as dict, so we’ll transform tool functions with user-defined types into tool functions that accept a dict in lieu of the user-defined type.
First let’s convert a single argument:
_is_builtin
decides whether to pass an argument through as-is. Let’s check the argument conversion:
int), _is_builtin(Book), _is_builtin(List)) (_is_builtin(
(True, False, True)
555, int),
(_convert("title": "War and Peace", "pages": 923}, Book),
_convert({1, 2, 3, 4], List)) _convert([
(555,
Book Title : War and Peace
Number of Pages : 923,
[1, 2, 3, 4])
To apply tool()
to a function is to return a new function where the user-defined types are replaced with dictionary inputs.
tool
tool (func)
A function is transformed into a function with dict arguments substituted for user-defined types. Built-in types such as percent
here are left untouched.
=Book("War and Peace", 950), percent=50) find_page(book
475
"title": "War and Peace", "pages": 950}, percent=50) tool(find_page)({
475
By passing tools wrapped by tool()
, user-defined types now work completes without failing in tool calls.
= "How many pages do I have to read to get halfway through my 950 page copy of War and Peace"
pr = tool(find_page)
tools tools
<function __main__.find_page(book: __main__.Book, percent: int) -> int>
= c(pr, tools=[tools])
r find_block(r, ToolUseBlock)
ToolUseBlock(id='toolu_01BWLxyj4oCdpAdSWpXtKR8w', input={'book': {'title': 'War and Peace', 'pages': 950}, 'percent': 50}, name='find_page', type='tool_use')
= mk_toolres(r, ns=[tools])
tr tr
[{'role': 'assistant',
'content': [{'citations': None,
'text': "I'll help you find the halfway point of your copy of War and Peace.",
'type': 'text'},
{'id': 'toolu_01BWLxyj4oCdpAdSWpXtKR8w',
'input': {'book': {'title': 'War and Peace', 'pages': 950}, 'percent': 50},
'name': 'find_page',
'type': 'tool_use'}]},
{'role': 'user',
'content': [{'type': 'tool_result',
'tool_use_id': 'toolu_01BWLxyj4oCdpAdSWpXtKR8w',
'content': '475'}]}]
= [pr]+tr
msgs =sp, tools=[tools])) contents(c(msgs, sp
"To get halfway through your 950-page copy of War and Peace, you need to read to page 475. That means you'll have 475 pages to go to reach the halfway point!"
Chat
Rather than manually adding the responses to a dialog, we’ll create a simple Chat
class to do that for us, each time we make a request. We’ll also store the system prompt and tools here, to avoid passing them every time.
Chat
Chat (model:Optional[str]=None, cli:Optional[__main__.Client]=None, sp='', tools:Optional[list]=None, temp=0, cont_pr:Optional[str]=None, cache:bool=False, hist:list=None, ns:Optional[collections.abc.Mapping]=None)
Anthropic chat client.
Type | Default | Details | |
---|---|---|---|
model | Optional | None | Model to use (leave empty if passing cli ) |
cli | Optional | None | Client to use (leave empty if passing model ) |
sp | str | Optional system prompt | |
tools | Optional | None | List of tools to make available to Claude |
temp | int | 0 | Temperature |
cont_pr | Optional | None | User prompt to continue an assistant response |
cache | bool | False | Use Claude cache? |
hist | list | None | Initialize history |
ns | Optional | None | Namespace to search for tools |
The class stores the Client
that will provide the responses in c
, and a history of messages in h
.
= "Never mention what tools you use."
sp = Chat(model, sp=sp)
chat chat.c.use, chat.h
(In: 0; Out: 0; Cache create: 0; Cache read: 0; Total Tokens: 0; Search: 0, [])
chat.c.use.cost(pricing[model_types[chat.c.model]])
0.0
This is clunky. Let’s add cost
as a property for the Chat
class. It will pass in the appropriate prices for the current model to the usage cost calculator.
Chat.cost
Chat.cost ()
Exported source
@patch(as_prop=True)
def cost(self: Chat) -> float: return self.c.cost
chat.cost
0.0
Chat.__call__
Chat.__call__ (pr=None, temp=None, maxtok=4096, maxthinktok=0, stream=False, prefill='', tool_choice:Optional[dict]=None, **kw)
Call self as a function.
Type | Default | Details | |
---|---|---|---|
pr | NoneType | None | Prompt / message |
temp | NoneType | None | Temperature |
maxtok | int | 4096 | Maximum tokens |
maxthinktok | int | 0 | Maximum thinking tokens |
stream | bool | False | Stream response? |
prefill | str | Optional prefill to pass to Claude as start of its response | |
tool_choice | Optional | None | Optionally force use of some tool |
kw | VAR_KEYWORD |
Exported source
@patch
def _post_pr(self:Chat, pr, prev_role):
if pr is None and prev_role == 'assistant':
if self.cont_pr is None:
raise ValueError("Prompt must be given after completion, or use `self.cont_pr`.")
= self.cont_pr # No user prompt, keep the chain
pr if pr: self.h.append(mk_msg(pr, cache=self.cache))
Exported source
@patch
def _append_pr(self:Chat, pr=None):
= nested_idx(self.h, -1, 'role') if self.h else 'assistant' # First message should be 'user'
prev_role if pr and prev_role == 'user': self() # already user request pending
self._post_pr(pr, prev_role)
Exported source
@patch
def __call__(self:Chat,
=None, # Prompt / message
pr=None, # Temperature
temp=4096, # Maximum tokens
maxtok=0, # Maximum thinking tokens
maxthinktok=False, # Stream response?
stream='', # Optional prefill to pass to Claude as start of its response
prefilldict]=None, # Optionally force use of some tool
tool_choice:Optional[**kw):
if temp is None: temp=self.temp
self._append_pr(pr)
def _cb(v):
self.last = mk_toolres(v, ns=self.ns)
self.h += self.last
return self.c(self.h, stream=stream, prefill=prefill, sp=self.sp, temp=temp, maxtok=maxtok, maxthinktok=maxthinktok,
=self.tools, tool_choice=tool_choice, cb=_cb, **kw) tools
The __call__
method just passes the request along to the Client
, but rather than just passing in this one prompt, it appends it to the history and passes it all along. As a result, we now have state!
= Chat(model, sp=sp) chat
"I'm Jeremy")
chat("What's my name?") chat(
Your name is Jeremy.
- id:
msg_01VYhPmDt1MFCMthfYfTjenC
- content:
[{'citations': None, 'text': 'Your name is Jeremy.', 'type': 'text'}]
- model:
claude-sonnet-4-20250514
- role:
assistant
- stop_reason:
end_turn
- stop_sequence:
None
- type:
message
- usage:
{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 42, 'output_tokens': 8, 'server_tool_use': None, 'service_tier': 'standard'}
chat.use, chat.cost
(In: 59; Out: 25; Cache create: 0; Cache read: 0; Total Tokens: 84; Search: 0,
0.000552)
Let’s try out prefill too:
= "Very concisely, what is the meaning of life?"
q = 'According to Douglas Adams,' pref
=pref) chat(q, prefill
According to Douglas Adams,42. But seriously: to find purpose, connect with others, and create meaning through your choices and relationships.
- id:
msg_015VCyp9iXg1RYZEi9MvZnb3
- content:
[{'citations': None, 'text': 'According to Douglas Adams,42. But seriously: to find purpose, connect with others, and create meaning through your choices and relationships.', 'type': 'text'}]
- model:
claude-sonnet-4-20250514
- role:
assistant
- stop_reason:
end_turn
- stop_sequence:
None
- type:
message
- usage:
{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 70, 'output_tokens': 25, 'server_tool_use': None, 'service_tier': 'standard'}
By default messages must be in user, assistant, user format. If this isn’t followed (aka calling chat()
without a user message) it will error out:
try: chat()
except ValueError as e: print("Error:", e)
Error: Prompt must be given after completion, or use `self.cont_pr`.
Setting cont_pr
allows a “default prompt” to be specified when a prompt isn’t specified. Usually used to prompt the model to continue.
= "Tell me a little more..."
chat.cont_pr chat()
The meaning of life is deeply personal and has been pondered for millennia. Some common threads:
Purpose: Finding what gives your life direction - whether through work, creativity, service, or personal growth.
Connection: Building meaningful relationships and contributing to something larger than yourself.
Experience: Embracing both joy and struggle as part of the human condition.
Growth: Continuously learning, evolving, and becoming more of who you’re meant to be.
Legacy: How you impact others and what you leave behind.
Many find meaning through religion, philosophy, family, art, or simply in small daily acts of kindness. The beauty might be that there’s no single answer - you get to discover and create your own meaning as you live.
What resonates most with you, Jeremy?
- id:
msg_01P9isdM7LhwLFSJvjT4W6r1
- content:
[{'citations': None, 'text': "The meaning of life is deeply personal and has been pondered for millennia. Some common threads:\n\n**Purpose**: Finding what gives your life direction - whether through work, creativity, service, or personal growth.\n\n**Connection**: Building meaningful relationships and contributing to something larger than yourself.\n\n**Experience**: Embracing both joy and struggle as part of the human condition.\n\n**Growth**: Continuously learning, evolving, and becoming more of who you're meant to be.\n\n**Legacy**: How you impact others and what you leave behind.\n\nMany find meaning through religion, philosophy, family, art, or simply in small daily acts of kindness. The beauty might be that there's no single answer - you get to discover and create your own meaning as you live.\n\nWhat resonates most with you, Jeremy?", 'type': 'text'}]
- model:
claude-sonnet-4-20250514
- role:
assistant
- stop_reason:
end_turn
- stop_sequence:
None
- type:
message
- usage:
{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 104, 'output_tokens': 172, 'server_tool_use': None, 'service_tier': 'standard'}
We can also use streaming:
= Chat(model, sp=sp)
chat for o in chat("I'm Jeremy", stream=True): print(o, end='')
Hi Jeremy! Nice to meet you. How are you doing today?
= chat(q, prefill=pref, stream=True)
r for o in r: print(o, end='')
r.value
According to Douglas Adams,it's 42. But seriously: to find purpose through connection, growth, and contributing something meaningful to the world.
According to Douglas Adams,it’s 42. But seriously: to find purpose through connection, growth, and contributing something meaningful to the world.
- id:
msg_01UgH5ioXxS4KznbFLjtPnrY
- content:
[{'citations': None, 'text': "According to Douglas Adams,it's 42. But seriously: to find purpose through connection, growth, and contributing something meaningful to the world.", 'type': 'text'}]
- model:
claude-sonnet-4-20250514
- role:
assistant
- stop_reason:
end_turn
- stop_sequence:
None
- type:
message
- usage:
{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 54, 'output_tokens': 27, 'server_tool_use': None, 'service_tier': 'standard'}
You can provide a history of messages to initialise Chat
with:
= Chat(model, sp=sp, hist=["Can you guess my name?", "Hmmm I really don't know. Is it 'Merlin G. Penfolds'?"])
chat 'Wow how did you know?') chat(
I have to admit - I was just making a playful, completely random guess! I actually have no way of knowing your real name since we just started chatting. I was being a bit silly with that very specific (and probably fictional-sounding) name.
What’s your actual name, if you don’t mind me asking? Or if you’d prefer to keep some mystery, that’s totally fine too!
- id:
msg_014WDDynWpKPERFgg7bEjqBQ
- content:
[{'citations': None, 'text': "I have to admit - I was just making a playful, completely random guess! I actually have no way of knowing your real name since we just started chatting. I was being a bit silly with that very specific (and probably fictional-sounding) name. \n\nWhat's your actual name, if you don't mind me asking? Or if you'd prefer to keep some mystery, that's totally fine too!", 'type': 'text'}]
- model:
claude-sonnet-4-20250514
- role:
assistant
- stop_reason:
end_turn
- stop_sequence:
None
- type:
message
- usage:
{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 58, 'output_tokens': 89, 'server_tool_use': None, 'service_tier': 'standard'}
Chat tool use
We automagically get streamlined tool use as well:
= f"What is {a}+{b}?"
pr pr
'What is 604542+6458932?'
= Chat(model, sp=sp, tools=[sums])
chat = chat(pr)
r r
Finding the sum of 604542 and 6458932
ToolUseBlock(id=‘toolu_01N5Bgew7D22HofErCSBUB4u’, input={‘a’: 604542, ‘b’: 6458932}, name=‘sums’, type=‘tool_use’)
- id:
msg_01SSejjTbpTtqEzMVxiEb3T9
- content:
[{'id': 'toolu_01N5Bgew7D22HofErCSBUB4u', 'input': {'a': 604542, 'b': 6458932}, 'name': 'sums', 'type': 'tool_use'}]
- model:
claude-sonnet-4-20250514
- role:
assistant
- stop_reason:
tool_use
- stop_sequence:
None
- type:
message
- usage:
{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 437, 'output_tokens': 72, 'server_tool_use': None, 'service_tier': 'standard'}
Now we need to send this result to Claude—calling the object with no parameters tells it to return the tool result to Claude:
chat()
604542 + 6458932 = 7,063,474
- id:
msg_01MaKaz28ozKkfZtMsPVGQRV
- content:
[{'citations': None, 'text': '604542 + 6458932 = 7,063,474', 'type': 'text'}]
- model:
claude-sonnet-4-20250514
- role:
assistant
- stop_reason:
end_turn
- stop_sequence:
None
- type:
message
- usage:
{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 530, 'output_tokens': 19, 'server_tool_use': None, 'service_tier': 'standard'}
It should be correct, because it actually used our Python function to do the addition. Let’s check:
+b a
7063474
Let’s try the same thing with streaming:
= Chat(model, sp=sp, tools=[sums])
chat = chat(pr, stream=True)
r for o in r: print(o, end='')
Finding the sum of 604542 and 6458932
The full message, including tool call details, are in value
:
r.value
ToolUseBlock(id=‘toolu_016trCR1JH8k8szz9StcSpyC’, input={‘a’: 604542, ‘b’: 6458932}, name=‘sums’, type=‘tool_use’)
- id:
msg_01AAw9hyATYn5uexjoTHofqw
- content:
[{'id': 'toolu_016trCR1JH8k8szz9StcSpyC', 'input': {'a': 604542, 'b': 6458932}, 'name': 'sums', 'type': 'tool_use'}]
- model:
claude-sonnet-4-20250514
- role:
assistant
- stop_reason:
tool_use
- stop_sequence:
None
- type:
message
- usage:
{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 437, 'output_tokens': 72, 'server_tool_use': None, 'service_tier': 'standard'}
= chat(stream=True)
r for o in r: print(o, end='')
604542 + 6458932 = 7,063,474
r.value
604542 + 6458932 = 7,063,474
- id:
msg_01RGyUJM15hgyRG7sSbzuhJN
- content:
[{'citations': None, 'text': '604542 + 6458932 = 7,063,474', 'type': 'text'}]
- model:
claude-sonnet-4-20250514
- role:
assistant
- stop_reason:
end_turn
- stop_sequence:
None
- type:
message
- usage:
{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 530, 'output_tokens': 19, 'server_tool_use': None, 'service_tier': 'standard'}
The history shows both the tool_use and tool_result messages:
chat.h
[{'role': 'user', 'content': 'What is 604542+6458932?'},
{'role': 'assistant',
'content': [{'id': 'toolu_016trCR1JH8k8szz9StcSpyC',
'input': {'a': 604542, 'b': 6458932},
'name': 'sums',
'type': 'tool_use'}]},
{'role': 'user',
'content': [{'type': 'tool_result',
'tool_use_id': 'toolu_016trCR1JH8k8szz9StcSpyC',
'content': 'MySum(val=7063474)'}]},
{'role': 'assistant',
'content': [{'citations': None,
'text': '604542 + 6458932 = 7,063,474',
'type': 'text'}]}]
Let’s test a function with user defined types.
= Chat(model, sp=sp, tools=[find_page])
chat = chat("How many pages is three quarters of the way through my 80 page edition of Tao Te Ching?")
r r
ToolUseBlock(id=‘toolu_019xKYBWXvh8UYHjKXYJQwQx’, input={‘book’: {‘title’: ‘Tao Te Ching’, ‘pages’: 80}, ‘percent’: 75}, name=‘find_page’, type=‘tool_use’)
- id:
msg_01KSzFKwYFCNGY48u4t3Kxdj
- content:
[{'id': 'toolu_019xKYBWXvh8UYHjKXYJQwQx', 'input': {'book': {'title': 'Tao Te Ching', 'pages': 80}, 'percent': 75}, 'name': 'find_page', 'type': 'tool_use'}]
- model:
claude-sonnet-4-20250514
- role:
assistant
- stop_reason:
tool_use
- stop_sequence:
None
- type:
message
- usage:
{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 547, 'output_tokens': 86, 'server_tool_use': None, 'service_tier': 'standard'}
chat()
Three quarters of the way through your 80-page edition of Tao Te Ching would be page 60.
- id:
msg_01CxVMYi639cb4HWkhn5SCiB
- content:
[{'citations': None, 'text': 'Three quarters of the way through your 80-page edition of Tao Te Ching would be page 60.', 'type': 'text'}]
- model:
claude-sonnet-4-20250514
- role:
assistant
- stop_reason:
end_turn
- stop_sequence:
None
- type:
message
- usage:
{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 647, 'output_tokens': 29, 'server_tool_use': None, 'service_tier': 'standard'}
Exported source
@patch
def _repr_markdown_(self:Chat):
if not hasattr(self.c, 'result'): return 'No results yet'
= contents(self.c.result)
last_msg
def fmt_msg(m):
= contents(m)
t if isinstance(t, dict): return t['content']
return t
= '\n\n'.join(f"**{m['role']}**: {fmt_msg(m)}"
history for m in self.h)
= self.c._repr_markdown_().split('\n\n')[-1]
det if history: history = f"""
<details>
<summary>► History</summary>
{history}
</details>
"""
return f"""{last_msg}
{history}
{det}"""
# TODO: fix history format
chat
Three quarters of the way through your 80-page edition of Tao Te Ching would be page 60.
► History
user: H
assistant: {‘id’: ‘toolu_019xKYBWXvh8UYHjKXYJQwQx’, ‘input’: {‘book’: {‘title’: ‘Tao Te Ching’, ‘pages’: 80}, ‘percent’: 75}, ‘name’: ‘find_page’, ‘type’: ‘tool_use’}
user: 60
assistant: Three quarters of the way through your 80-page edition of Tao Te Ching would be page 60.
Metric | Count | Cost (USD) |
---|---|---|
Input tokens | 1,194 | 0.003582 |
Output tokens | 115 | 0.001725 |
Cache tokens | 0 | 0.000000 |
Server tool use | 0 | 0.000000 |
Total | 1,309 | $0.005307 |
= Chat(model, tools=[text_editor_conf['sonnet']], ns=mk_ns(str_replace_based_edit_tool)) chat
When not providing tools directly as Python functions (like sum
), you must create and pass a namespace dictionary (mapping the tool name string to the function object) using the ns
parameter to methods like mk_toolres
or toolloop
. toolslm
cannot automatically generate the namespace in this case. For schema-based tools (i.e., Python functions), claudette
handles namespace creation automatically.
= chat('Please explain very concisely what my _quarto.yml does. It is in the current path. Use your tools')
r find_block(r, ToolUseBlock)
ToolUseBlock(id='toolu_012hczQGy49DqnMPYdN2wVr8', input={'command': 'view', 'path': '_quarto.yml'}, name='str_replace_based_edit_tool', type='tool_use')
chat()
Your _quarto.yml
configures a Quarto website with:
- Website type with custom preview on port 3000
- HTML styling: Cosmo theme, custom CSS, table of contents, code tools, and wide layout (1800px body)
- Navigation: Primary navbar with search, floating sidebar
- Social: Twitter cards and Open Graph enabled
- Resources: Includes .txt files and metadata from nbdev.yml/sidebar.yml
- Output: Keeps markdown files and supports CommonMark format
It’s set up for a documentation/blog website with code-friendly features and social media integration.
- id:
msg_01FcSdGbwJGsbkTQkksqcjn4
- content:
[{'citations': None, 'text': "Your
_quarto.ymlconfigures a Quarto website with:\n\n- **Website type** with custom preview on port 3000\n- **HTML styling**: Cosmo theme, custom CSS, table of contents, code tools, and wide layout (1800px body)\n- **Navigation**: Primary navbar with search, floating sidebar\n- **Social**: Twitter cards and Open Graph enabled\n- **Resources**: Includes .txt files and metadata from nbdev.yml/sidebar.yml\n- **Output**: Keeps markdown files and supports CommonMark format\n\nIt's set up for a documentation/blog website with code-friendly features and social media integration.", 'type': 'text'}]
- model:
claude-sonnet-4-20250514
- role:
assistant
- stop_reason:
end_turn
- stop_sequence:
None
- type:
message
- usage:
{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 1443, 'output_tokens': 144, 'server_tool_use': None, 'service_tier': 'standard'}
Images
Claude can handle image data as well. As everyone knows, when testing image APIs you have to use a cute puppy.
# Image is Cute_dog.jpg from Wikimedia
= Path('samples/puppy.jpg')
fn =fn, width=200) Image(filename
= fn.read_bytes() img
Claude expects an image message to have the following structure
{'role': 'user',
'content': [
'type':'text', 'text':'What is in the image?'},
{
{'type':'image',
'source': {
'type':'base64', 'media_type':'media_type', 'data': 'data'
}
}
] }
msglm
automatically detects if a message is an image, encodes it, and generates the data structure above. All we need to do is a create a list containing our image and a query and then pass it to mk_msg
.
Let’s try it out…
= "In brief, what color flowers are in this image?"
q = mk_msg([img, q]) msg
c([msg])
The flowers in this image are purple.
- id:
msg_01CcJPzhr8iSdKbJXhRr73gc
- content:
[{'citations': None, 'text': 'The flowers in this image are purple.', 'type': 'text'}]
- model:
claude-sonnet-4-20250514
- role:
assistant
- stop_reason:
end_turn
- stop_sequence:
None
- type:
message
- usage:
{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 110, 'output_tokens': 11, 'server_tool_use': None, 'service_tier': 'standard'}
You don’t need to call mk_msg
on each individual message before passing them to the Chat
class. Instead you can pass your messages in a list and the Chat
class will automatically call mk_msgs
in the background.
"How are you?", r]) c([
For messages that contain multiple content types (like an image with a question), you’ll need to enclose the message contents in a list as shown below:
"How are you?", r, [img, q]]) c([
= Chat(model)
c c([img, q])
The flowers in this image are purple.
- id:
msg_01AtaM1NYemMdksP7WqzKZZn
- content:
[{'citations': None, 'text': 'The flowers in this image are purple.', 'type': 'text'}]
- model:
claude-sonnet-4-20250514
- role:
assistant
- stop_reason:
end_turn
- stop_sequence:
None
- type:
message
- usage:
{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 110, 'output_tokens': 11, 'server_tool_use': None, 'service_tier': 'standard'}
def contents(r):
"Helper to get the contents from Claude response `r`."
= find_block(r)
blk if not blk and r.content: blk = r.content[0]
if hasattr(blk,'text'): return blk.text.strip()
elif hasattr(blk,'content'): return blk.content.strip()
elif hasattr(blk,'source'): return f'*Media Type - {blk.type}*'
return str(blk)
0]) contents(c.h[
'*Media Type - image*'
c
The flowers in this image are purple.
► History
user: Media Type - image
assistant: The flowers in this image are purple.
Metric | Count | Cost (USD) |
---|---|---|
Input tokens | 110 | 0.000330 |
Output tokens | 11 | 0.000165 |
Cache tokens | 0 | 0.000000 |
Server tool use | 0 | 0.000000 |
Total | 121 | $0.000495 |
Unfortunately, not all Claude models support images 😞. This table summarizes the capabilities of each Claude model and the different modalities they support.
Caching
Claude supports context caching by adding a cache_control
header to the message content.
{"role": "user",
"content": [
{"type": "text",
"text": "Please cache my message",
"cache_control": {"type": "ephemeral"}
}
] }
To cache a message, we simply set cache=True
when calling mk_msg
.
'hi', 'there'], cache=True) mk_msg([
{ 'content': [ {'text': 'hi', 'type': 'text'},
{ 'cache_control': {'type': 'ephemeral'},
'text': 'there',
'type': 'text'}],
'role': 'user'}
Claude also now supports smart cache look-ups, so it’s very simple to keep an entire conversation in cache by constantly telling it to update the cache with the latest message. To do this, we just need to set cache=True
when creating a Chat
.
= Chat(model, sp=sp, cache=True) chat
Caching has a minimum token limit of 1024 tokens for Sonnet and Opus, and 2048 for Haiku. If your conversation is below this limit, it will not be cached.
"Hi, I'm Jeremy.") chat(
Hi Jeremy! Nice to meet you. How are you doing today?
- id:
msg_018Y8ogR6Jxa1XHxVtHt4oSg
- content:
[{'citations': None, 'text': 'Hi Jeremy! Nice to meet you. How are you doing today?', 'type': 'text'}]
- model:
claude-sonnet-4-20250514
- role:
assistant
- stop_reason:
end_turn
- stop_sequence:
None
- type:
message
- usage:
{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 20, 'output_tokens': 17, 'server_tool_use': None, 'service_tier': 'standard'}
chat.use
In: 20; Out: 17; Cache create: 0; Cache read: 0; Total Tokens: 37; Search: 0
Note the usage: no cache is created, nor used. Now, let’s send a long enough message to trigger caching.
"""Lorem ipsum dolor sit amet""" * 150) chat(
I see you’ve sent a very long block of “Lorem ipsum” placeholder text! That’s quite a wall of text there, Jeremy.
Are you testing something, or did you perhaps copy and paste that by accident? I’m happy to help with whatever you actually wanted to discuss or ask about.
- id:
msg_01YEYg2chSwatgZJLuUctKQu
- content:
[{'citations': None, 'text': 'I see you\'ve sent a very long block of "Lorem ipsum" placeholder text! That\'s quite a wall of text there, Jeremy. \n\nAre you testing something, or did you perhaps copy and paste that by accident? I\'m happy to help with whatever you actually wanted to discuss or ask about.', 'type': 'text'}]
- model:
claude-sonnet-4-20250514
- role:
assistant
- stop_reason:
end_turn
- stop_sequence:
None
- type:
message
- usage:
{'cache_creation_input_tokens': 1084, 'cache_read_input_tokens': 0, 'input_tokens': 4, 'output_tokens': 65, 'server_tool_use': None, 'service_tier': 'standard'}
chat.use
In: 24; Out: 82; Cache create: 1084; Cache read: 0; Total Tokens: 1190; Search: 0
The context is now long enough for cache to be used. All the conversation history has now been written to the temporary cache. Any subsequent message will read from it rather than re-processing the entire conversation history.
"Oh thank you! Sorry, my lorem ipsum generator got out of control!") chat(
No worries at all, Jeremy! Those lorem ipsum generators can definitely get a bit enthusiastic sometimes. It happens to the best of us - one minute you need a little placeholder text, and the next minute you’ve got enough to fill a small novel!
Is there something I can actually help you with today, or were you just testing things out?
- id:
msg_01X5hUf98WTBoTi3GL5nJdZz
- content:
[{'citations': None, 'text': "No worries at all, Jeremy! Those lorem ipsum generators can definitely get a bit enthusiastic sometimes. It happens to the best of us - one minute you need a little placeholder text, and the next minute you've got enough to fill a small novel!\n\nIs there something I can actually help you with today, or were you just testing things out?", 'type': 'text'}]
- model:
claude-sonnet-4-20250514
- role:
assistant
- stop_reason:
end_turn
- stop_sequence:
None
- type:
message
- usage:
{'cache_creation_input_tokens': 83, 'cache_read_input_tokens': 1084, 'input_tokens': 4, 'output_tokens': 75, 'server_tool_use': None, 'service_tier': 'standard'}
chat.use
In: 28; Out: 157; Cache create: 1167; Cache read: 1084; Total Tokens: 2436; Search: 0
Extended Thinking
Claude >=3.7 Sonnet & Opus have enhanced reasoning capabilities for complex tasks. See docs for more info.
We can enable extended thinking by passing a thinking
param with the following structure.
={ "type": "enabled", "budget_tokens": 16000 } thinking
When extended thinking is enabled a thinking block is included in the response as shown below.
{"content": [
{"type": "thinking",
"thinking": "To approach this, let's think about...",
"signature": "Imtakcjsu38219c0.eyJoYXNoIjoiYWJjM0NTY3fQ...."
,
}
{"type": "text",
"text": "Yes, there are infinitely many prime numbers such that..."
}
] }
Note: When thinking is enabled prefill
must be empty and the temp
must be 1.
think_md
think_md (txt, thk)
def contents(r, show_thk=True):
"Helper to get the contents from Claude response `r`."
= find_block(r)
blk if show_thk:
= find_block(r, blk_type=ThinkingBlock)
tk_blk if tk_blk: return think_md(blk.text.strip(), tk_blk.thinking.strip())
if not blk and r.content: blk = r.content[0]
if hasattr(blk,'text'): return blk.text.strip()
elif hasattr(blk,'content'): return blk.content.strip()
elif hasattr(blk,'source'): return f'*Media Type - {blk.type}*'
return str(blk)
Let’s call the model without extended thinking enabled.
= Chat(model) chat
"Write a sentence about Python!") chat(
Python is a versatile, high-level programming language known for its clean syntax and readability, making it popular for everything from web development and data science to artificial intelligence and automation.
- id:
msg_013oWtFFn4TiJeFQxEWt52Vb
- content:
[{'citations': None, 'text': 'Python is a versatile, high-level programming language known for its clean syntax and readability, making it popular for everything from web development and data science to artificial intelligence and automation.', 'type': 'text'}]
- model:
claude-sonnet-4-20250514
- role:
assistant
- stop_reason:
end_turn
- stop_sequence:
None
- type:
message
- usage:
{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 13, 'output_tokens': 40, 'server_tool_use': None, 'service_tier': 'standard'}
Now, let’s call the model with extended thinking enabled.
"Write a sentence about Python!", maxthinktok=1024) chat(
Python’s extensive library ecosystem and beginner-friendly design have made it one of the most widely-used programming languages for both newcomers learning to code and experienced developers building complex applications.
Thinking
The human is asking me to write a sentence about Python again. They might want a different sentence this time, or they might be testing to see if I give the same response. I should provide a different sentence about Python to be more helpful and show variety.- id:
msg_01Snj6bDh7rQnPomcPbHLqs5
- content:
[{'signature': 'EqwDCkYIBBgCKkAh6vNdPs/eVUbd9Dklf6cqK4EGSfxy7/QH8bonEhm144FNUiergY2L84NvVbvQj6JnwAcbGa+uZveEVR5lZ3KcEgwgmUKYajAqgfyry0IaDAmkcR0llYLQMFRX+CIwxhlpIjsDjNbhG4Bv59btuoRrYryh++3AkoEys91KR1/0W9ZAFkj0Ihh9EGAZ1XrUKpMCcMTG3KNxqwcd7Uv6rUiCTF8GUg3t+oFzYaFzVdMyRZ+9AJ6HWS5DmvhGkMIpn2MTN1ej5vM2FRZCZjnP1YaBl1fah3vVAyJFUt2mJB/gDRcGR0Z7GbWbmyrX/ckrLQxC6FgaYnY5yXRdZYFnJtLtczKXrHygNq2USs+H78rUq4rdoYT8kuk/dTsiDr0MmXRpvvf2K8hz2ygvujjj4E1FXGlwB5vOcIFxCmJnOH6nGJmXJVWNhXww904DANR1oQ2q8SPTkmo9w0AuT4vdepye0JqPvxqdDHXzXc/92hT2uZRLJj6vSjCiscdc/u0MegMmjc7i1ECk+JNZrcrVbFYgDxTFrd7m8RwkdbxMYsPCNHmunV4YAQ==', 'thinking': 'The human is asking me to write a sentence about Python again. They might want a different sentence this time, or they might be testing to see if I give the same response. I should provide a different sentence about Python to be more helpful and show variety.', 'type': 'thinking'}, {'citations': None, 'text': "Python's extensive library ecosystem and beginner-friendly design have made it one of the most widely-used programming languages for both newcomers learning to code and experienced developers building complex applications.", 'type': 'text'}]
- model:
claude-sonnet-4-20250514
- role:
assistant
- stop_reason:
end_turn
- stop_sequence:
None
- type:
message
- usage:
{'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 90, 'output_tokens': 101, 'server_tool_use': None, 'service_tier': 'standard'}
Server Tools and Web Search
The str_replace
special tool type is a client side tool, i.e., one where we provide the implementation. However, Anthropic also supports server side tools. The current one available is their search tool, which you can find the documentation for here. When provided as a tool to claude, claude can decide to search the web in order to answer or solve the task at hand.
search_conf
search_conf (max_uses:int=None, allowed_domains:list=None, blocked_domains:list=None, user_location:dict=None)
Little helper to create a search tool config
Similar to client side tools, you provide to the tools
argument in the anthropic api a non-schema dictionary with the tool’s name, type, and any additional metadata specific to that tool. Here’s a function to make that process easier for the web search tool.
search_conf()
{'type': 'web_search_20250305', 'name': 'web_search'}
The web search tool returns a list of TextBlock
s comprised of response text from the model, ServerToolUseBlock
and server tool results block such as WebSearchToolResultBlock
. Some of these TextBlock
s will contain citations with references to the results of the web search tool. Here is what all this looks like:
{"content": [
{"type": "text",
"text": "I'll check the current weather in...",
,
}
{"type": "server_tool_use",
"name": "web_search",
"input": {"query": "San Diego weather forecast today May 12 2025"},
"id":"srvtoolu_014t7fS449voTHRCVzi5jQGC"
,
}
{"type": "web_search_tool_result",
"tool_use_id": "srvtoolu_014t7fS449voTHRCVzi5jQGC",
"content": [
"type": "web_search_result",
"title": "Heat Advisory issued May 9...",
"url": "https://kesq.com/weather/...",
...
]
}
{"type": "text",
"citations": [
{"cited_text": 'The average temperature during this month...',
"title": "Weather San Diego in May 2025:...",
"url": "https://en.climate-data.org/...",
"encrypted_index": "EpMBCioIAxgCIiQ4ODk4YTF..."
},
]"text": "The average temperature in San Diego during May is..."
,
}...
] }
Let’s update our contents
function to handle these cases. For handling citations, we will use the excellent reference syntax in markdown to make clickable citation links.
find_blocks
find_blocks (r, blk_type=<class 'anthropic.types.text_block.TextBlock'>, type='text')
Helper to find all blocks of type blk_type
in response r
.
blks2cited_txt
blks2cited_txt (txt_blks)
Helper to get the contents from a list of TextBlock
s, with citations.
contents
contents (r, show_thk=True)
Helper to get the contents from Claude response r
.
= Chat(model, sp='Be concise in your responses.', tools=[search_conf()], cache=True)
chat = 'What is the weather in San Diego?'
pr = chat(pr)
r r
Based on the current weather information for San Diego, here’s what I found:
Today’s weather in San Diego is cooler with highs around 4-7 degrees below normal for most areas 1. Tomorrow (Saturday, June 21st) is expected to have a high of 67°F 2 3 with cloudy conditions early with partial sunshine expected later 4. Winds will be from the SSW at 10 to 15 mph 5.
Much cooler weather is expected on Saturday with highs as much as 10-15 degrees below normal inland 6. Tomorrow’s temperature is forecast to be cooler than today 7.
For air quality, the air has reached a high level of pollution and is unhealthy for sensitive groups, so it’s recommended to reduce time spent outside if experiencing symptoms like difficulty breathing or throat irritation 8.
- id:
msg_01KYWs2c5aou6Kgi7HH5X9Ex
- content:
[{'id': 'srvtoolu_01SEnDGkdbPEGTSfR9NaGNdG', 'input': {'query': 'San Diego weather today'}, 'name': 'web_search', 'type': 'server_tool_use'}, {'content': [{'encrypted_content': 'EqcCCioIBBgCIiQ4ODk4YTFkYy0yMTNkLTRhNmYtOTljYi03ZTBlNTUzZDc0NWISDI+BoColI20gEO0xOxoMdwUdOeATq0s0WrXWIjDQ25iRW01TJAc8+jr8ca9mh6iQP0itAnmcuGLQHvQcMJPIjWvQpfPqZWgqQnvEojkqqgEGce/serEpBbCiEWVeLMq8G2+eitamNOsSsTcHZRuUyYBs9IlV5LIsmoduIeI3Y3Vq3A8Cnv9VZ33uzTv+DoRO1JuU8+jaXZWx+Hrc0gntSgA5veSU/nnTzYtLAQcLRVMNdGUFeofLXg2Lp8+7mrSauXG1/hyVIxTvPRpKTG2gNF2sOsgsbk214lFZQTtt1DdrZ/mKAqw1sCf0godsw9CLYe+Mjt6p/i7AIBgD', 'page_age': '3 days ago', 'title': '10-Day Weather Forecast for San Diego, CA - The Weather Channel | weather.com', 'type': 'web_search_result', 'url': 'https://weather.com/weather/tenday/l/San+Diego+CA?canonicalCityId=3b2b39ed755b459b725bf2a29c71d678'}, {'encrypted_content': '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', 'page_age': '3 days ago', 'title': 'San Diego, CA Weather Forecast | AccuWeather', 'type': 'web_search_result', 'url': 'https://www.accuweather.com/en/us/san-diego/92101/weather-forecast/347628'}, {'encrypted_content': 'Er0ECioIBBgCIiQ4ODk4YTFkYy0yMTNkLTRhNmYtOTljYi03ZTBlNTUzZDc0NWISDMNHwA48kn3Z+pa+3xoMJLQxyZCLTZ/3iLijIjD5HxtIj8I+isYjK5VpVeDMZWNv8l4+dVkV8W5MMeOR3ny4EbZIDI7pYEZHtoaWxz0qwAPnm/af39sPOBZNYSLKk5RsL4pWjEAcmxi5DInEno6leMMsKDMUPmWvRuOfF9yVjDMQejCqjWcLTCy9fGXdBLIooToz5rskj4JdaHyq2pXsbUrDBGLDKLfKVM9sBc57oaGQvta+PrpJr6dz5ubRjw1FrKrq+J1sf3dLxlROHQRQMcDcASb75E0Hq10ku2pEBedFJnq7lEP0C76O+7WoVAu2bZdzog+cySmZFQOy8S6Rj+eZCy3BWUciD/XezvcxEBAxYmxXsx6WxHEBpzG4J0DnbB7C5NgSrmh91PVAEPBVEx8DzVnxpRP+WiQ0eOBll6PTucgo6yLBG5NBB/9GozHSoS+Bgg2QH9Bj8V7qs8wtQTcb3aL98NlPYFVjQmc8PCD4whxj6bMxWBD8viQHylEXWuq8aRL7b67zXsoJFkXcl/4aFV6nNrANxBydvsP+9mNI0eF/5sd/G2UQqdFcCMeRwGy/mSJkB0yVIPuJWsNmPuJxD5r0aBzfbjvey/iE20T2o7Lm/JQsAuXZn0o/h6FGKHpcr6q5r+wXEiJT/DOeubRRQWgvGBRGc3BbGwT85L+dEyHi0B6kep4sjfp5p9UtGAM=', 'page_age': '3 days ago', 'title': 'San Diego weather forecast – NBC 7 San Diego', 'type': 'web_search_result', 'url': 'https://www.nbcsandiego.com/weather/'}, {'encrypted_content': 'EpcCCioIBBgCIiQ4ODk4YTFkYy0yMTNkLTRhNmYtOTljYi03ZTBlNTUzZDc0NWISDE2nFyNGUdyFhEIsWhoMMvtfNBkq9ty8XyIWIjBWYLOLGLU5n30lstTiC96L+kcpApIFho3fj6v4slOQzZlqU8XxcJGi/nmyBdnZuIYqmgH73N/OSjf+Dm7Xd4p56w2juugd3edkL3VnSu2kFveNBo6ub85QSSdGog8mMf1h2T+X+EifiNk7oA6rWHSDZjxGLpUQlmyd7bqbhERYH9CDi8nV4/fSn67lzstS6+1h9kNYbeY+/HtfJjjWF9HzEbxGakvcWhJ8XLk85Ws/FHWyANisuG8lkUsp2LIfEb6xqnHYo/Q06jBQvAMdGAM=', 'page_age': '3 days ago', 'title': 'Weather Forecast and Conditions for San Diego, CA - The Weather Channel | Weather.com', 'type': 'web_search_result', 'url': 'https://weather.com/weather/today/l/San+Diego+CA?canonicalCityId=3b2b39ed755b459b725bf2a29c71d678'}, {'encrypted_content': 'ErEDCioIBBgCIiQ4ODk4YTFkYy0yMTNkLTRhNmYtOTljYi03ZTBlNTUzZDc0NWISDHgbguYKNQzIarUqMxoMXBze5mqL/MVvKMcZIjASo7xlG5DFVqlkCJtDe10CexoXshSk86xcH0c0wcapweJD3ev6UUxlALbhDpQfKDMqtAKOSBKP0k7JTH/EyTC5nh0+mhWFgO1Cb9qJUmDKXllxgb9/gK1UhuQx57pJKRhN1nQaiVvREBG5huv3mXLejC4V+j9SR1jriEf/jb/+5O7h5bvA0IWbj7/YdqSSDgpv6v97KAZ60Cq6JC/JmBIDvkKJv5l4AzVxvv9cR9mrOT5Y6x06GFQADRwuVlb3pX5rFAbNQl8904PpF8EaheljTJgAxhKtRE5kx1uQmb9aMgYZipw4ZMzOgfNQfzTXqCS/t4NR2+0Xe36nRU7BAcbajJG0Oqv52PI8o9OPXi12y0pYR33mJqeuD0Hsxg92a+5J13fPPBQrUqIiYtCwgw0vKr4fqTqUgGbBgfBo2LB83XV7rH/RFi7PKcclIN+8l64C2uXGD9LgygMkjRYrt4v+6FBcFApgLBgD', 'page_age': '3 days ago', 'title': 'San Diego, CA', 'type': 'web_search_result', 'url': 'https://www.weather.gov/sgx/'}, {'encrypted_content': 'EpkCCioIBBgCIiQ4ODk4YTFkYy0yMTNkLTRhNmYtOTljYi03ZTBlNTUzZDc0NWISDNbi0pVbNCUk7n+kqBoMM9XAavUvpOduz1qmIjC90Cv0uj4Vs/6FVOdRgt09zU3zVWCXIjhjyfVfSatMGGOtBWVWbpgdZjhgRrD0LaAqnAHtYuZ2apTct7R0npC2LWa7vn+V2q9118K4l41YMwJGhjfO4xMb8EaFY44ZIALaV9DRr5aa5CGTAlomZJaZuFZWnbT50D/Iv6Tfd3GztycVeAAuGVk733BgmVv0DHu77HvG+xKQJdNv74FnIdmXxrySroS5S13L9hq7M6b79hh6GYoenw2TEB9rKdmGC3/7pNTjLRC4CB+kEc833xkYAw==', 'page_age': None, 'title': 'San Diego, CA Weather Forecast | KGTV | kgtv.com', 'type': 'web_search_result', 'url': 'https://www.10news.com/weather'}, {'encrypted_content': 'EpQCCioIBBgCIiQ4ODk4YTFkYy0yMTNkLTRhNmYtOTljYi03ZTBlNTUzZDc0NWISDHGR56fwguBok/wsSRoMHZoqgJqsDXp9TfUmIjAlWhnVB0yKdEoz/sAl82iQLIfivli9OkkS8CzcwAWndftgRFyPYwb815UqIGjRxlQqlwH2opSva/Nd65uJJpcFOhIP7JEpaz3fLMaO3nvXMWBEs7mFepvxYLBzS1AGv5W45UGGdNgIclVMCZ6wy6VfvNde9ayD+xJQp/WT4BnVQYPeC6nTuG/7ZUukiFHpd+fk14WHMR9bh9NCb8W1Roufz2CMwc7h7lkfvRSc3jvns6vkgOh0bOmGzvY7fo0BUs7q7P0mXzGptsuBGAM=', 'page_age': '1 week ago', 'title': 'Hourly Weather Forecast for San Diego, CA - The Weather Channel | Weather.com', 'type': 'web_search_result', 'url': 'https://weather.com/weather/hourbyhour/l/San+Diego+CA?canonicalCityId=3b2b39ed755b459b725bf2a29c71d678'}, {'encrypted_content': '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', 'page_age': None, 'title': 'San Diego, CA 10-Day Weather Forecast | Weather Underground', 'type': 'web_search_result', 'url': 'https://www.wunderground.com/forecast/us/ca/san-diego'}, {'encrypted_content': 'Ev0ECioIBBgCIiQ4ODk4YTFkYy0yMTNkLTRhNmYtOTljYi03ZTBlNTUzZDc0NWISDE6oq45u4mQL3Plg7hoMHuxtoKYlD+q5L041IjC22CA19UITXTu1N0AOpf7zXrcj/OMrrq5PrLIFNN9FCtO9kZbvXqUUgtn5c+4zpPoqgAROA+6BBsJeVDHXK5HRx7k7qntye83e5YalJHJ7/klTF5VDhwNsnXeod3ydN6NyOOa7A5DlKbXWGyn/s+4SHQCTqGHc4Pszqs5xUDM41sc8spv5hoEqhctLIJqVoqFJPRiFHoq5SGW5ixA0ZiRx673QpyC5mGgTTgDMUmVmVuiNkZ8VbaWkAdVk0j0/988VlZyrXNroOqvVN/ssgNsTKWnJooEnHDzUokh/vC87XH3VixWfAgXCC3xaN60pCN3O+crHQnknpuUJvkyMHASOjL6ltfRuBzNGKXtOxJlvjD90+O7JJIj3gTtR7nRjfDqYMCSk3BsqiN8OTR425RyJWS78fYtZyMWqnSEpZQbgUATGPrmYhQ1efyXWyA15rkOFl6D9FgWjEXw4sSVnBo+oSG+z2y+sSIZWo6En0m1852ruTpgif19at658bEDouEP/9GjeeQbd6Ccx3ydP/fgsbm7gyb0dve+cc7rR3MGrM1WN0bi9wxFM4g08M3O/rWUChnhMJobutOaxWK0VMji0Ufn7oCfygg4pxnkLSm0O2vVpCxF1HX7vkv9jMrx6EDgOBKA2ULYofv9LM+WEDblcW6q/Vdq0kw8zaFy+FBqhnhMsp0lRDg3MKtcnh/jQgr5xdFuYMp48ApELDofseLjmM4RipX2yWwG9m/+6FxDm0VOLtRgD', 'page_age': '3 days ago', 'title': 'San Diego, CA Weather Conditions | Weather Underground', 'type': 'web_search_result', 'url': 'https://www.wunderground.com/weather/us/ca/san-diego'}, {'encrypted_content': 'Ev8CCioIBBgCIiQ4ODk4YTFkYy0yMTNkLTRhNmYtOTljYi03ZTBlNTUzZDc0NWISDGS2thWZ9PG3iWh8SxoMIC5yXSLUmRqET1GuIjCzl1wJb8vJt7IfWLG1ZqaSkuxWagmBIN7TOhlcHZ+sQg3OqVfeGYLkssqoRs4hZ70qggJ+sER9OCSi+xKJlwmvmxJQIdPVnegX7OEaa3b7T3L2JZEY+1IAWzQ+bkSimGYdT+LCGBbCLaVizwmnrTDvo+oe3yVAQGvXNuwfs241T1JjBi6Bhv/yl74V6sFISp8e4a3b63xuxq6C15srxzxX2k08CkJqNreO+XXTA7tKd835GOmN8EswuGRIkZsaiGyWav8GWdD/NRHHGTNwxTDgHKDMIdJIbKJzq2B5kadi02gC671iPcAacCc/3w1OTw5eI9LwU/Bp7atH0LhLeh1q3fONrshKUYA3psW+3ngUXo8V74LClY/58WcyKaHmhGCM7udXPJZGZlPCWYe0GjGF9TZ0xUwYAw==', 'page_age': '5 days ago', 'title': 'National Weather Service', 'type': 'web_search_result', 'url': 'https://forecast.weather.gov/zipcity.php?inputstring=San+Diego,CA/'}], 'tool_use_id': 'srvtoolu_01SEnDGkdbPEGTSfR9NaGNdG', 'type': 'web_search_tool_result'}, {'citations': None, 'text': "Based on the current weather information for San Diego, here's what I found:\n\n", 'type': 'text'}, {'citations': [{'cited_text': 'Cooler today with highs around 4-7 degrees below normal for most areas, still slightly above normal in the lower deserts. ', 'encrypted_index': 'Eo8BCioIBBgCIiQ4ODk4YTFkYy0yMTNkLTRhNmYtOTljYi03ZTBlNTUzZDc0NWISDIBedyZUhGHNaJ2ukxoMHy8XaiSh+H6gk0inIjC/wmnq/r1vT/Qe76IaV/Xd6klBMGzL6NEPCWexfa9deSXX/Llfr41IA8xsq3TiN54qE8jQGhBEf+lPmzn/s9jVsMsNct8YBA==', 'title': 'San Diego, CA', 'type': 'web_search_result_location', 'url': 'https://www.weather.gov/sgx/'}], 'text': "Today's weather in San Diego is cooler with highs around 4-7 degrees below normal for most areas", 'type': 'text'}, {'citations': None, 'text': '. ', 'type': 'text'}, {'citations': [{'cited_text': 'TomorrowSat 06/21 High · 67 °F · 14% Precip. ', 'encrypted_index': 'Eo8BCioIBBgCIiQ4ODk4YTFkYy0yMTNkLTRhNmYtOTljYi03ZTBlNTUzZDc0NWISDNOYme4jM5M1V4I7jxoMXs9PV1wb1WfMW+QqIjDEbT//JYi0Nn18vdZhg0Uk8pCASzjturZDJty6kOQLVA1ohoq6nImkWkf3sKVdSvYqEysdg3NRHgjZmjZXbOmCnSdU1ikYBA==', 'title': 'San Diego, CA Weather Conditions | Weather Underground', 'type': 'web_search_result_location', 'url': 'https://www.wunderground.com/weather/us/ca/san-diego'}, {'cited_text': 'High 67F. ', 'encrypted_index': 'Eo8BCioIBBgCIiQ4ODk4YTFkYy0yMTNkLTRhNmYtOTljYi03ZTBlNTUzZDc0NWISDGF9sGRznM4mRbqsehoMGU3flFvbTPSQz4K4IjAItXQoDAgLBsgMzmKnlWeqYmGRwr4ad12NCDxZp6YomTyIr7H8ROsNH5g2ikrCGQUqExjoerJ9TcM8HpfVHwHFkfrkfJgYBA==', 'title': 'San Diego, CA Weather Conditions | Weather Underground', 'type': 'web_search_result_location', 'url': 'https://www.wunderground.com/weather/us/ca/san-diego'}], 'text': 'Tomorrow (Saturday, June 21st) is expected to have a high of 67°F', 'type': 'text'}, {'citations': None, 'text': ' with ', 'type': 'text'}, {'citations': [{'cited_text': '/ 0.00 °in Cloudy early with partial sunshine expected late. ', 'encrypted_index': 'Eo8BCioIBBgCIiQ4ODk4YTFkYy0yMTNkLTRhNmYtOTljYi03ZTBlNTUzZDc0NWISDL76Lvlp8NU3QPwLWxoMDkKBpH5TRs9U9v9zIjC9R51OymIJdmJd2DPA0R6rO3teSex8m/xKenpd4olvb3257lBdPATcWWHlzwW4Zi0qE6QxVAUCe4tUEciTCZ1eR7bZr40YBA==', 'title': 'San Diego, CA Weather Conditions | Weather Underground', 'type': 'web_search_result_location', 'url': 'https://www.wunderground.com/weather/us/ca/san-diego'}], 'text': 'cloudy conditions early with partial sunshine expected later', 'type': 'text'}, {'citations': None, 'text': '. ', 'type': 'text'}, {'citations': [{'cited_text': 'Winds SSW at 10 to 15 mph. ', 'encrypted_index': 'Eo8BCioIBBgCIiQ4ODk4YTFkYy0yMTNkLTRhNmYtOTljYi03ZTBlNTUzZDc0NWISDEKUNG3gK0QQ1aEc4RoM8C1ABOx/aMrrNtfLIjB3qOzI96PtDyAZRWVI0HGfCRN92yVAOT+sDp/66+OS7paBc0pCCJyLxYbeTvhqSLoqExyvs+1HMXMEgjzuLdtsGaeCKpYYBA==', 'title': 'San Diego, CA Weather Conditions | Weather Underground', 'type': 'web_search_result_location', 'url': 'https://www.wunderground.com/weather/us/ca/san-diego'}], 'text': 'Winds will be from the SSW at 10 to 15 mph', 'type': 'text'}, {'citations': None, 'text': '.\n\n', 'type': 'text'}, {'citations': [{'cited_text': 'Read More... Much cooler on Saturday with highs as much as 10-15 degrees below normal inland. ', 'encrypted_index': 'Eo8BCioIBBgCIiQ4ODk4YTFkYy0yMTNkLTRhNmYtOTljYi03ZTBlNTUzZDc0NWISDBBt8AI5oeHRNUT9jxoMSrr/Nfce4q1KbnkBIjCGF4Lf3By4TV7G0ZJFS9kTOEtCmiM7vdOdPRC8lZnJ3t2WHMfuYz9UbQxjhfTS7FgqE8A9kPCDGgDyMtD0T0y1Et3B7P0YBA==', 'title': 'San Diego, CA', 'type': 'web_search_result_location', 'url': 'https://www.weather.gov/sgx/'}], 'text': 'Much cooler weather is expected on Saturday with highs as much as 10-15 degrees below normal inland', 'type': 'text'}, {'citations': None, 'text': '. ', 'type': 'text'}, {'citations': [{'cited_text': "Tomorrow's temperature is forecast to be COOLER than today.", 'encrypted_index': 'EpABCioIBBgCIiQ4ODk4YTFkYy0yMTNkLTRhNmYtOTljYi03ZTBlNTUzZDc0NWISDCSmGM0EMIc2UCJ6mBoMLDxZqPZm8YnHel0uIjC6M62kZqg81skz6BSkGrVkNKf7qAf6WMZRn61nkqgyzyiR8JOCiyS103sM64py7/cqFCid3qnff/rr9aLcVGmr75oYiVmEGAQ=', 'title': 'San Diego, CA Weather Conditions | Weather Underground', 'type': 'web_search_result_location', 'url': 'https://www.wunderground.com/weather/us/ca/san-diego'}], 'text': "Tomorrow's temperature is forecast to be cooler than today", 'type': 'text'}, {'citations': None, 'text': '.\n\nFor air quality, ', 'type': 'text'}, {'citations': [{'cited_text': 'The air has reached a high level of pollution and is unhealthy for sensitive groups. Reduce time spent outside if you are feeling symptoms such as dif...', 'encrypted_index': 'EpEBCioIBBgCIiQ4ODk4YTFkYy0yMTNkLTRhNmYtOTljYi03ZTBlNTUzZDc0NWISDBGWylZumU93E9v/0RoMUer3ASSy12VRQRmIIjD3TnjC+pt1GrgVdQbqkBQ2N3cSrJbZ9amWBsdk2fnk3KjVWiqjW4rLZ+lwdDovubAqFSbdCPir/nKsF83zH82E+UArihNyThgE', 'title': 'San Diego, CA Weather Forecast | AccuWeather', 'type': 'web_search_result_location', 'url': 'https://www.accuweather.com/en/us/san-diego/92101/weather-forecast/347628'}], 'text': "the air has reached a high level of pollution and is unhealthy for sensitive groups, so it's recommended to reduce time spent outside if experiencing symptoms like difficulty breathing or throat irritation", 'type': 'text'}, {'citations': None, 'text': '.', 'type': 'text'}]
- model:
claude-sonnet-4-20250514
- role:
assistant
- stop_reason:
end_turn
- stop_sequence:
None
- type:
message
- usage:
{'cache_creation_input_tokens': 5246, 'cache_read_input_tokens': 2042, 'input_tokens': 12, 'output_tokens': 354, 'server_tool_use': {'web_search_requests': 1}, 'service_tier': 'standard'}
Third party providers
NB: The 3rd party model list is currently out of date–PRs to fix that would be welcome!
Amazon Bedrock
These are Amazon’s current Claude models:
models_aws
['anthropic.claude-sonnet-4-20250514-v1:0',
'claude-3-5-haiku-20241022',
'claude-3-7-sonnet-20250219',
'anthropic.claude-3-opus-20240229-v1:0',
'anthropic.claude-3-5-sonnet-20241022-v2:0']
Provided boto3
is installed, we otherwise don’t need any extra code to support Amazon Bedrock – we just have to set up the approach client:
= AnthropicBedrock(
ab =os.environ['AWS_ACCESS_KEY'],
aws_access_key=os.environ['AWS_SECRET_KEY'],
aws_secret_key
)= Client(models_aws[0], ab) client
= Chat(cli=client) chat
"I'm Jeremy") chat(
Google Vertex
models_goog
['anthropic.claude-3-sonnet-20240229-v1:0',
'anthropic.claude-3-haiku-20240307-v1:0',
'claude-3-opus@20240229',
'claude-3-5-sonnet-v2@20241022',
'claude-3-sonnet@20240229',
'claude-3-haiku@20240307']
from anthropic import AnthropicVertex
import google.auth
= google.auth.default()[1]
project_id = "us-east5"
region = AnthropicVertex(project_id=project_id, region=region)
gv = Client(models_goog[-1], gv) client
= Chat(cli=client) chat
"I'm Jeremy") chat(
Footnotes
https://www.weather.gov/sgx/ “Cooler today with highs around 4-7 degrees below normal for most areas, still slightly above normal in the lower deserts.”↩︎
https://www.wunderground.com/weather/us/ca/san-diego “TomorrowSat 06/21 High · 67 °F · 14% Precip.”↩︎
https://www.wunderground.com/weather/us/ca/san-diego “High 67F.”↩︎
https://www.wunderground.com/weather/us/ca/san-diego “/ 0.00 °in Cloudy early with partial sunshine expected late.”↩︎
https://www.wunderground.com/weather/us/ca/san-diego “Winds SSW at 10 to 15 mph.”↩︎
https://www.weather.gov/sgx/ “Read More… Much cooler on Saturday with highs as much as 10-15 degrees below normal inland.”↩︎
https://www.wunderground.com/weather/us/ca/san-diego “Tomorrow’s temperature is forecast to be COOLER than today.”↩︎
https://www.accuweather.com/en/us/san-diego/92101/weather-forecast/347628 “The air has reached a high level of pollution and is unhealthy for sensitive groups. Reduce time spent outside if you are feeling symptoms such as dif…”↩︎