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
from cachy import enable_cachy
import warningsenable_cachy()warnings.filterwarnings("ignore", message="Pydantic serializer warnings")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-1-20250805': 'opus',
'claude-sonnet-4-5': 'sonnet',
'claude-haiku-4-5': 'haiku',
'claude-opus-4-20250514': 'opus-4',
'claude-3-opus-20240229': 'opus-3',
'claude-sonnet-4-20250514': 'sonnet-4',
'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-opus-4-1-20250805-v1:0': 'opus',
'anthropic.claude-3-5-sonnet-20241022-v2:0': 'sonnet',
'anthropic.claude-3-opus-20240229-v1:0': 'opus-3',
'anthropic.claude-3-sonnet-20240229-v1:0': 'sonnet',
'anthropic.claude-3-haiku-20240307-v1:0': 'haiku',
# Google
'claude-opus-4-1@20250805': 'opus',
'claude-3-5-sonnet-v2@20241022': 'sonnet',
'claude-3-opus@20240229': 'opus-3',
'claude-3-sonnet@20240229': 'sonnet',
'claude-3-haiku@20240307': 'haiku',
}
all_models = list(model_types)models['claude-opus-4-1-20250805',
'claude-sonnet-4-5',
'claude-haiku-4-5',
'claude-opus-4-20250514',
'claude-3-opus-20240229',
'claude-sonnet-4-20250514',
'claude-3-7-sonnet-20250219',
'claude-3-5-sonnet-20241022']
Exported source
text_only_models = ('claude-3-5-haiku-20241022',)Exported source
has_streaming_models = set(all_models)
has_system_prompt_models = set(all_models)
has_temperature_models = set(all_models)
has_extended_thinking_models = {
'claude-opus-4-1-20250805', 'claude-opus-4-20250514', 'claude-sonnet-4-20250514', 'claude-3-7-sonnet-20250219',
'sonnet-4-5', 'haiku-4-5'
}has_extended_thinking_models{'claude-3-7-sonnet-20250219',
'claude-opus-4-1-20250805',
'claude-opus-4-20250514',
'claude-sonnet-4-20250514',
'haiku-4-5',
'sonnet-4-5'}
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_modelscan_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.
model = models[1]; model'claude-sonnet-4-5'
For examples, we’ll use the latest Sonnet, since it’s awesome.
Antropic SDK
cli = Anthropic()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.
m = {'role': 'user', 'content': "I'm Jeremy"}
r = cli.messages.create(messages=[m], model=model, max_tokens=100)
rHello Jeremy! Nice to meet you. How can I help you today?
- id:
msg_01M2o3SENPk6JXLarGYZffo4 - content:
[{'citations': None, 'text': 'Hello Jeremy! Nice to meet you. How can I help you today?', 'type': 'text'}] - model:
claude-sonnet-4-5-20250929 - role:
assistant - stop_reason:
end_turn - stop_sequence:
None - type:
message - usage:
{'cache_creation': {'ephemeral_1h_input_tokens': 0, 'ephemeral_5m_input_tokens': 0}, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 10, 'output_tokens': 18, '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
blk_type:type|str=TextBlock # The type of block to find
):
"Find the first block of type `blk_type` in `r.content`."
f = (lambda x:_type(x)==blk_type) if isinstance(blk_type,str) else (lambda x:isinstance(x,blk_type))
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 can I help you today?', type='text')
def contents(r):
"Helper to get the contents from Claude response `r`."
blk = find_block(r)
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)For display purposes, we often just want to show the text itself.
contents(r)'Hello Jeremy! Nice to meet you. How can I help you today?'
Exported source
@patch
def _repr_markdown_(self:(Message)):
det = '\n- '.join(f'{k}: `{v}`' for k,v in self.model_dump().items())
cts = re.sub(r'\$', '$', contents(self)) # escape `$` for jupyter latex
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.
rHello Jeremy! Nice to meet you. How can I help you today?
- id:
msg_01M2o3SENPk6JXLarGYZffo4 - content:
[{'citations': None, 'text': 'Hello Jeremy! Nice to meet you. How can I help you today?', 'type': 'text'}] - model:
claude-sonnet-4-5-20250929 - role:
assistant - stop_reason:
end_turn - stop_sequence:
None - type:
message - usage:
{'cache_creation': {'ephemeral_1h_input_tokens': 0, 'ephemeral_5m_input_tokens': 0}, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 10, 'output_tokens': 18, '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.usageIn: 10; Out: 18; Cache create: 0; Cache read: 0; Total Tokens: 28; 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
out=0, # Output tokens
cache_create=0, # Cache creation tokens
cache_read=0, # Cache read tokens
server_tool_use=server_tool_usage() # 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_input_tokens=cache_read, server_tool_use=server_tool_use)The constructor provided by Anthropic is rather verbose, so we clean it up a bit, using a lowercase version of the name.
usage(5)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.
usage(5,1).total6
Usage.__repr__
Usage.__repr__ ()
Return repr(self).
Exported source
@patch
def __repr__(self:Usage):
io_toks = f'In: {self.input_tokens}; Out: {self.output_tokens}'
cache_toks = f'Cache create: {_dgetattr(self, "cache_creation_input_tokens",0)}; Cache read: {_dgetattr(self, "cache_read_input_tokens",0)}'
server_tool_use = _dgetattr(self, "server_tool_use",server_tool_usage())
server_tool_use_str = f'Search: {server_tool_use.web_search_requests}'
total_tok = f'Total Tokens: {self.total}'
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__
usage(5)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.
server_tool_usage(1) + server_tool_usage(2)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,
_dgetattr(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()))r.usage+r.usage + usage(server_tool_use=server_tool_usage(1))In: 20; Out: 36; Cache create: 0; Cache read: 0; Total Tokens: 56; Search: 1
Creating messages
Creating correctly formatted dicts 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.
prompt = "I'm Jeremy"
m = mk_msg(prompt)
r = cli.messages.create(messages=[m], model=model, max_tokens=100)
rHello Jeremy! Nice to meet you. How can I help you today?
- id:
msg_01M2o3SENPk6JXLarGYZffo4 - content:
[{'citations': None, 'text': 'Hello Jeremy! Nice to meet you. How can I help you today?', 'type': 'text'}] - model:
claude-sonnet-4-5-20250929 - role:
assistant - stop_reason:
end_turn - stop_sequence:
None - type:
message - usage:
{'cache_creation': {'ephemeral_1h_input_tokens': 0, 'ephemeral_5m_input_tokens': 0}, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 10, 'output_tokens': 18, '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.
msgs = mk_msgs([prompt, r, "I forgot my name. Can you remind me please?"])
msgs[{'role': 'user', 'content': "I'm Jeremy"},
{'role': 'assistant',
'content': [TextBlock(citations=None, text='Hello Jeremy! Nice to meet you. How can I help you today?', type='text')]},
{'role': 'user', 'content': 'I forgot my name. Can you remind me please?'}]
cli.messages.create(messages=msgs, model=model, max_tokens=200)Yes, your name is Jeremy! You told me that at the start of our conversation.
- id:
msg_01W1nFmwinpP3YwaB9zLhP2Y - content:
[{'citations': None, 'text': 'Yes, your name is Jeremy! You told me that at the start of our conversation.', 'type': 'text'}] - model:
claude-sonnet-4-5-20250929 - role:
assistant - stop_reason:
end_turn - stop_sequence:
None - type:
message - usage:
{'cache_creation': {'ephemeral_1h_input_tokens': 0, 'ephemeral_5m_input_tokens': 0}, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 42, 'output_tokens': 21, '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 = cacheWe’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.
c = Client(model)
c.useIn: 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:
blk = find_block(r)
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 rWe 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.useIn: 10; Out: 18; Cache create: 0; Cache read: 0; Total Tokens: 28; 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.resultOnce 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
o.value = s.get_final_message()
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|Omit=<anthropic.Omit object at 0x7f469390a1d0>, service_tier:"Literal['auto','standard_ only']|Omit"=<anthropic.Omit object at 0x7f469390a1d0>, stop_sequences:SequenceNotStr[str]|Omit=<anthropic.Omit object at 0x7f469390a1d0>, system:Union[str,Iterable[Tex tBlockParam]]|Omit=<anthropic.Omit object at 0x7f469390a1d0>, temperature:float|Omit=<anthropic.Omit object at 0x7f469390a1d0>, thinking:ThinkingConfigParam|Omit=<anthropic.Omit object at 0x7f469390a1d0>, top_k:int|Omit=<anthropic.Omit object at 0x7f469390a1d0>, top_p:float|Omit=<anthropic.Omit object at 0x7f469390a1d0>, 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 | Omit | <anthropic.Omit object at 0x7f469390a1d0> | |
| service_tier | Literal[‘auto’, ‘standard_only’] | Omit | <anthropic.Omit object at 0x7f469390a1d0> | |
| stop_sequences | SequenceNotStr[str] | Omit | <anthropic.Omit object at 0x7f469390a1d0> | |
| system | Union[str, Iterable[TextBlockParam]] | Omit | <anthropic.Omit object at 0x7f469390a1d0> | |
| temperature | float | Omit | <anthropic.Omit object at 0x7f469390a1d0> | |
| thinking | ThinkingConfigParam | Omit | <anthropic.Omit object at 0x7f469390a1d0> | |
| top_k | int | Omit | <anthropic.Omit object at 0x7f469390a1d0> | |
| top_p | float | Omit | <anthropic.Omit object at 0x7f469390a1d0> | |
| 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:
kwargs['thinking'] = {'type':'enabled', 'budget_tokens':maxthinktok}
temp,prefill = 1,''
pref = [prefill.strip()] if prefill else []
if not isinstance(msgs,list): msgs = [msgs]
if stop is not None:
if not isinstance(stop, (list)): stop = [stop]
kwargs["stop_sequences"] = stop
msgs = mk_msgs(msgs+pref, cache=self.cache, cache_last_ckpt_only=self.cache)
assert not ('image' in get_types(msgs) and self.text_only), f"Images not supported by: {self.model}"
kwargs |= dict(max_tokens=maxtok, system=sp, temperature=temp)
return msgs, kwargsExported source
@patch
@delegates(messages.Messages.create)
def __call__(self:Client,
msgs:list, # List of messages in the dialog
sp='', # The system prompt
temp=0, # Temperature
maxtok=4096, # Maximum tokens
maxthinktok=0, # Maximum thinking tokens
prefill='', # Optional prefill to pass to Claude as start of its response
stream:bool=False, # Stream response?
stop=None, # Stop sequence
tools:Optional[list]=None, # List of tools to make available to Claude
tool_choice:Optional[dict]=None, # Optionally force use of some tool
cb=None, # Callback to pass result to when complete
**kwargs):
"Make a call to Claude."
msgs,kwargs = self._precall(msgs, prefill, sp, temp, maxtok, maxthinktok, stream,
stop, tools, tool_choice, kwargs)
m = self.c.messages
f = m.stream if stream else m.create
res = f(model=self.model, messages=msgs, **kwargs)
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.
c = Client(model, log=True)
c.useIn: 0; Out: 0; Cache create: 0; Cache read: 0; Total Tokens: 0; Search: 0
c('Hi')Hello! How can I help you today?
- id:
msg_01XR7UCdLWF8MUeJZR2hhy5j - content:
[{'citations': None, 'text': 'Hello! How can I help you today?', 'type': 'text'}] - model:
claude-sonnet-4-5-20250929 - role:
assistant - stop_reason:
end_turn - stop_sequence:
None - type:
message - usage:
{'cache_creation': {'ephemeral_1h_input_tokens': 0, 'ephemeral_5m_input_tokens': 0}, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 8, 'output_tokens': 12, 'server_tool_use': None, 'service_tier': 'standard'}
Usage details are automatically updated after each call:
c.useIn: 8; Out: 12; Cache create: 0; Cache read: 0; Total Tokens: 20; 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_01XR7UCdLWF8MUeJZR2hhy5j', content=[TextBlock(citations=None, text='Hello! How can I help you today?', type='text')], model='claude-sonnet-4-5-20250929', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 8; Out: 12; Cache create: 0; Cache read: 0; Total Tokens: 20; Search: 0),
'stop_reason': 'end_turn',
'stop_sequence': None,
'system': '',
'temperature': 0,
'use': In: 8; Out: 12; Cache create: 0; Cache read: 0; Total Tokens: 20; Search: 0}]
Let’s try out prefill:
q = "Very concisely, what is the meaning of life?"
pref = 'According to Douglas Adams, 'c(q, prefill=pref)According to Douglas Adams, it’s 42.
More seriously: Create meaning through connections, growth, and contribution to something beyond yourself.
- id:
msg_013f6m5WD8Km3CzRRbY2wqAa - content:
[{'citations': None, 'text': "According to Douglas Adams, it's 42.\n\nMore seriously: Create meaning through connections, growth, and contribution to something beyond yourself.", 'type': 'text'}] - model:
claude-sonnet-4-5-20250929 - role:
assistant - stop_reason:
end_turn - stop_sequence:
None - type:
message - usage:
{'cache_creation': {'ephemeral_1h_input_tokens': 0, 'ephemeral_5m_input_tokens': 0}, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 24, 'output_tokens': 27, 'server_tool_use': None, 'service_tier': 'standard'}
c.useIn: 32; Out: 39; Cache create: 0; Cache read: 0; Total Tokens: 71; Search: 0
We can pass stream=True to stream the response back incrementally:
r = c('Hi', stream=True)
for o in r: print(o, end='')Hello! How can I help you today?
c.useIn: 40; Out: 51; Cache create: 0; Cache read: 0; Total Tokens: 91; Search: 0
The full final message after completion of streaming is in the value attr of the response:
r.valueHello! How can I help you today?
- id:
msg_01SDCfi2p2xtS7EbtwEMWXW5 - content:
[{'citations': None, 'text': 'Hello! How can I help you today?', 'type': 'text'}] - model:
claude-sonnet-4-5-20250929 - role:
assistant - stop_reason:
end_turn - stop_sequence:
None - type:
message - usage:
{'cache_creation': {'ephemeral_1h_input_tokens': 0, 'ephemeral_5m_input_tokens': 0}, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 8, 'output_tokens': 12, 'server_tool_use': None, 'service_tier': 'standard'}
for o in c(q, prefill=pref, stream=True): print(o, end='')According to Douglas Adams, it's 42.
More seriously: Create meaning through connections, growth, and contribution to something beyond yourself.
c.useIn: 64; Out: 78; Cache create: 0; Cache read: 0; Total Tokens: 142; Search: 0
Pass a stop sequence if you want claude to stop generating text when it encounters it.
c("Count from 1 to 10", stop="5")1, 2, 3, 4,
- id:
msg_011Ew5LZ8U9toQLNgyzZbrD9 - content:
[{'citations': None, 'text': '1, 2, 3, 4, ', 'type': 'text'}] - model:
claude-sonnet-4-5-20250929 - role:
assistant - stop_reason:
stop_sequence - stop_sequence:
5 - type:
message - usage:
{'cache_creation': {'ephemeral_1h_input_tokens': 0, 'ephemeral_5m_input_tokens': 0}, '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:
cache_w, cache_r = _dgetattr(self, "cache_creation_input_tokens",0), _dgetattr(self, "cache_read_input_tokens",0)
tok_cost = sum([self.input_tokens * costs[0] + self.output_tokens * costs[1] + cache_w * costs[2] + cache_r * costs[3]]) / 1e6
server_tool_use = _dgetattr(self, "server_tool_use",server_tool_usage())
server_tool_cost = server_tool_use.web_search_requests * server_tool_pricing['web_search_requests'] / 1e3
return tok_cost + server_tool_costClient.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):
costs = pricing[model_types[c.model]]
inp_cost = c.use.input_tokens * costs[0] / 1e6
out_cost = c.use.output_tokens * costs[1] / 1e6
cache_w = c.use.cache_creation_input_tokens
cache_r = c.use.cache_read_input_tokens
cache_cost = (cache_w * costs[2] + cache_r * costs[3]) / 1e6
server_tool_use = c.use.server_tool_use
server_tool_cost = server_tool_use.web_search_requests * server_tool_pricing['web_search_requests'] / 1e3
return inp_cost, out_cost, cache_cost, cache_w + cache_r, server_tool_costThe 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'
msg = contents(self.result)
inp_cost, out_cost, cache_cost, cached_toks, server_tool_cost = get_costs(self)
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}** |"""c1, 2,
| Metric | Count | Cost (USD) |
|---|---|---|
| Input tokens | 94 | 0.000282 |
| Output tokens | 100 | 0.001500 |
| Cache tokens | 0 | 0.000000 |
| Server tool use | 0 | 0.000000 |
| Total | 194 | $0.001782 |
Pass a list of alternating user/assistant messages to give Claude a “dialog”.
c(["My name is Jeremy", "Hi Jeremy!", "Can you remind me what my name is?"])Yes, your name is Jeremy!
- id:
msg_016EVejFbBZrcbXkjEYBAC12 - content:
[{'citations': None, 'text': 'Yes, your name is Jeremy!', 'type': 'text'}] - model:
claude-sonnet-4-5-20250929 - role:
assistant - stop_reason:
end_turn - stop_sequence:
None - type:
message - usage:
{'cache_creation': {'ephemeral_1h_input_tokens': 0, 'ephemeral_5m_input_tokens': 0}, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 29, 'output_tokens': 10, 'server_tool_use': None, 'service_tier': 'standard'}
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:
@dataclass
class MySum: val:int
def sums(
a:int, # First thing to sum
b:int=1 # Second thing to sum
) -> int: # The sum of the inputs
"Adds a + b."
print(f"Finding the sum of {a} and {b}")
return MySum(a + b)a,b = 604542,6458932
pr = f"What is {a}+{b}?"
sp = "Always use tools when calculations are required."Claudette can autogenerate a schema thanks to the toolslm library. We’ll force the use of the tool using the function we created earlier.
tools=[get_schema(sums)]
choice = mk_tool_choice('sums')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.
msgs = mk_msgs(pr)
r = c(msgs, sp=sp, tools=tools, tool_choice=choice)
rToolUseBlock(id=‘toolu_01A7hoYwW5hnMpaKfFxVXAzB’, input={‘a’: 604542, ‘b’: 6458932}, name=‘sums’, type=‘tool_use’)
- id:
msg_019ybw6cSukU8zdHB2HaPZzD - content:
[{'id': 'toolu_01A7hoYwW5hnMpaKfFxVXAzB', 'input': {'a': 604542, 'b': 6458932}, 'name': 'sums', 'type': 'tool_use'}] - model:
claude-sonnet-4-5-20250929 - role:
assistant - stop_reason:
tool_use - stop_sequence:
None - type:
message - usage:
{'cache_creation': {'ephemeral_1h_input_tokens': 0, 'ephemeral_5m_input_tokens': 0}, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 713, '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.
ns = mk_ns(sums)
ns{'sums': <function __main__.sums(a: int, b: int = 1) -> int>}
ToolResult is used for two special cases:
When tool calls are RPCs with claudette running on an application server and code execution happening elsewhere, wrapping with a
result_typefield is used as a type descriptor for the claudette client.Different types are handled in message history with specific format, so
mk_funcresbranches the Anthropic representation (see depending on theresult_type.
Currently images are the only supported tool result type - see https://docs.anthropic.com/en/docs/agents-and-tools/tool-use/implement-tool-use#example-of-tool-result-with-images for the format implemented in mk_funcres.
ToolResult
ToolResult (result_type:str, data)
Base class for objects needing a basic __repr__
mk_funcres
mk_funcres (fc, ns)
Given tool use block ‘fc’, get tool result, and create a tool_result response.
We can now use the function requested by Claude. We look it up in ns, and pass in the provided parameters.
fcs = [o for o in r.content if isinstance(o,ToolUseBlock)]
fcs[ToolUseBlock(id='toolu_01A7hoYwW5hnMpaKfFxVXAzB', input={'a': 604542, 'b': 6458932}, name='sums', type='tool_use')]
res = [mk_funcres(fc, ns=ns) for fc in fcs]
resFinding the sum of 604542 and 6458932
[{'type': 'tool_result',
'tool_use_id': 'toolu_01A7hoYwW5hnMpaKfFxVXAzB',
'content': 'MySum(val=7063474)'}]
mk_toolres
mk_toolres (r:collections.abc.Mapping, ns:Optional[collections.abc.Mapping]=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 |
Exported source
def mk_toolres(
r:abc.Mapping, # Tool use request response from Claude
ns:Optional[abc.Mapping]=None # Namespace to search for tools
):
"Create a `tool_result` message from response `r`."
cts = getattr(r, 'content', [])
res = [mk_msg(r.model_dump(), role='assistant')]
if ns is None: ns=globals()
tcs = [mk_funcres(o, ns) for o in cts if isinstance(o,ToolUseBlock)]
if tcs: res.append(mk_msg(tcs))
return resfoo = []
foo.append({})
foo.append({})
foo[{}, {}]
In order to tell Claude the result of the tool call, we pass back the tool use assistant request and the tool_result response.
tr = mk_toolres(r, ns=ns)
trFinding the sum of 604542 and 6458932
[{'role': 'assistant',
'content': [{'id': 'toolu_01A7hoYwW5hnMpaKfFxVXAzB',
'input': {'a': 604542, 'b': 6458932},
'name': 'sums',
'type': 'tool_use'}]},
{'role': 'user',
'content': [{'type': 'tool_result',
'tool_use_id': 'toolu_01A7hoYwW5hnMpaKfFxVXAzB',
'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.
msgs += tr
contents(c(msgs, sp=sp, tools=tools))'The sum of 604542 + 6458932 = **7,063,474**'
contents(msgs[-1])'MySum(val=7063474)'
msgs[{'role': 'user', 'content': 'What is 604542+6458932?'},
{'role': 'assistant',
'content': [{'id': 'toolu_01A7hoYwW5hnMpaKfFxVXAzB',
'input': {'a': 604542, 'b': 6458932},
'name': 'sums',
'type': 'tool_use'}]},
{'role': 'user',
'content': [{'type': 'tool_result',
'tool_use_id': 'toolu_01A7hoYwW5hnMpaKfFxVXAzB',
'content': 'MySum(val=7063474)'}]}]
Text editing
Anthropic also has a special tool type specific to text editing.
tools = [text_editor_conf['sonnet']]
tools[{'type': 'text_editor_20250728', 'name': 'str_replace_based_edit_tool'}]
pr = 'Could you please explain my _quarto.yml file?'
msgs = [mk_msg(pr)]
r = c(msgs, sp=sp, tools=tools)
find_block(r, ToolUseBlock)ToolUseBlock(id='toolu_01LFKfdhgBTckSrecDjYisDY', input={'command': 'view', 'path': '.'}, 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.
ns = mk_ns(str_replace_based_edit_tool)
tr = mk_toolres(r, ns=ns)
msgs += tr
print(contents(c(msgs, sp=sp, tools=tools))[:128])ToolUseBlock(id='toolu_01BtxanjZJXxMTztgQwvci7k', input={'command': 'view', 'path': '/_quarto.yml'}, name='str_replace_based_edi
Structured data
a,b = 604542,6458932
pr = f"What is {a}+{b}?"
sp = "Always use your tools for calculations."for tools in [sums, [get_schema(sums)]]:
r = c(pr, tools=tools, tool_choice='sums')
print(r)Message(id='msg_017V8xDmc28RJQChvHYRLAtM', content=[ToolUseBlock(id='toolu_01WzqADg5E5n2AKczQDppugU', input={'a': 604542, 'b': 6458932}, name='sums', type='tool_use')], model='claude-sonnet-4-5-20250929', role='assistant', stop_reason='tool_use', stop_sequence=None, type='message', usage=In: 708; Out: 53; Cache create: 0; Cache read: 0; Total Tokens: 761; Search: 0)
Message(id='msg_017V8xDmc28RJQChvHYRLAtM', content=[ToolUseBlock(id='toolu_01WzqADg5E5n2AKczQDppugU', input={'a': 604542, 'b': 6458932}, name='sums', type='tool_use')], model='claude-sonnet-4-5-20250929', role='assistant', stop_reason='tool_use', stop_sequence=None, type='message', usage=In: 708; Out: 53; Cache create: 0; Cache read: 0; Total Tokens: 761; Search: 0)
ns = mk_ns(sums)
tr = mk_toolres(r, ns=ns)Finding the sum of 604542 and 6458932
Client.structured
Client.structured (msgs:list, tools:Optional[list]=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|Omit=<anthropic.Omit object at 0x7f469390a1d0>, service_tier:"Literal['auto','standar d_only']|Omit"=<anthropic.Omit object at 0x7f469390a1d0>, stop_sequences:SequenceNotStr[str]|Om it=<anthropic.Omit object at 0x7f469390a1d0>, system:U nion[str,Iterable[TextBlockParam]]|Omit=<anthropic.Omi t object at 0x7f469390a1d0>, temperature:float|Omit=<anthropic.Omit object at 0x7f469390a1d0>, thinking:ThinkingConfigParam|Omit=<anthropic.Omit object at 0x7f469390a1d0>, top_k:int|Omit=<anthropic.Omit object at 0x7f469390a1d0>, top_p:float|Omit=<anthropic.Omit object at 0x7f469390a1d0>, 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 |
| 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 | Omit | <anthropic.Omit object at 0x7f469390a1d0> | |
| service_tier | Literal[‘auto’, ‘standard_only’] | Omit | <anthropic.Omit object at 0x7f469390a1d0> | |
| stop_sequences | SequenceNotStr[str] | Omit | <anthropic.Omit object at 0x7f469390a1d0> | |
| system | Union[str, Iterable[TextBlockParam]] | Omit | <anthropic.Omit object at 0x7f469390a1d0> | |
| temperature | float | Omit | <anthropic.Omit object at 0x7f469390a1d0> | |
| thinking | ThinkingConfigParam | Omit | <anthropic.Omit object at 0x7f469390a1d0> | |
| top_k | int | Omit | <anthropic.Omit object at 0x7f469390a1d0> | |
| top_p | float | Omit | <anthropic.Omit object at 0x7f469390a1d0> | |
| 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,
msgs:list, # List of messages in the dialog
tools:Optional[list]=None, # List of tools to make available to Claude
ns:Optional[abc.Mapping]=None, # Namespace to search for tools
**kwargs):
"Return the value of all tool calls (generally used for structured outputs)"
tools = listify(tools)
res = self(msgs, tools=tools, tool_choice=tools, **kwargs)
if ns is None: ns=mk_ns(*tools)
cts = getattr(res, 'content', [])
tcs = [call_func(o.name, o.input, ns=ns) for o in cts if isinstance(o,ToolUseBlock)]
return tcsAnthropic’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.
c.structured(pr, tools=[sums])Finding the sum of 604542 and 6458932
[MySum(val=7063474)]
cToolUseBlock(id=‘toolu_01JuugWVrXHqxi2DCwMo9ZUy’, input={‘a’: 604542, ‘b’: 6458932}, name=‘sums’, type=‘tool_use’)
| Metric | Count | Cost (USD) |
|---|---|---|
| Input tokens | 8,677 | 0.026031 |
| Output tokens | 538 | 0.008070 |
| Cache tokens | 0 | 0.000000 |
| Server tool use | 0 | 0.000000 |
| Total | 9,215 | $0.034101 |
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}"Book("War and Peace", 950)Book Title : War and Peace
Number of Pages : 950
def find_page(book: Book, # The book to find the halfway point of
percent: int, # Percent of a book to read to, e.g. halfway == 50,
) -> 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,'}},
'required': ['book', 'percent'],
'$defs': {'Book': {'type': 'object',
'properties': {'title': {'type': 'string', 'description': ''},
'pages': {'type': 'integer', 'description': ''}},
'title': 'Book',
'required': ['title', 'pages']}}}}
choice = mk_tool_choice('find_page')
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:
(_is_builtin(int), _is_builtin(Book), _is_builtin(List))(True, False, True)
(_convert(555, int),
_convert({"title": "War and Peace", "pages": 923}, Book),
_convert([1, 2, 3, 4], List))(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.
find_page(book=Book("War and Peace", 950), percent=50)475
tool(find_page)({"title": "War and Peace", "pages": 950}, percent=50)475
By passing tools wrapped by tool(), user-defined types now work completes without failing in tool calls.
pr = "How many pages do I have to read to get halfway through my 950 page copy of War and Peace"
tools = tool(find_page)
tools<function __main__.find_page(book: __main__.Book, percent: int) -> int>
r = c(pr, tools=[tools])
find_block(r, ToolUseBlock)ToolUseBlock(id='toolu_01C4PLJtgj6rktLBoabzu2vG', input={'book': {'title': 'War and Peace', 'pages': 950}, 'percent': 50}, name='find_page', type='tool_use')
tr = mk_toolres(r, ns=[tools])
tr[{'role': 'assistant',
'content': [{'id': 'toolu_01C4PLJtgj6rktLBoabzu2vG',
'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_01C4PLJtgj6rktLBoabzu2vG',
'content': '475'}]}]
msgs = [pr]+tr
contents(c(msgs, sp=sp, tools=[tools]))'To get halfway through your 950-page copy of War and Peace, you need to read **475 pages**.'
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.
sp = "Never mention what tools you use."
chat = Chat(model, sp=sp)
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.costchat.cost0.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`.")
pr = self.cont_pr # No user prompt, keep the chain
if pr: self.h.append(mk_msg(pr, cache=self.cache))Exported source
@patch
def _append_pr(self:Chat, pr=None):
prev_role = nested_idx(self.h, -1, 'role') if self.h else 'assistant' # First message should be 'user'
if pr and prev_role == 'user': self() # already user request pending
self._post_pr(pr, prev_role)Exported source
@patch
def __call__(self:Chat,
pr=None, # Prompt / message
temp=None, # Temperature
maxtok=4096, # Maximum tokens
maxthinktok=0, # Maximum thinking tokens
stream=False, # Stream response?
prefill='', # Optional prefill to pass to Claude as start of its response
tool_choice:Optional[dict]=None, # Optionally force use of some tool
**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,
tools=self.tools, tool_choice=tool_choice, cb=_cb, **kw)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 = Chat(model, sp=sp)chat("I'm Jeremy")
chat("What's my name?")Your name is Jeremy! You told me that when we started our conversation.
- id:
msg_01SbfLJTbZTUqaGhPfisiDfW - content:
[{'citations': None, 'text': 'Your name is Jeremy! You told me that when we started our conversation.', 'type': 'text'}] - model:
claude-sonnet-4-5-20250929 - role:
assistant - stop_reason:
end_turn - stop_sequence:
None - type:
message - usage:
{'cache_creation': {'ephemeral_1h_input_tokens': 0, 'ephemeral_5m_input_tokens': 0}, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 51, 'output_tokens': 18, 'server_tool_use': None, 'service_tier': 'standard'}
chat.use, chat.cost(In: 68; Out: 44; Cache create: 0; Cache read: 0; Total Tokens: 112; Search: 0,
0.000864)
Let’s try out prefill too:
q = "Very concisely, what is the meaning of life?"
pref = 'According to Douglas Adams,'chat(q, prefill=pref)According to Douglas Adams, it’s 42.
More seriously: to find purpose through connection, growth, and contributing something meaningful to others.
- id:
msg_01WXQKBxnxtqniY5LZmwdndW - content:
[{'citations': None, 'text': "According to Douglas Adams, it's 42. \n\nMore seriously: to find purpose through connection, growth, and contributing something meaningful to others.", 'type': 'text'}] - model:
claude-sonnet-4-5-20250929 - role:
assistant - stop_reason:
end_turn - stop_sequence:
None - type:
message - usage:
{'cache_creation': {'ephemeral_1h_input_tokens': 0, 'ephemeral_5m_input_tokens': 0}, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 89, 'output_tokens': 29, '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.
chat.cont_pr = "Tell me a little more..."
chat()The meaning of life is deeply personal, but common threads emerge:
Connection - We find meaning through relationships, love, and belonging. Humans are social creatures who thrive when bonded to others.
Growth - Learning, overcoming challenges, and becoming more than we were yesterday gives life direction and satisfaction.
Contribution - Creating something, helping others, leaving things better than we found them. Meaning often comes from looking beyond ourselves.
Experience - Simply being present - savoring beauty, joy, even struggle. The richness of conscious existence itself.
Purpose - Having something to strive toward, whether that’s raising a family, mastering a craft, pursuing justice, or exploring ideas.
Many philosophers suggest we create meaning rather than discover it - that life is what we make of it through our choices and values. The “meaning” isn’t handed to us; it emerges from how we engage with the world and what we decide matters.
What resonates with you, Jeremy?
- id:
msg_01UWMgALUQCscQir2jtb87fs - content:
[{'citations': None, 'text': 'The meaning of life is deeply personal, but common threads emerge:\n\n**Connection** - We find meaning through relationships, love, and belonging. Humans are social creatures who thrive when bonded to others.\n\n**Growth** - Learning, overcoming challenges, and becoming more than we were yesterday gives life direction and satisfaction.\n\n**Contribution** - Creating something, helping others, leaving things better than we found them. Meaning often comes from looking beyond ourselves.\n\n**Experience** - Simply being present - savoring beauty, joy, even struggle. The richness of conscious existence itself.\n\n**Purpose** - Having something to strive toward, whether that\'s raising a family, mastering a craft, pursuing justice, or exploring ideas.\n\nMany philosophers suggest we *create* meaning rather than discover it - that life is what we make of it through our choices and values. The "meaning" isn\'t handed to us; it emerges from how we engage with the world and what we decide matters.\n\nWhat resonates with you, Jeremy?', 'type': 'text'}] - model:
claude-sonnet-4-5-20250929 - role:
assistant - stop_reason:
end_turn - stop_sequence:
None - type:
message - usage:
{'cache_creation': {'ephemeral_1h_input_tokens': 0, 'ephemeral_5m_input_tokens': 0}, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 127, 'output_tokens': 219, 'server_tool_use': None, 'service_tier': 'standard'}
We can also use streaming:
chat = Chat(model, sp=sp)
for o in chat("I'm Jeremy", stream=True): print(o, end='')Hello Jeremy! Nice to meet you. How are you doing today? Is there anything I can help you with?
r = chat(q, prefill=pref, stream=True)
for o in r: print(o, end='')
r.valueAccording to Douglas Adams, it's 42.
More seriously: to find meaning through connection, growth, and contributing something beyond yourself.
According to Douglas Adams, it’s 42.
More seriously: to find meaning through connection, growth, and contributing something beyond yourself.
- id:
msg_01EVuKVkDzMM3eB6QbQmgstY - content:
[{'citations': None, 'text': "According to Douglas Adams, it's 42.\n\nMore seriously: to find meaning through connection, growth, and contributing something beyond yourself.", 'type': 'text'}] - model:
claude-sonnet-4-5-20250929 - role:
assistant - stop_reason:
end_turn - stop_sequence:
None - type:
message - usage:
{'cache_creation': {'ephemeral_1h_input_tokens': 0, 'ephemeral_5m_input_tokens': 0}, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 63, 'output_tokens': 27, 'server_tool_use': None, 'service_tier': 'standard'}
You can provide a history of messages to initialise Chat with:
chat = 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?')I actually didn’t know - I was just making a playful guess! “Merlin G. Penfolds” was a completely random name I made up.
Is that really your name, or are you playing along with me? If it actually is your name, that would be an absolutely wild coincidence!
- id:
msg_015GNjsYKrEcMnE2s3wqxECB - content:
[{'citations': None, 'text': 'I actually didn\'t know - I was just making a playful guess! "Merlin G. Penfolds" was a completely random name I made up. \n\nIs that really your name, or are you playing along with me? If it actually is your name, that would be an absolutely wild coincidence!', 'type': 'text'}] - model:
claude-sonnet-4-5-20250929 - role:
assistant - stop_reason:
end_turn - stop_sequence:
None - type:
message - usage:
{'cache_creation': {'ephemeral_1h_input_tokens': 0, 'ephemeral_5m_input_tokens': 0}, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 58, 'output_tokens': 70, 'server_tool_use': None, 'service_tier': 'standard'}
Chat tool use
We automagically get streamlined tool use as well:
pr = f"What is {a}+{b}?"
pr'What is 604542+6458932?'
chat = Chat(model, sp=sp, tools=[sums])
r = chat(pr)
rFinding the sum of 604542 and 6458932
ToolUseBlock(id=‘toolu_0156PbBN1a7y1VFD4YuBPp9x’, input={‘a’: 604542, ‘b’: 6458932}, name=‘sums’, type=‘tool_use’)
- id:
msg_017i1HX51CG9a7hfKvFT5JDV - content:
[{'id': 'toolu_0156PbBN1a7y1VFD4YuBPp9x', 'input': {'a': 604542, 'b': 6458932}, 'name': 'sums', 'type': 'tool_use'}] - model:
claude-sonnet-4-5-20250929 - role:
assistant - stop_reason:
tool_use - stop_sequence:
None - type:
message - usage:
{'cache_creation': {'ephemeral_1h_input_tokens': 0, 'ephemeral_5m_input_tokens': 0}, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 620, '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()The sum of 604542 + 6458932 is 7,063,474.
- id:
msg_016aVUB1HeyqED7vBQziprpB - content:
[{'citations': None, 'text': 'The sum of 604542 + 6458932 is **7,063,474**.', 'type': 'text'}] - model:
claude-sonnet-4-5-20250929 - role:
assistant - stop_reason:
end_turn - stop_sequence:
None - type:
message - usage:
{'cache_creation': {'ephemeral_1h_input_tokens': 0, 'ephemeral_5m_input_tokens': 0}, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 713, 'output_tokens': 24, '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:
a+b7063474
Let’s try the same thing with streaming:
chat = Chat(model, sp=sp, tools=[sums])
r = chat(pr, stream=True)
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.valueToolUseBlock(id=‘toolu_01VRFyr4Rn6HHjVSXBLmdoZe’, input={‘a’: 604542, ‘b’: 6458932}, name=‘sums’, type=‘tool_use’)
- id:
msg_01E8hssNQ2r4wvZrYmQFS6UE - content:
[{'id': 'toolu_01VRFyr4Rn6HHjVSXBLmdoZe', 'input': {'a': 604542, 'b': 6458932}, 'name': 'sums', 'type': 'tool_use'}] - model:
claude-sonnet-4-5-20250929 - role:
assistant - stop_reason:
tool_use - stop_sequence:
None - type:
message - usage:
{'cache_creation': {'ephemeral_1h_input_tokens': 0, 'ephemeral_5m_input_tokens': 0}, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 620, 'output_tokens': 72, 'server_tool_use': None, 'service_tier': 'standard'}
r = chat(stream=True)
for o in r: print(o, end='')The sum of 604542 + 6458932 is **7,063,474**.
r.valueThe sum of 604542 + 6458932 is 7,063,474.
- id:
msg_01H6B8tR27YcTr2Yobk9e3Y7 - content:
[{'citations': None, 'text': 'The sum of 604542 + 6458932 is **7,063,474**.', 'type': 'text'}] - model:
claude-sonnet-4-5-20250929 - role:
assistant - stop_reason:
end_turn - stop_sequence:
None - type:
message - usage:
{'cache_creation': {'ephemeral_1h_input_tokens': 0, 'ephemeral_5m_input_tokens': 0}, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 713, 'output_tokens': 24, '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_01VRFyr4Rn6HHjVSXBLmdoZe',
'input': {'a': 604542, 'b': 6458932},
'name': 'sums',
'type': 'tool_use'}]},
{'role': 'user',
'content': [{'type': 'tool_result',
'tool_use_id': 'toolu_01VRFyr4Rn6HHjVSXBLmdoZe',
'content': 'MySum(val=7063474)'}]},
{'role': 'assistant',
'content': [{'citations': None,
'text': 'The sum of 604542 + 6458932 is **7,063,474**.',
'type': 'text'}]}]
Let’s test a function with user defined types.
chat = Chat(model, sp=sp, tools=[find_page])
r = chat("How many pages is three quarters of the way through my 80 page edition of Tao Te Ching?")
rToolUseBlock(id=‘toolu_019Cc2pacZoZ4voqnA3JK4c5’, input={‘book’: {‘title’: ‘Tao Te Ching’, ‘pages’: 80}, ‘percent’: 75}, name=‘find_page’, type=‘tool_use’)
- id:
msg_012ZLiQV1ozrhizZxLY92mNd - content:
[{'id': 'toolu_019Cc2pacZoZ4voqnA3JK4c5', 'input': {'book': {'title': 'Tao Te Ching', 'pages': 80}, 'percent': 75}, 'name': 'find_page', 'type': 'tool_use'}] - model:
claude-sonnet-4-5-20250929 - role:
assistant - stop_reason:
tool_use - stop_sequence:
None - type:
message - usage:
{'cache_creation': {'ephemeral_1h_input_tokens': 0, 'ephemeral_5m_input_tokens': 0}, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 730, '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 is page 60.
- id:
msg_018SAcnBrSuuiWa8xT1Lppnz - content:
[{'citations': None, 'text': 'Three quarters of the way through your 80-page edition of Tao Te Ching is page 60.', 'type': 'text'}] - model:
claude-sonnet-4-5-20250929 - role:
assistant - stop_reason:
end_turn - stop_sequence:
None - type:
message - usage:
{'cache_creation': {'ephemeral_1h_input_tokens': 0, 'ephemeral_5m_input_tokens': 0}, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 830, 'output_tokens': 28, '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'
last_msg = contents(self.c.result)
def fmt_msg(m):
t = contents(m)
if isinstance(t, dict): return t['content']
return t
history = '\n\n'.join(f"**{m['role']}**: {fmt_msg(m)}"
for m in self.h)
det = self.c._repr_markdown_().split('\n\n')[-1]
if history: history = f"""
<details>
<summary>► History</summary>
{history}
</details>
"""
return f"""{last_msg}
{history}
{det}"""# TODO: fix history formatchatThree quarters of the way through your 80-page edition of Tao Te Ching is page 60.
► History
user: H
assistant: {‘id’: ‘toolu_019Cc2pacZoZ4voqnA3JK4c5’, ‘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 is page 60.
| Metric | Count | Cost (USD) |
|---|---|---|
| Input tokens | 1,560 | 0.004680 |
| Output tokens | 114 | 0.001710 |
| Cache tokens | 0 | 0.000000 |
| Server tool use | 0 | 0.000000 |
| Total | 1,674 | $0.006390 |
chat = Chat(model, tools=[text_editor_conf['sonnet']], ns=mk_ns(str_replace_based_edit_tool))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.
r = chat('Please explain very concisely what my _quarto.yml does. It is in the current path. Use your tools')
find_block(r, ToolUseBlock)ToolUseBlock(id='toolu_016nQmu59mG21Ck5u4wjPZL3', input={'command': 'view', 'path': '.'}, name='str_replace_based_edit_tool', type='tool_use')
chat()ToolUseBlock(id=‘toolu_012VDLH46pmrYuD6bPtBTp2s’, input={‘command’: ‘view’, ‘path’: ’/_quarto.yml’}, name=‘str_replace_based_edit_tool’, type=‘tool_use’)
- id:
msg_01Vu3rW5fXgQYnoYE1g7pJ3b - content:
[{'id': 'toolu_012VDLH46pmrYuD6bPtBTp2s', 'input': {'command': 'view', 'path': '/_quarto.yml'}, 'name': 'str_replace_based_edit_tool', 'type': 'tool_use'}] - model:
claude-sonnet-4-5-20250929 - role:
assistant - stop_reason:
tool_use - stop_sequence:
None - type:
message - usage:
{'cache_creation': {'ephemeral_1h_input_tokens': 0, 'ephemeral_5m_input_tokens': 0}, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 3714, 'output_tokens': 80, '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
fn = Path('samples/puppy.jpg')
Image(filename=fn, width=200)
img = fn.read_bytes()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…
q = "In brief, what color flowers are in this image?"
msg = mk_msg([img, q])c([msg])The flowers in this image are purple.
- id:
msg_011fqyUyucLCtDf9mke8Fa2K - content:
[{'citations': None, 'text': 'The flowers in this image are **purple**.', 'type': 'text'}] - model:
claude-sonnet-4-5-20250929 - role:
assistant - stop_reason:
end_turn - stop_sequence:
None - type:
message - usage:
{'cache_creation': {'ephemeral_1h_input_tokens': 0, 'ephemeral_5m_input_tokens': 0}, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 110, 'output_tokens': 12, '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.
c(["How are you?", r])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:
c(["How are you?", r, [img, q]])c = Chat(model)
c([img, q])The flowers in this image are purple.
- id:
msg_011fqyUyucLCtDf9mke8Fa2K - content:
[{'citations': None, 'text': 'The flowers in this image are **purple**.', 'type': 'text'}] - model:
claude-sonnet-4-5-20250929 - role:
assistant - stop_reason:
end_turn - stop_sequence:
None - type:
message - usage:
{'cache_creation': {'ephemeral_1h_input_tokens': 0, 'ephemeral_5m_input_tokens': 0}, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 110, 'output_tokens': 12, 'server_tool_use': None, 'service_tier': 'standard'}
contents(c.h[0])'*Media Type - image*'
cThe 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 | 12 | 0.000180 |
| Cache tokens | 0 | 0.000000 |
| Server tool use | 0 | 0.000000 |
| Total | 122 | $0.000510 |
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.
mk_msg(['hi', 'there'], cache=True){ '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 = Chat(model, sp=sp, cache=True)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.
chat("Hi, I'm Jeremy.")Hello Jeremy! Nice to meet you. How are you doing today? Is there anything I can help you with?
- id:
msg_01YZZxrch65BWrDiKQs4eSam - content:
[{'citations': None, 'text': 'Hello Jeremy! Nice to meet you. How are you doing today? Is there anything I can help you with?', 'type': 'text'}] - model:
claude-sonnet-4-5-20250929 - role:
assistant - stop_reason:
end_turn - stop_sequence:
None - type:
message - usage:
{'cache_creation': {'ephemeral_1h_input_tokens': 0, 'ephemeral_5m_input_tokens': 0}, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 20, 'output_tokens': 26, 'server_tool_use': None, 'service_tier': 'standard'}
chat.useIn: 20; Out: 26; Cache create: 0; Cache read: 0; Total Tokens: 46; Search: 0
Note the usage: no cache is created, nor used. Now, let’s send a long enough message to trigger caching.
chat("""Lorem ipsum dolor sit amet""" * 150)I see you’ve sent me a lot of repeated “Lorem ipsum dolor sit amet” text - that’s the classic placeholder text often used in design and publishing!
Was there something specific you wanted to discuss or ask about, Jeremy? Or were you perhaps testing something? I’m here to help with whatever you need.
- id:
msg_01UdXDvuTDHc2dY2YsGrtnFm - content:
[{'citations': None, 'text': 'I see you\'ve sent me a lot of repeated "Lorem ipsum dolor sit amet" text - that\'s the classic placeholder text often used in design and publishing!\n\nWas there something specific you wanted to discuss or ask about, Jeremy? Or were you perhaps testing something? I\'m here to help with whatever you need.', 'type': 'text'}] - model:
claude-sonnet-4-5-20250929 - role:
assistant - stop_reason:
end_turn - stop_sequence:
None - type:
message - usage:
{'cache_creation': {'ephemeral_1h_input_tokens': 0, 'ephemeral_5m_input_tokens': 1094}, 'cache_creation_input_tokens': 1094, 'cache_read_input_tokens': 0, 'input_tokens': 3, 'output_tokens': 67, 'server_tool_use': None, 'service_tier': 'standard'}
chat.useIn: 23; Out: 93; Cache create: 1094; Cache read: 0; Total Tokens: 1210; 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.
chat("Oh thank you! Sorry, my lorem ipsum generator got out of control!")No worries at all, Jeremy! Those things can definitely get a mind of their own sometimes. 😄 It happens!
So, now that we’ve got that sorted - what can I actually help you with today? Whether it’s answering questions, brainstorming ideas, or just having a chat, I’m all ears!
- id:
msg_01GzfzcdUfYMbLwiv88zxX6r - content:
[{'citations': None, 'text': "No worries at all, Jeremy! Those things can definitely get a mind of their own sometimes. 😄 It happens!\n\nSo, now that we've got that sorted - what can I actually help you with today? Whether it's answering questions, brainstorming ideas, or just having a chat, I'm all ears!", 'type': 'text'}] - model:
claude-sonnet-4-5-20250929 - role:
assistant - stop_reason:
end_turn - stop_sequence:
None - type:
message - usage:
{'cache_creation': {'ephemeral_1h_input_tokens': 0, 'ephemeral_5m_input_tokens': 85}, 'cache_creation_input_tokens': 85, 'cache_read_input_tokens': 1094, 'input_tokens': 3, 'output_tokens': 74, 'server_tool_use': None, 'service_tier': 'standard'}
chat.useIn: 26; Out: 167; Cache create: 1179; Cache read: 1094; Total Tokens: 2466; 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.
thinking={ "type": "enabled", "budget_tokens": 16000 }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`."
blk = find_block(r)
if show_thk:
tk_blk = find_block(r, blk_type=ThinkingBlock)
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 = Chat(model)chat("Write a sentence about Python!")Python is a versatile, high-level programming language known for its clean syntax and readability, making it popular for everything from web development to data science and machine learning.
- id:
msg_013yMES6TZEhsGDXBFGC9xf8 - 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 to data science and machine learning.', 'type': 'text'}] - model:
claude-sonnet-4-5-20250929 - role:
assistant - stop_reason:
end_turn - stop_sequence:
None - type:
message - usage:
{'cache_creation': {'ephemeral_1h_input_tokens': 0, 'ephemeral_5m_input_tokens': 0}, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 13, 'output_tokens': 38, 'server_tool_use': None, 'service_tier': 'standard'}
Now, let’s call the model with extended thinking enabled.
chat("Write a sentence about Python!", maxthinktok=1024)Python’s extensive standard library and vast ecosystem of third-party packages make it an excellent choice for beginners and experienced developers alike.
Thinking
The user is asking me to write another sentence about Python. I should write a different sentence than before to provide variety.- id:
msg_013wXiJbSVz9Nc5vTD7R5v7Z - content:
[{'signature': 'EqoCCkYICRgCKkBQB7PJH8fZPgTfBGbWKFq9xRPuQI4TpQ5IHN5KDZmYu5bF7CU0jq3gAkpKXdznIM0MggxsuKZ/KV31KHh+HJTBEgw45GDws13hgo3DKXoaDPTCerdY+hLN1fMCeyIwntAHJ/ILI9uDvbX9mwka6JLYpuu2XtSWx41YAWykvq2g/97GPnAUWFLCIL/Qo2efKpEBuRNRgKAU7LpGCEjZt/J1+nKIEI6phhKbovsBSPj7zg0e72Ts2gyxXtJnaLdWvNjnz+ujpRmVW3DDb62OptFFrslxKrneMqVaVw9RywRm8EdcktWMvJhnR412k94ueawbskXlLQ5qwZo6gYpDvZj6g+LTT6O4Vrgf1eVvR4Q8ANDV9JPTRhlPsSLbSmikCQnh/BgB', 'thinking': 'The user is asking me to write another sentence about Python. I should write a different sentence than before to provide variety.', 'type': 'thinking'}, {'citations': None, 'text': "Python's extensive standard library and vast ecosystem of third-party packages make it an excellent choice for beginners and experienced developers alike.", 'type': 'text'}] - model:
claude-sonnet-4-5-20250929 - role:
assistant - stop_reason:
end_turn - stop_sequence:
None - type:
message - usage:
{'cache_creation': {'ephemeral_1h_input_tokens': 0, 'ephemeral_5m_input_tokens': 0}, 'cache_creation_input_tokens': 0, 'cache_read_input_tokens': 0, 'input_tokens': 88, 'output_tokens': 62, '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 TextBlocks comprised of response text from the model, ServerToolUseBlock and server tool results block such as WebSearchToolResultBlock. Some of these TextBlocks 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 TextBlocks, with citations.
contents
contents (r, show_thk=True)
Helper to get the contents from Claude response r.
chat = Chat(model, sp='Be concise in your responses.', tools=[search_conf()], cache=True)
pr = 'What is the weather in San Diego?'
r = chat(pr)
rThe weather in San Diego today (Tuesday, November 4) is cloudy giving way to sun with a high of 69°F 1 and a low of 59°F tonight 2. There are areas of dense morning fog 3, and conditions are expected to be partly cloudy with a high of 69°F 4. Winds are from the west at 5 to 10 mph 5.
- id:
msg_01BXNzz3CRitC4iVkFftFyV1 - content:
[{'id': 'srvtoolu_01NktSQtP5NmFjjAA8w7iLLq', 'input': {'query': 'San Diego weather today'}, 'name': 'web_search', 'type': 'server_tool_use'}, {'content': [{'encrypted_content': 'EqwVCioICRgCIiQ4ODk4YTFkYy0yMTNkLTRhNmYtOTljYi03ZTBlNTUzZDc0NWISDOsVec+0RHdngeIVmRoMLksaw61QftOYYIp7IjA1eEsFO8XMgDKT5HUosWBFCi7JMpb2bsNvKn8/Q9RZQ1Du708ep/clCOzlJ3BQhacqrxS0rH1m8XXVvMp9iuLUatRjbz5tcP1E54NS2boWOX68g//JIKlN4S6TAWys2MNi8bsv0P9sUOPX9RmiK8F0iXhUrt0OB7OlsztY0Bm/3CLPv1hkvXkVsmpgODhi7EfftwZhOb7+yguwujJIua7M68937JaSv2mCEfP6MoDUWQzHwKPKpTtU8UJTPDcvyfVEeSdv3HmGPXTz+O+C9S9/qF7XfNqvglKypk2CSHbjrDTnRiVGn/5rw5zGfbU9+sw8pLzLZ3SJPBxxu2grCST3GUtQQdw05C1uXSGxrMJ7+lPE+JK8tj03bbn9mhwu0J9za4KczKwAIt1Ag+CokknQBEFcZVG8iaAorY0pLQGnuaiQIeK+3n3gJupbUMK3RWJPDb28mYQhlBeBiiCE4mBtbzcjzLW6Q5ctJs6S2048EKxk549H42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ER1ZSzyyJNcpcWICJTcNKIDpIzT5jcR+ni4AOhLmyypjL+mxDl7lzGMHbZZ26X1xLu8uWvMmeNcKAQGfla9imfXbJMG1v+w6u41k7vpZpgdfzX1RIWfeeFhYH9TT0QiElGWLto/C6aaG3F3HjjVqd9sAG3bDDJPEa6Dj3OApbtgDyi28nAqA1kPlPecgbah3bkd7JRWrew+k0obaL2hqXI46ue0oNhH11tlffRAQ1G0qztzLxwPTJI0bJdtddEukHNuF7S2bc2eIOQWHbZmj0mEy6nfgVsdXnogzCBCqObe8BbzhKxvMtHOzcnRpEqB7+CWNm3Ymn+kd4LdTMezc2KvaPyCxmGM1X1eYkJriQSSHvBn2smq2vZA054jIRGG954Aa1iklAeE0U+9e7Wc2Qf5tNtyclDF+h9FYqNw/4eHPxKWV9bP195bs+ymPohE9tUOy/5pCj08rELSzVBbxoO4k/6K7xQtv7mEcgv+sk/2lT5kT+N3zfNG1DKszvqUkeVKQSsFTrzjw7090ciVQhRcDGFK4GDMIXTAuP6GsKVe6yOvkjBW1Rv85DhtE889QHvnMFWjjsGioLgEydMJ674zZZwtwTos8PHBs+i9nD+pq6bLGe1kaAVrW6Eunb/xEeGk+XMaujDUBCEnk9Rd1Li2tZFf8pL5DSRLuVwQdis+b/Kwb/VIrOM+sY1B39ZFglMtbmBnN8LpPiUtTU2Z2wd6cnWPQmXUXKDjUosXJVgEvx3ymiuvaF8xTlLkDwEXRYj2HsdHfwQ3ElNy+FCzlQrGcnATeJ9/EDpq8w3NYQgwERRR3W3CqwX5X35c/MZxCfAVWdMgx6K/FBQiy9ueKdtedVBDkhmmI67QmQtlBnZxCAIA/PJDREAmlRR6db+29cIuacYAw==', 'page_age': '2 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': 'Er0ECioICRgCIiQ4ODk4YTFkYy0yMTNkLTRhNmYtOTljYi03ZTBlNTUzZDc0NWISDOs1miuwCK6N+6roiRoMPxaOj6YUCf1ECabQIjDaPsgp9Iga6lMiA0FsUp5M1jifxEcafjQpiEOSr+jb4zugTlWLF7U5nRF8FY+CPucqwAPuaTVvDBTEfXP9WiL/lUpcKro0VBxEF+a2KPSfoL+AggdXli2D4Y1u3yB3ARbdzwJuy8W7pAwdMhWOY9DKtTAvj/1yaJuHVq3snRxO6aEVyLZPT7iVyt3rmlrxKt4hlFYd1qVue7prfRLMpAfuj9H0BNGEFd9b5zy9MbR5yapRUUznPsF6sCCw1e7ixONQkk6hQ9WJWHU1bun8Hr8gFO8xvZvMlkheswtvNQzwQE+A+QexDNzqPhf59H576ejWDejkmXnoi89mZecd0OoyyBOg9XAxYmLzJ1PdUjZD7UoeOFMSXArEEKBZEsF5ZdJ4eqqmiBgONvdXCo0EaYVNoOlZOh7RRIr2zK6LxlSlRFg+paXa9gRm3zhc+ScA+e4rmvb/UEzEDCAk4lIIw04bl53sPuQ5/B6CDOy7iEdpO9sFzL/5UtAiz8+CUSOLYLXDs3YXuC1YeE4r1t5vl1W3Ea/6MJF+jVjX7YYslPf2odcfYwZvsJRQ6hOUOVQ5TA34ivcR+qmtZeogwvt/oGTKwrGRivBiEgCiQK0f/Ey/WNmhNj5QNAt6HatMO1RUGTMi9yjk718gaFMMgrV32WNRae7kGAM=', 'page_age': None, 'title': 'San Diego weather forecast – NBC 7 San Diego', 'type': 'web_search_result', 'url': 'https://www.nbcsandiego.com/weather/'}, {'encrypted_content': 'EvcGCioICRgCIiQ4ODk4YTFkYy0yMTNkLTRhNmYtOTljYi03ZTBlNTUzZDc0NWISDPBuFsrdOOnc59keexoMCGlVnPeuvtM0G1iqIjDqCtvKL+9R+9J1a06ZZXqXA/7awzu06TgZ7FxYoTjNMJt+ERcdsxQqy8xjw/p3oK4q+gXEIruqvzM8wlc7XJbw2tKWNV17MvoLCBr+opnHIUjyLw10D9M9vBd2hovKr7AsNCPr3Y0j/e5dka7x/+SvoT78Vp+QF8IWGzHe16DTCPGRdDiOpxgZxxNgP6Y5HHHbdZ5UH26BlzTi0RYhshOWoiPwh3xzEzK3e/sAd/dUx37khNBu/rjB6vLs3DPQGfPHiSH1v/Olc66ogjREG7a8sz4wCfinSM51UPQD61zDHc7ZfJc8Lf+pZ/vnJaneVx1q8vm5Y+zvzpmDilUZp8OAvWZEJ3+JbIs/GPtzXeaRS64IjObdw2UpoRiqalNvhPzWX0/t1++k6vTiP5PHVl1D5nQADj9liznOU55Lp3KOX6SkHV4baanbl3/fZMujRJhcwwJmsaDpc5BCkbCFo/OXoipGke8euqndrhPgVVRdZopmhOJZ8eSrEmsO1SqeBFlNcTjs2kY5E/y05lpOCfCcZPMZHM9SBWrUipfH9qMNQJcBpKpNJNE2Y0rviYXZwZGDnD+LIYXRUmxUSbRu8UlI/MCuKTBgdshPFp4CaZxdVpwBQ7thWYyQ4tmzAXQvnZkJej9olFzYogdvDABF0aj09PjD7BIExc55EKPtJ+2SZcpSe6UqE1upuziWFec5ekaXH6ClkkmoaYa85nEQEbHJz0faoVfkBL1wXYoa3llPTW9LEH6/4SJpOD745WJBDOrdpuaObGVG9EEb/zKSSMtoyZnoWKi+wKYdJjtzBPcrWKbpDVa6NSPV9pZKuj/B7e952Sba6Y2CY0PzoMh+LArjwVc9Q+l3bg2oJcZ4IXAxehX67S5mS8OWKE0PZHufjeDKQCjWAED4KmTj8vK5CCGw4m3PtVi4F4g3Qrht6Zud4kSEGjmfyYKLaIUCKxqSZDDp0iaBKIbo3MH8iaXYtP47Zf8p/MBLGY1qRoLCqj23kar8Lt61RviKNFEV4pp1L4Uu5On05gYfUUUj//dPiaCGCAVb92710eWZQtNC1HS8pXZFyC4S1OhTHBKY2uMYAw==', 'page_age': None, 'title': 'San Diego, CA Hourly Weather Forecast | Weather Underground', 'type': 'web_search_result', 'url': 'https://www.wunderground.com/hourly/us/ca/san-diego'}, {'encrypted_content': 'EpkCCioICRgCIiQ4ODk4YTFkYy0yMTNkLTRhNmYtOTljYi03ZTBlNTUzZDc0NWISDK0widPjf1RM8sbCtRoM7V134UKitP2+u+yZIjB8KzC75xkQ1eC+zQbPK+kb2Yl/SVXic6NX/I2J1h46I6dqwhtQBcMO5ENuN1mwIAsqnAHEJSk2ON/UaiFU8RxYRBTRG0CpYosE6TC58SKhXAGFpDBpUpdb2/khDbD37TYf3wR2Fq31vZVUMippWuVSeb0eVh62NnFzclNisF9YivjOQav4LBUdnZKu5lOLu1SHR8tTPEgvYf5U60IPkf8m9AMr6HVlBKBQK8PK+tLpbEz5wYS5ncbgbP5cDQfA6q7OCDs4gmtHBpDlWs8H46YYAw==', 'page_age': None, 'title': 'San Diego, CA Weather Forecast | KGTV | kgtv.com', 'type': 'web_search_result', 'url': 'https://www.10news.com/weather'}, {'encrypted_content': 'Ep0JCioICRgCIiQ4ODk4YTFkYy0yMTNkLTRhNmYtOTljYi03ZTBlNTUzZDc0NWISDGgUhCY1RlCKpOY+/BoMDYlxxep/aBs696wbIjA7qQTmSBlULhPxwytpWa3g7qoozv+DtzW2MLcB3vRsSrRVY4bNYxujuxvvYS18eVIqoAhadm0YV7BRv0voFVXNXbh4iqvbFn5g+0SUa8fzsuMrhcDDjPwWT232TfKg1Jf2KlppFIqyyx87O7GPswtD73YcvYXd9lHwGvOvN5R2O/L6g2/xyBAZSZnbL8Boxfbfy4GbCBSZgPIcbKtgRc0O2RLGc4L87/6zLzuNtHB7/23dEulByX2eoGSNfrJj0Lxc63RRewFvonG7BFMcYxtDvc07el5/AEiEY/26uR03Y3AtCg+LhCw0Yo+9Gc6XKBLsYxHYsqQ9KRBSxRJjcm62SLVa0rPBaeiV+W7wVH+yxggcKTUaBdHavLrZSUw9E1PgJcviI4o0Zmmc7Dtn1bOYRuTGjq8K/slxfqR1OHa7SbiVAfQcEKVtu4KDoP31U6OCrEYmQyk1dC4NC0wiCBcAPglRnvd0C/ShPKC7hNnaoSSZn3eKN8oOhfw7r3j63ax5EnlT1J1TqShm0KC2wdrzZz594SxAm8AGGK2quLPagWyK5fU/asD4YeEuczuwrBEwSqBG5R6O3NcBU03X8bo6rHdfMdUICdA7zDpMkvXbCIjKEcEIUacXYfSbe5n0uelW+zfWYJSf4Z6eXiDyZGG1UXECVz83G710p4lkF4QniFgeO0ZoFsjiN5SFZ+RQ/6vPZoHqZ4mIZtnqgQqtOR4e/bCBTU3OXLJruWQ3ks+a/n1rfbQt9NbF1NHGrFLoZLNWSF58GVuvEk2R0xjVBx6ysOoBBiEjta+Onrmtq3rj8a2c9xdD5GhTo5+7gmHLkWW8ijyBddndZ6FV+pzEP1MlElm6BGqUTrNnJdIthNjj7A07VqqKuTqo8M9DZy6MQBaBuGbahzoH1kZ/QLtSxF+Yi84cPdkg5wQBlsO9Cd70kvvjp+78C8LVaMQgPHk1J3BROa/IqvdfWjqU6XuAqJo2VbLBP//U4SLQ1+SUjJOjGT6bUr0mUwU9qR/kG9h+gJN2mDIdU2UfvLuLD1o0mcXoJZNBbrq3LVLg1BpIC3vrxFW2aCJUTrh4euo+iXuPPDKTLvB81X34VOgqfKEc2RFMRqR/TuQ3R+D8N15LpXWC0aGD0XGLohByRSD0LFZrzPRlle+2cWjFrIqJROaWl1tV6U8dzQ+jUg6umfhCIV9ljMiCuxbsemCs8Dhan/QjEvCJn3qGmPrgzkrLE0FyVOb7yrvCdfAbSFh+BvTPU8AGS79D8FQpY98zCXfWVi00vg7cjk3dnhPll0NHRopipdPgMaWvN2R8j+g3lwBPOGlad67zy1G6iaIJGh3fS/v+knN92hnBerJusi5tkC+GsartsXTvzlSSZbNiOsYPBG5xTQszXR06wEGVIQpnwl2XRreWa0IoEwsMTplcYPSvnepjYZsE04qRQ7u8sgd6CjWhtigKm/tdLZ1fpjDebV+6+t3aYY4YAw==', 'page_age': 'August 30, 2021', 'title': 'San Diego 7-Day Weather Forecast | FOX 5 San Diego', 'type': 'web_search_result', 'url': 'https://fox5sandiego.com/weather/forecast/'}, {'encrypted_content': 'EsoCCioICRgCIiQ4ODk4YTFkYy0yMTNkLTRhNmYtOTljYi03ZTBlNTUzZDc0NWISDDT3ddADKqKVfEa8QBoMxjdY0AuEp2dTv1fAIjD6BJaw0nJ9/3rB02WqHstq1ObR5Kuu+pJGQ7zYKS+Axm3dfQpdbaXBZyq79RRzlc8qzQGmOlv+v/a1hI70CCgz8m9dtnQ0GCiw0GiKlVlHGBeI1O8mhEPfL65nvmBnOYKtCGwzRA1yNpGGsbbCtt37lvoppMhJGxaNWR6nwVblIEhKgzkoKd8ev7ouCUGegxqC4QqIuaVGKS4M4oLONqUbN4XOF4KO40YNZiCFEP/81GokLVxiz0jnJYKVetF0TJxtv9P9aUP6svyxR1FBdd7FspBXISg+89voDI/A44jltsYUuSWLumv0NiEMUtplvQTa7UKbXUhKXe87WzWEqJu2GAM=', 'page_age': '2 days ago', 'title': 'San Diego, CA', 'type': 'web_search_result', 'url': 'https://www.weather.gov/sgx/'}, {'encrypted_content': 'EskKCioICRgCIiQ4ODk4YTFkYy0yMTNkLTRhNmYtOTljYi03ZTBlNTUzZDc0NWISDFnEeLjcLvxiB5jdBxoMht6AvB/wKYwi0VsGIjAK/mAW4TxKlYH8J0XAuKW+39VQhtedoWyo1Av9Ojus4knIHCERorzAK/cngIH5YUkqzAl97duVM/RvmFq7SPhZqKsYspFBsewhs7dxyQqjck9bQ78COSGCPG6iD1IEirhyjBmedczuvbCzXUo2qVw+eegL0YYdBOq01eUPLFRPMOSG9FVVtE53jwfj/mspF7626d7LgOPYQatk5FQSQjSsEDqjbc3BcAkOEJV3mkqXPsIr6AQRvkbP3OF5oqT1fxq6eX5+5e2wOCIC0wegVJ8y/qO3IPil2uZJPKOdn53F2YHHcBwyZSUiqZx321Ecwntme81eDPGwbdt2qmZI2J+z51gr1j6hD/tWOQOK4VHk/3Vsk2E7svMUzFZ/R8t/cbaSWm6JNeTpfRCrusEPZ1QNnZ7n89K4wvCjPRGS64sWJiwo1qF1TsG73VgWgtPHsL6BVofAumDS8wrq2lKC6pLY7rRt+5LwebjSJVLvAlrugl2l42NWZkn21h6+5opdakUfD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AoAGA1gGwFDv9iPkU2Om7rQkPSEVNUbpEmJhFBmlUpYzv3SOq4oITcMSlIpmTlXzJscN6A2XzHS6893LKZCS4wrisSnUNOTvfGRPGJV24ZihDaEWVkGAM=', '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': 'EssBCioICRgCIiQ4ODk4YTFkYy0yMTNkLTRhNmYtOTljYi03ZTBlNTUzZDc0NWISDA6zDzPxmyp5sG0I/BoMDDD7LxEWXz092xEEIjBGLvE1eJCdbnxN9AS41CXPvyuItyRMv1ruSlqC+C67/xXFpWiK5ioZb2QGqVDHTkEqT1p0bttFthiYyFQeP0EkQwYU/lLcGxU3zGGlGwWP35u2k/XnX+00qTqdvAwzi/I4WxAj8vEH/tJTXWaXgsV36dH/sDQVBmjm9STduGo+0h8YAw==', 'page_age': 'September 12, 2025', 'title': 'San Diego, CA Weather Forecast, Conditions, and Maps – Yahoo Weather', 'type': 'web_search_result', 'url': 'https://weather.yahoo.com/us/ca/san-diego'}, {'encrypted_content': 'EsgHCioICRgCIiQ4ODk4YTFkYy0yMTNkLTRhNmYtOTljYi03ZTBlNTUzZDc0NWISDM3qNDYymGVnINbvYRoMm2vPNF5Dg47nqvzzIjAI22Sr5jWPBIeIHRj2YrfiOqFLc6S1lGjVdDLQNCAgunYSqF5vT2c0Xh5tHm4Wv8kqywbz9fHCOP4WQ1WOlK1Mw4bJLEVENL7yexMknpb/pv8WLHwRRBNy4gA9b2ugbK8nNSTpzG7RI6NxC0mlzH5MqOixZR+Ajikt47WcIMdJE9McvcKt+LunDXFKds5QsgzRO4yvO5gB/R3e5oGjmpk9R05FOxUdkDC2xLMz+XZJdnMa2rfsG8z40FeKhXIXEg8a+S7MEIPOC/NAuNa9LATnNZD0bddIGZzb6AbrYfjvKa6AjVAE1fpoayHTx19phwiH+HsPFFpgoI3doZat9Q6uoNpR0MdRbmlnlk9JJEuyPSVXxXebygcdXdhWPc9lJBXLxf4w0kSfL/gPFhjBHWffUHiVCSa1y/VOOGCcRMJeYMJL+oqhZ5U8q/Uyp7WIHITuOfH8wOEuAk5bINKK+nqoYglY+dx78n6nhqcPSMyj/7KqkbC4OrmtZde90uVemqL5rwVWgeauolBIaYvJGCLwYNErpXKOdyWC4LZxsdYaVTfdRjACAkMUrIH2KJUdNZGYblRfs940Vqlq8SSRiVs97ekZrLebpCoiGzJTuwp+jIN3i4jaDyY6f/3hF7IOlgiQicbdwbGRo+dFB38h7MHS5mMZ7GXJ2ITdu1bMeWshZJCfIhkWBvTde9Op00Ftt7wnYUfnVPoBt7Ad7hpxXsWI8Awu81luYZdmBjsHQtEFf7SAKTMDQKnT5uJ6A41PHYbVsTyzeeNsaulXNpBDN5e3Rx/P1SSw7yONZmhuaDlYyHiig3eelsT/9DUtzXCLvshI2PUSn0wjnHg1yysopZzMpjQZLRdauYjqW9BR++Bd72aoYAFNOVJNHAikNuGLu386islkRGFiSxB/gVx5vtif39gD2W878YoT4g/h+4pgxDeRmPHy9P0IdIbloeauPms9em7fjrZHNr78NCgKr//rUF+wS/wdsDliCUrStdOaA6GVi2jQJM+4wa8iXP4vPiWKdj7CvE/oxXQc74IdmvyCtl4ZK6XuNKokpZmKQtSdm+8K4mx0ivr5+MQ1JO7gqoTb/wfL+ZOvoFvB4OhpKOMxJ7PfkzFJx1kEi+g7HVQddycKAlc2c7ugWfdXiRZQIEvExUR37ZbAC7Iyq+Rt3ownCViGimL8ovVpEyu1sr8YAw==', 'page_age': None, 'title': 'San Diego, CA Weather Conditions | Weather Underground', 'type': 'web_search_result', 'url': 'https://www.wunderground.com/weather/us/ca/san-diego'}, {'encrypted_content': 'EoIVCioICRgCIiQ4ODk4YTFkYy0yMTNkLTRhNmYtOTljYi03ZTBlNTUzZDc0NWISDCSCuhJgxFJA7C2IshoMipedm9mwFxLEFAduIjD98Rw1cAlTCSp2qKZjgylRjat5IgwBKNJCOyQvjBYAW23utu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VAML3OA+WaxhPXbHpUpuj4uwjASNwR2Ev+Li0WmPsldN5/9/6KEb4edZEs3Gqtu6d4bSbTkAKHmGRTFmYmd5eR9E5qNwY/vPELt9WJrYhdo/T1O/6CdBqmklbT+etGH7B0J6gfMPCnw2tSu3bvq2lazep+FY0SlFdUTgsnfl6hMSmdqUYAw==', 'page_age': None, 'title': 'San Diego, CA Hourly Weather | AccuWeather', 'type': 'web_search_result', 'url': 'https://www.accuweather.com/en/us/san-diego/92101/hourly-weather-forecast/347628'}], 'tool_use_id': 'srvtoolu_01NktSQtP5NmFjjAA8w7iLLq', 'type': 'web_search_tool_result'}, {'citations': None, 'text': 'The weather in San Diego today (Tuesday, November 4) is ', 'type': 'text'}, {'citations': [{'cited_text': 'San Diego, CA Weather Today WinterCast ... · Tue, Nov 4 · Clouds giving way to sun Hi: 69° · Tonight: Mostly clear this evening followed by low clouds...', 'encrypted_index': 'Eo8BCioICRgCIiQ4ODk4YTFkYy0yMTNkLTRhNmYtOTljYi03ZTBlNTUzZDc0NWISDCI8RZHER5H4wy1UQxoM99Iu58rn2qcOIJ/RIjBF4Mk1hivspsvTmN8Wfdjc5cn9VLuEAOHUrwVSTb8nlITqE8D8+ca4oVFdKLHDZyMqE+maM1hVg/jzuO9k1Yddb4hhobYYBA==', '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': 'cloudy giving way to sun with a high of 69°F', 'type': 'text'}, {'citations': None, 'text': ' and ', 'type': 'text'}, {'citations': [{'cited_text': 'San Diego, CA Weather Today WinterCast ... · Tue, Nov 4 · Clouds giving way to sun Hi: 69° · Tonight: Mostly clear this evening followed by low clouds...', 'encrypted_index': 'Eo8BCioICRgCIiQ4ODk4YTFkYy0yMTNkLTRhNmYtOTljYi03ZTBlNTUzZDc0NWISDGjWzpY3qwEIVY3izxoMzFzug5M+598r05G9IjBxnPARQnUfGTyRGu08qCyNc/fEhlCSUSr2V62BaKICPZsD4znO+YD7/7gsvE85P8UqE9+ZR/3ZK7Fjll1qnoa2n+DMEYAYBA==', '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': 'a low of 59°F tonight', 'type': 'text'}, {'citations': None, 'text': '. ', 'type': 'text'}, {'citations': [{'cited_text': 'zoom out · Showing Stations · Hourly Forecast for Today, Tuesday 11/04Hourly for Today, Tue 11/04 · Today 11/04 · 6% / 0 in · Areas of dense morning f...', 'encrypted_index': 'Eo8BCioICRgCIiQ4ODk4YTFkYy0yMTNkLTRhNmYtOTljYi03ZTBlNTUzZDc0NWISDCUenOMJHS91dxgcZxoMNTwCctyl0i8iVTiCIjBLBuhDaOXCnyjiXzZzk0Jxh5p0m95RC8xnoFqZyFzG9gggm1UzuNeIr7Uvlgz0OCYqE4Gnk4d+UnVMFB9nCurVCsccLL8YBA==', 'title': 'San Diego, CA Hourly Weather Forecast | Weather Underground', 'type': 'web_search_result_location', 'url': 'https://www.wunderground.com/hourly/us/ca/san-diego'}], 'text': 'There are areas of dense morning fog', 'type': 'text'}, {'citations': None, 'text': ', and conditions are expected to be ', 'type': 'text'}, {'citations': [{'cited_text': 'Partly cloudy. High 69F.', 'encrypted_index': 'EpEBCioICRgCIiQ4ODk4YTFkYy0yMTNkLTRhNmYtOTljYi03ZTBlNTUzZDc0NWISDDl7RZDzBvFObljPlxoMuuO58/cSxdUFQIDFIjCIK+q0nmRBx7jZCgw/fPPnNBsMJOGyJlP/YsoIHwDPGD+WevKHxkcUUDDQe/p+5V8qFR4+s+sAqNIvC0Io8QK85dnlkBEntBgE', 'title': 'San Diego, CA Hourly Weather Forecast | Weather Underground', 'type': 'web_search_result_location', 'url': 'https://www.wunderground.com/hourly/us/ca/san-diego'}], 'text': 'partly cloudy with a high of 69°F', 'type': 'text'}, {'citations': None, 'text': '. ', 'type': 'text'}, {'citations': [{'cited_text': 'Winds W at 5 to 10 mph. ', 'encrypted_index': 'EpABCioICRgCIiQ4ODk4YTFkYy0yMTNkLTRhNmYtOTljYi03ZTBlNTUzZDc0NWISDFnjnamfAYtQ4mn2FBoMaLpFtaTxV2joNHsZIjBf7LY2Bj3+0xdlqJ0kEZHSS1DUbofo0iI2sLQSxRExEMl551preQlEw4gJWvLj7WkqFOnWWkxYvgkUrIawSec7xz/qrDW1GAQ=', 'title': 'San Diego, CA Hourly Weather Forecast | Weather Underground', 'type': 'web_search_result_location', 'url': 'https://www.wunderground.com/hourly/us/ca/san-diego'}], 'text': 'Winds are from the west at 5 to 10 mph', 'type': 'text'}, {'citations': None, 'text': '.', 'type': 'text'}] - model:
claude-sonnet-4-5-20250929 - role:
assistant - stop_reason:
end_turn - stop_sequence:
None - type:
message - usage:
{'cache_creation': {'ephemeral_1h_input_tokens': 0, 'ephemeral_5m_input_tokens': 2226}, 'cache_creation_input_tokens': 7606, 'cache_read_input_tokens': 2226, 'input_tokens': 10, 'output_tokens': 226, '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-opus-4-1-20250805-v1:0',
'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:
ab = AnthropicBedrock(
aws_access_key=os.environ['AWS_ACCESS_KEY'],
aws_secret_key=os.environ['AWS_SECRET_KEY'],
)
client = Client(models_aws[0], ab)chat = Chat(cli=client)chat("I'm Jeremy")Google Vertex
models_goog['claude-opus-4-1@20250805',
'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.authproject_id = google.auth.default()[1]
region = "us-east5"
gv = AnthropicVertex(project_id=project_id, region=region)
client = Client(models_goog[-1], gv)chat = Chat(cli=client)chat("I'm Jeremy")Footnotes
https://www.accuweather.com/en/us/san-diego/92101/weather-forecast/347628 “San Diego, CA Weather Today WinterCast … · Tue, Nov 4 · Clouds giving way to sun Hi: 69° · Tonight: Mostly clear this evening followed by low clouds…”↩︎
https://www.accuweather.com/en/us/san-diego/92101/weather-forecast/347628 “San Diego, CA Weather Today WinterCast … · Tue, Nov 4 · Clouds giving way to sun Hi: 69° · Tonight: Mostly clear this evening followed by low clouds…”↩︎
https://www.wunderground.com/hourly/us/ca/san-diego “zoom out · Showing Stations · Hourly Forecast for Today, Tuesday 11/04Hourly for Today, Tue 11/04 · Today 11/04 · 6% / 0 in · Areas of dense morning f…”↩︎
https://www.wunderground.com/hourly/us/ca/san-diego “Partly cloudy. High 69F.”↩︎
https://www.wunderground.com/hourly/us/ca/san-diego “Winds W at 5 to 10 mph.”↩︎