Try an interactive version of this dialog: Sign up at solve.it.com, click Upload, and pass this URL.

Code: 28 ()

from fastcore.utils import *
from dialoghelper import *
from IPython.display import HTML,Markdown

Note: 9

Anthropic's agent demo

Note: 60

Let's replicate Anthropic's Customer Service Agent demo using Solveit.

Code: 267 ()

orders = {
    "O1": dict(id="O1", product="Widget A", quantity=2, price=19.99, status="Shipped"),
    "O2": dict(id="O2", product="Gadget B", quantity=1, price=49.99, status="Processing"),
    "O3": dict(id="O3", product="Gadget B", quantity=2, price=49.99, status="Shipped")}

customers = {
    "C1": dict(name="John Doe", email="john@example.com", phone="123-456-7890",
                orders=[orders['O1'], orders['O2']]),
    "C2": dict(name="Jane Smith", email="jane@example.com", phone="987-654-3210",
                orders=[orders['O3']])
}

Code: 277 ()

def get_customer_info(
    customer_id:str # ID of the customer
): # Customer's name, email, phone number, and list of orders
    "Retrieves a customer's information and their orders based on the customer ID"
    return customers.get(customer_id, "Customer not found")

def get_order_details(
    order_id:str # ID of the order
): # Order's ID, product name, quantity, price, and order status
    "Retrieves the details of a specific order based on the order ID"
    return orders.get(order_id, "Order not found")

def cancel_order(
    order_id:str # ID of the order to cancel
)->bool: # True if the cancellation is successful
    "Cancels an order based on the provided order ID"
    if order_id not in orders: return False
    orders[order_id]['status'] = 'Cancelled'
    return True

Note: 34

Tools available:

  • &get_customer_info
  • &get_order_details
  • &cancel_order

Prompt: 18

Can you tell me the email address for customer C2?

Assistant: 291

I'll look up the customer information for customer C2.

Copied!
{
  "id": "toolu_019xAvUcktcitSSgaDgkpeuJ",
  "call": {
    "function": "get_customer_info",
    "arguments": {
      "customer_id": "C2"
    }
  },
  "result": "{'name': 'Jane Smith',\n 'email': 'jane@example.com',\n 'phone': '987-654-3210',\n 'orders': [{'id': 'O3',\n   'product': 'Gadget B',\n   'quantity': 2,\n   'price': 49.99,\n   'status': 'Shipped'}]}"
}

The email address for customer C2 (Jane Smith) is jane@example.com.

Prompt: 15

What is the status of Order O2?

Assistant: 216

I'll look up the details for Order O2.

Copied!
{
  "id": "toolu_014BantQ7EYvgGB88o3RPD3s",
  "call": {
    "function": "get_order_details",
    "arguments": {
      "order_id": "O2"
    }
  },
  "result": "{'id': 'O2',\n 'product': 'Gadget B',\n 'quantity': 1,\n 'price': 49.99,\n 'status': 'Processing'}"
}

The status of Order O2 is Processing.

Prompt: 15

Please cancel all orders for customer C1.

Assistant: 684

I'll first look up customer C1's information to see all their orders, then cancel them.

Copied!
{
  "id": "toolu_01LmBiQFLPdovJHfXEKWLqTn",
  "call": {
    "function": "get_customer_info",
    "arguments": {
      "customer_id": "C1"
    }
  },
  "result": "{'name': 'John Doe',\n 'email': 'john@example.com',\n 'phone': '123-456-7890',\n 'orders': [{'id': 'O1',\n   'product': 'Widget A',\n   'quantity': 2,\n   'price': 19.99,\n   'status': 'Shipped'},\n  {'id': 'O2',\n   'product': 'Gadget B',\n   'quantity': 1,\n   'price': 49.99,\n   'status': 'Processing'}]}"
}

Customer C1 (John Doe) has two orders: O1 and O2. I'll cancel both of them now.

Copied!
{
  "id": "toolu_01QdEfaYaJ9YUcz3cbgXm4gk",
  "call": {
    "function": "cancel_order",
    "arguments": {
      "order_id": "O1"
    }
  },
  "result": "True"
}
Copied!
{
  "id": "toolu_01QTPhAnipvkAgvUUwcmN17K",
  "call": {
    "function": "cancel_order",
    "arguments": {
      "order_id": "O2"
    }
  },
  "result": "True"
}

I've successfully cancelled both orders for customer C1 (John Doe):

  • Order O1 (Widget A) - Cancelled
  • Order O2 (Gadget B) - Cancelled

Prompt: 28

Great, please double-check the status of those orders to confirm the cancellation went through successfully.

Assistant: 471

I'll check the status of both orders to confirm the cancellations.

Copied!
{
  "id": "toolu_01TKtLMRjbtJ5J8jzonWjv6h",
  "call": {
    "function": "get_order_details",
    "arguments": {
      "order_id": "O1"
    }
  },
  "result": "{'id': 'O1',\n 'product': 'Widget A',\n 'quantity': 2,\n 'price': 19.99,\n 'status': 'Cancelled'}"
}
Copied!
{
  "id": "toolu_01QGKoDzDTNrsdvZDonKUSiz",
  "call": {
    "function": "get_order_details",
    "arguments": {
      "order_id": "O2"
    }
  },
  "result": "{'id': 'O2',\n 'product': 'Gadget B',\n 'quantity': 1,\n 'price': 49.99,\n 'status': 'Cancelled'}"
}

Perfect! Both cancellations have been confirmed:

  • Order O1 (Widget A) - Status: Cancelled ✓
  • Order O2 (Gadget B) - Status: Cancelled ✓

The cancellations went through successfully for customer C1.

Note: 13

Using tools to explore and learn.

Code: 9 ()

from fastcore.tools import *

Note: 253

Tools available from fastcore.tools:

  • &rg: Run the rg command with the args in argstr (no need to backslash escape)
  • &sed: Run the sed command with the args in argstr (e.g for reading a section of a file)
  • &view: View directory or file contents with optional line range and numbers
  • &create: Creates a new file with the given content at the specified path
  • &insert: Insert new_str at specified line number
  • &str_replace: Replace first occurrence of old_str with new_str in file
  • &strs_replace: Replace for each str pair in old_strs,new_strs
  • &replace_lines: Replace lines in file using start and end line-numbers

Code: 21 ()

from claudette import *
from claudette.toolloop import *

Code: 6 ()

Chat.toolloop?

Output: 339

Signature:
Chat.toolloop(
    self: claudette.core.Chat,
    pr,
    max_steps=10,
    cont_func: <built-in function callable> = <function noop at 0x7fb5aef1e8e0>,
    final_prompt='You have no more tool uses. Please summarize your findings. If you did not complete your goal please tell the user what further work needs to be done so they can choose how best to proceed.',
    *,
    temp=None,
    maxtok=4096,
    maxthinktok=0,
    stream=False,
    prefill='',
    tool_choice: Optional[dict] = None,
)
Docstring: Add prompt `pr` to dialog and get a response from Claude, automatically following up with `tool_use` messages
File:      /usr/local/lib/python3.12/site-packages/claudette/toolloop.py
Type:      function

Code: 25 ()

%cd /usr/local/lib/python3.12/site-packages/claudette

Output: 42

/usr/local/lib/python3.12/site-packages/claudette

Prompt: 120

I want to learn how Claudette's tool loop works. Can you please read the documentation at https://claudette.answer.ai/toolloop.html to see how to use it, and then use your tools to investigate the source in the current directory and show me how it's all put together? As you use your tools, detail what you're doing and exactly what you find out along the way.

Prompt: 1

Prompt: 60

That's probably enough detail at this point. Could you give me a detailed explanation of everything you've learned about how this works, please, and suggest some approaches I could use to learn more about it?

Prompt: 64

Wow, that looks amazing, but it's also kind of intimidating, to be honest. Can you help me take me through this step by step, really slowly? What's the first simplest thing I could do here?

Assistant: 211

🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠

Absolutely! Let's start with the very basics. The simplest thing to understand is that Claudette's tool loop just automates what you'd otherwise do manually.

Let's start with the most basic example - a single tool that Claude can use:

Copied!
def get_weather(city: str):
    """Get the weather for a city"""
    return f"It's sunny in {city}!"

This is just a normal Python function. The docstring and type hint (: str) tell Claude what it does and what kind of input it expects.

Before we even use the tool loop, would you like to try creating your own simple tool function? Maybe something that does a basic calculation or returns some information?

Code: 39 ()

def get_weather(city: str):
    """Get the weather for a city"""
    return f"It's sunny in {city}!"

Prompt: 33

No, this one looks simple enough. Thank you. Can you show me what the next step is?

Assistant: 132

Great! Now let's use this tool with Claude. The next step is to create a Chat object and give it access to your tool:

Copied!
chat = Chat(model, tools=[get_weather])

That's it! Now Claude knows about your get_weather function and can call it.

Would you like to try creating that chat object, and then we can see what happens when you ask Claude a question about weather?

Code: 13 ()

models
m = models[2]
m

Output: 40

'claude-haiku-4-5'

Code: 16 ()

chat = Chat(m, tools=[get_weather])
chat

Output: 10

No results yet

Prompt: 1

Assistant: 93

🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠🧠

Perfect! You've created the chat object with your tool. Now let's ask Claude something that would need the weather tool:

Copied!
r = chat("What's the weather in Paris?")
r

Try running this and see what happens. Claude should recognize it needs to use your get_weather function.

Code: 16 ()

r = chat("What's the weather in Paris?")
r

Output: 477

[ToolUseBlock(id='toolu_019Dr9C7gy8fnhg6X8EbvM6v', input={'city': 'Paris'}, name='get_weather', type='tool_use')]

  • id: msg_019ncV4SxsYa3DXUn5c2P7z6
  • content: [{'id': 'toolu_019Dr9C7gy8fnhg6X8EbvM6v', 'input': {'city': 'Paris'}, 'name': 'get_weather', 'type': 'tool_use'}]
  • model: claude-haiku-4-5-20251001
  • 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': 568, 'output_tokens': 54, 'server_tool_use': None, 'service_tier': 'standard'}

Code: 3 ()

rg??

Output: 249

def rg(
    argstr:str, # All args to the command, will be split with shlex
    disallow_re:str=None, # optional regex which, if matched on argstr, will disallow the command
    allow_re:str=None # optional regex which, if not matched on argstr, will disallow the command
):
    "Run the `rg` command with the args in `argstr` (no need to backslash escape)"
    return run_cmd('rg', '-n '+argstr, disallow_re=disallow_re, allow_re=allow_re)

File: /usr/local/lib/python3.12/site-packages/fastcore/tools.py

Code: 195 ()

def rg(
    argstr:str, # All args to the command, will be split with shlex
    disallow_re:str=None, # optional regex which, if matched on argstr, will disallow the command
    allow_re:str=None # optional regex which, if not matched on argstr, will disallow the command
):
    "Run the `rg` command with the args in `argstr` (no need to backslash escape)"
    disallow_re = r'\.\.'
    return run_cmd('rg', '-n '+argstr, disallow_re=disallow_re, allow_re=allow_re)

Note: 9 ()

Creating our own tool

Code: 21 ()

from toolslm.shell import get_shell
shell = get_shell()

Code: 64 ()

def ex(
    code:str, # Code to execute
):
    "Execute code in a shell"
    res = shell.run_cell(code)
    return res.stdout if res.result is None else res.result
    

Code: 22 ()

ex('import os')
ex('"AOC_SESSION" in os.environ')

Output: 19

True

Prompt: 150

I want to test the &ex tool that I just created. Please use it to grab the Advent of Code 2024 Puzzle 1 information and example data, and then try to solve the first part of the problem. Tell me your solution when you're done.

Have a look at the AOCD docs at https://raw.githubusercontent.com/wimglenn/advent-of-code-data/refs/heads/main/README.md . Their problem is available in the puzzle object.

Prompt: 28

Great, please write out the source code of your solution and tell me how it works.

Note: 15

Using Claudette's tool loop.

Code: 9 ()

from claudette import *

Code: 55 ()

def adder(a:int,b:int):
    "Add two numbers"
    return a+b

def multer(a:int,b:int):
    "Multiply two numbers"
    return a*b

Code: 19 ()

adder(1,2),multer(1,2)

Output: 24

(3, 2)

Prompt: 48

I want to test my tools. Can you use &[adder,multer] to solve 42427928*548749+547892?

Assistant: 391

I'll use the adder and multer tools to solve that calculation for you.

Copied!
{
  "id": "toolu_01GJt98GFn46sZYmUPRmfDPC",
  "call": {
    "function": "multer",
    "arguments": {
      "a": "42427928",
      "b": "548749"
    }
  },
  "result": "23282283062072"
}
Copied!
{
  "id": "toolu_01FkBLHEFhfNnANLyJbwNvKW",
  "call": {
    "function": "adder",
    "arguments": {
      "a": "23282283062072",
      "b": "547892"
    }
  },
  "result": "23282283609964"
}

The answer is 23,282,283,609,964.

I first multiplied 42,427,928 × 548,749 = 23,282,283,062,072, then added 547,892 to get the final result.

Code: 13 ()

42427928*548749+547892

Output: 25

23282283609964

Code: 16 ()

c = Chat(m, tools=[adder,multer])

Code: 10 ()

c("Hi I am Jeremy")

Output: 588

Hi Jeremy! Nice to meet you. 👋

I'm here to help you with various tasks. I have access to some mathematical tools that can help with:

  • Adding two numbers (adder)
  • Multiplying two numbers (multer)

Is there anything I can help you with today?

  • id: msg_018bhC1an3UAoM3hdsaQCFUz
  • content: [{'citations': None, 'text': "Hi Jeremy! Nice to meet you. 👋\n\nI'm here to help you with various tasks. I have access to some mathematical tools that can help with:\n- **Adding two numbers** (adder)\n- **Multiplying two numbers** (multer)\n\nIs there anything I can help you with today?", 'type': 'text'}]
  • model: claude-haiku-4-5-20251001
  • 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': 650, 'output_tokens': 72, 'server_tool_use': None, 'service_tier': 'standard'}

Code: 13 ()

c("Do you remember my name? ")

Output: 396

Yes, I do! Your name is Jeremy. 😊

  • id: msg_01Y7TZU7dezov8kL4T6FzmqS
  • content: [{'citations': None, 'text': 'Yes, I do! Your name is Jeremy. 😊', 'type': 'text'}]
  • model: claude-haiku-4-5-20251001
  • 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': 732, 'output_tokens': 16, 'server_tool_use': None, 'service_tier': 'standard'}

Code: 52 ()

pr = 'I want to test my tools. Can you use &`[adder,multer]` to solve 42427928*548749+547892? '

Code: 12 ()

r = c.toolloop(pr)
r

Output: 49

<fastcore.xtras._save_iter at 0x7fb56e80b590>

Code: 12 ()

for o in r: print(o)

Output: 1,458

Message(id='msg_01ApygyBRBac76f67zDikdPp', content=[TextBlock(citations=None, text="I'll solve that for you! Let me break it down:\n1. First, multiply 42427928 × 548749\n2. Then, add 547892 to the result", type='text'), ToolUseBlock(id='toolu_01KJXzc1nVWMFxJdLf53j2Vu', input={'a': 42427928, 'b': 548749}, name='multer', type='tool_use')], model='claude-haiku-4-5-20251001', role='assistant', stop_reason='tool_use', stop_sequence=None, type='message', usage=In: 785; Out: 114; Cache create: 0; Cache read: 0; Total Tokens: 899; Search: 0)
{'role': 'user', 'content': [{'type': 'tool_result', 'tool_use_id': 'toolu_01KJXzc1nVWMFxJdLf53j2Vu', 'content': '23282283062072'}]}
Message(id='msg_01URFRCXsTgCBUzKK6W3MdRg', content=[TextBlock(citations=None, text='Now let me add 547892 to that result:', type='text'), ToolUseBlock(id='toolu_0119mEnBtyDnryM3JPSm7zAj', input={'a': 23282283062072, 'b': 547892}, name='adder', type='tool_use')], model='claude-haiku-4-5-20251001', role='assistant', stop_reason='tool_use', stop_sequence=None, type='message', usage=In: 916; Out: 87; Cache create: 0; Cache read: 0; Total Tokens: 1003; Search: 0)
{'role': 'user', 'content': [{'type': 'tool_result', 'tool_use_id': 'toolu_0119mEnBtyDnryM3JPSm7zAj', 'content': '23282283609964'}]}
Message(id='msg_01RJWyXsQeAUwhkHXrZ2wjxu', content=[TextBlock(citations=None, text='**Result: 23,282,283,609,964**\n\nSo 42427928 × 548749 + 547892 = **23,282,283,609,964**', type='text')], model='claude-haiku-4-5-20251001', role='assistant', stop_reason='end_turn', stop_sequence=None, type='message', usage=In: 1020; Out: 46; Cache create: 0; Cache read: 0; Total Tokens: 1066; Search: 0)

Code: 9 ()

from IPython.display import display

Code: 12 ()

r = c.toolloop(pr)
r

Output: 52

<fastcore.xtras._save_iter at 0x7fb56e9f1e50>

Code: 12 ()

for o in r: display(o)

Output: 1,759

I'll solve that for you! Let me break it down:

  1. First, multiply 42427928 × 548749
  2. Then, add 547892 to the result
  • id: msg_011Qmxt4rdfrmYm9CBE3MAHe
  • content: [{'citations': None, 'text': "I'll solve that for you! Let me break it down:\n1. First, multiply 42427928 × 548749\n2. Then, add 547892 to the result", 'type': 'text'}, {'id': 'toolu_01U8q8CGyADechSkVFoCRWzh', 'input': {'a': 42427928, 'b': 548749}, 'name': 'multer', 'type': 'tool_use'}]
  • model: claude-haiku-4-5-20251001
  • 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': 1103, 'output_tokens': 114, 'server_tool_use': None, 'service_tier': 'standard'}
{ 'content': [ { 'content': '23282283062072',
                 'tool_use_id': 'toolu_01U8q8CGyADechSkVFoCRWzh',
                 'type': 'tool_result'}],
  'role': 'user'}

Now let me add 547892 to that result:

  • id: msg_01893vUyEYzrjPNbMrJt7bjL
  • content: [{'citations': None, 'text': 'Now let me add 547892 to that result:', 'type': 'text'}, {'id': 'toolu_011mvgjH6eypCWT82RtJwYGP', 'input': {'a': 23282283062072, 'b': 547892}, 'name': 'adder', 'type': 'tool_use'}]
  • model: claude-haiku-4-5-20251001
  • 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': 1234, 'output_tokens': 87, 'server_tool_use': None, 'service_tier': 'standard'}
{ 'content': [ { 'content': '23282283609964',
                 'tool_use_id': 'toolu_011mvgjH6eypCWT82RtJwYGP',
                 'type': 'tool_result'}],
  'role': 'user'}

Result: 23,282,283,609,964

So 42427928 × 548749 + 547892 = 23,282,283,609,964

  • id: msg_01LPm3bKDcQAMvjte52jwnDs
  • content: [{'citations': None, 'text': '**Result: 23,282,283,609,964**\n\nSo 42427928 × 548749 + 547892 = **23,282,283,609,964**', 'type': 'text'}]
  • model: claude-haiku-4-5-20251001
  • 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': 1338, 'output_tokens': 46, 'server_tool_use': None, 'service_tier': 'standard'}

Code: 15 ()

from toolslm.funccall import get_schema

Code: 6 ()

get_schema(ex)

Output: 261

{'name': 'ex',
 'description': 'Execute code in a shell',
 'input_schema': {'type': 'object',
  'properties': {'code': {'type': 'string', 'description': 'Code to execute'}},
  'required': ['code']}}

Note: 12

Making our own tool loop with Chat

Code: 16 ()

c = Chat(m, tools=[adder,multer])

Code: 1 ()

pr

Output: 78

'I want to test my tools. Can you use &`[adder,multer]` to solve 42427928*548749+547892? '

Code: 9 ()

r = c(pr)
r

Output: 550

I'll solve this step by step using the available tools.

First, let me multiply 42427928 * 548749, then add 547892 to the result.

  • id: msg_01Q4pWWvsupwmAt75AupeSDC
  • content: [{'citations': None, 'text': "I'll solve this step by step using the available tools.\n\nFirst, let me multiply 42427928 * 548749, then add 547892 to the result.", 'type': 'text'}, {'id': 'toolu_01QmUiE4xZ279CtcVjhgXuKv', 'input': {'a': 42427928, 'b': 548749}, 'name': 'multer', 'type': 'tool_use'}]
  • model: claude-haiku-4-5-20251001
  • 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': 680, 'output_tokens': 111, 'server_tool_use': None, 'service_tier': 'standard'}

Code: 4 ()

r.stop_reason

Output: 31

'tool_use'

Code: 3 ()

c()

Output: 472

Now I'll add 547892 to that result:

  • id: msg_013f3HxRwMZaQMsFC4C3HeMj
  • content: [{'citations': None, 'text': "Now I'll add 547892 to that result:", 'type': 'text'}, {'id': 'toolu_0139sVdfzDS8pFCyPmwXLSTn', 'input': {'a': 23282283062072, 'b': 547892}, 'name': 'adder', 'type': 'tool_use'}]
  • model: claude-haiku-4-5-20251001
  • 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': 808, 'output_tokens': 87, 'server_tool_use': None, 'service_tier': 'standard'}

Code: 7 ()

r = c()
r

Output: 448

Result: 42427928 × 548749 + 547892 = 23,282,283,609,964

  • id: msg_01KB5vPm8AXbQiQMH7GtVLhD
  • content: [{'citations': None, 'text': '**Result:** 42427928 × 548749 + 547892 = **23,282,283,609,964**', 'type': 'text'}]
  • model: claude-haiku-4-5-20251001
  • 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': 912, 'output_tokens': 33, 'server_tool_use': None, 'service_tier': 'standard'}

Code: 4 ()

r.stop_reason

Output: 31

'end_turn'

Code: 3 ()

c.h

Output: 1,363

[{'role': 'user',
  'content': 'I want to test my tools. Can you use &`[adder,multer]` to solve 42427928*548749+547892? '},
 {'role': 'assistant',
  'content': [{'citations': None,
    'text': "I'll solve this step by step using the available tools.\n\nFirst, let me multiply 42427928 * 548749, then add 547892 to the result.",
    'type': 'text'},
   {'id': 'toolu_01QmUiE4xZ279CtcVjhgXuKv',
    'input': {'a': 42427928, 'b': 548749},
    'name': 'multer',
    'type': 'tool_use'}]},
 {'role': 'user',
  'content': [{'type': 'tool_result',
    'tool_use_id': 'toolu_01QmUiE4xZ279CtcVjhgXuKv',
    'content': '23282283062072'}]},
 {'role': 'assistant',
  'content': [{'citations': None,
    'text': "Now I'll add 547892 to that result:",
    'type': 'text'},
   {'id': 'toolu_0139sVdfzDS8pFCyPmwXLSTn',
    'input': {'a': 23282283062072, 'b': 547892},
    'name': 'adder',
    'type': 'tool_use'}]},
 {'role': 'user',
  'content': [{'type': 'tool_result',
    'tool_use_id': 'toolu_0139sVdfzDS8pFCyPmwXLSTn',
    'content': '23282283609964'}]},
 {'role': 'assistant',
  'content': [{'citations': None,
    'text': '**Result:** 42427928 × 548749 + 547892 = **23,282,283,609,964**',
    'type': 'text'}]}]

Code: 19 ()

c = Chat(m, tools=[adder,multer])
c.h

Output: 19

[]

Code: 9 ()

r = c(pr)
r

Output: 556

I'll solve this step by step using the available tools.

First, let me multiply 42427928 * 548749, then add 547892 to the result.

  • id: msg_01URkeFztSGHkjc9oMTgSXv3
  • content: [{'citations': None, 'text': "I'll solve this step by step using the available tools.\n\nFirst, let me multiply 42427928 * 548749, then add 547892 to the result.", 'type': 'text'}, {'id': 'toolu_012suiQ5kJYN8vfrVw4QH7YL', 'input': {'a': 42427928, 'b': 548749}, 'name': 'multer', 'type': 'tool_use'}]
  • model: claude-haiku-4-5-20251001
  • 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': 680, 'output_tokens': 111, 'server_tool_use': None, 'service_tier': 'standard'}

Code: 25 ()

while r.stop_reason!='end_turn':
    r = c()
    display(r)

Output: 925

Now I'll add 547892 to that result:

  • id: msg_01QjMqEpCCzYgddueeCevZaz
  • content: [{'citations': None, 'text': "Now I'll add 547892 to that result:", 'type': 'text'}, {'id': 'toolu_01NQVEYB53QdTKQBRyQGqTjo', 'input': {'a': 23282283062072, 'b': 547892}, 'name': 'adder', 'type': 'tool_use'}]
  • model: claude-haiku-4-5-20251001
  • 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': 808, 'output_tokens': 87, 'server_tool_use': None, 'service_tier': 'standard'}

Result: 42427928 × 548749 + 547892 = 23,282,283,609,964

  • id: msg_01PNvReM19413nR4WuhJt5zT
  • content: [{'citations': None, 'text': '**Result:** 42427928 × 548749 + 547892 = **23,282,283,609,964**', 'type': 'text'}]
  • model: claude-haiku-4-5-20251001
  • 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': 912, 'output_tokens': 33, 'server_tool_use': None, 'service_tier': 'standard'}

Note: 18

I am doing it manually with no Chat.

Code: 1 ()

m

Output: 40

'claude-haiku-4-5'

Code: 7 ()

c = Chat(m)

Code: 12 ()

c('Hi, I am Jeremy.')

Output: 403

Hi Jeremy! Nice to meet you. How can I help you today?

  • id: msg_011oTWUyiw6sRJ8chZ9LZKBG
  • content: [{'citations': None, 'text': 'Hi Jeremy! Nice to meet you. How can I help you today?', 'type': 'text'}]
  • model: claude-haiku-4-5-20251001
  • 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': 18, 'server_tool_use': None, 'service_tier': 'standard'}

Code: 3 ()

c.h

Output: 235

[{'role': 'user', 'content': 'Hi, I am Jeremy.'},
 {'role': 'assistant',
  'content': [{'citations': None,
    'text': 'Hi Jeremy! Nice to meet you. How can I help you today?',
    'type': 'text'}]}]

Code: 16 ()

c('Do you know what my name is? ')

Output: 418

Yes, you just told me—your name is Jeremy. Is there something I can help you with?

  • id: msg_01MTdokfa32ypmRsJ9P1Yhs8
  • content: [{'citations': None, 'text': 'Yes, you just told me—your name is Jeremy. Is there something I can help you with?', 'type': 'text'}]
  • model: claude-haiku-4-5-20251001
  • 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': 43, 'output_tokens': 24, 'server_tool_use': None, 'service_tier': 'standard'}

Code: 27 ()

c.h[0]['content'] = 'Hi, I am Johno.'
c.h

Output: 468

[{'role': 'user', 'content': 'Hi, I am Johno.'},
 {'role': 'assistant',
  'content': [{'citations': None,
    'text': 'Hi Jeremy! Nice to meet you. How can I help you today?',
    'type': 'text'}]},
 {'role': 'user', 'content': 'Do you know what my name is? '},
 {'role': 'assistant',
  'content': [{'citations': None,
    'text': 'Yes, you just told me—your name is Jeremy. Is there something I can help you with?',
    'type': 'text'}]}]

Code: 31 ()

c('Wait, are you sure that does not sound like my name, if I remember correctly? ')

Output: 484

You're absolutely right, and I apologize for the error! You introduced yourself as Johno, not Jeremy. I made a mistake. Sorry about that! What can I help you with, Johno?

  • id: msg_013Zm17o4Te4iXu43QyWydRT
  • content: [{'citations': None, 'text': "You're absolutely right, and I apologize for the error! You introduced yourself as **Johno**, not Jeremy. I made a mistake. Sorry about that! What can I help you with, Johno?", 'type': 'text'}]
  • model: claude-haiku-4-5-20251001
  • 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': 91, 'output_tokens': 48, 'server_tool_use': None, 'service_tier': 'standard'}

Code: 7 ()

c = Client(m)

Code: 15 ()

c('Hi there, I am Jeremy. ')

Output: 403

Hi Jeremy! Nice to meet you. How can I help you today?

  • id: msg_01QXUTNp9fjQS7oXQXEGNG6S
  • content: [{'citations': None, 'text': 'Hi Jeremy! Nice to meet you. How can I help you today?', 'type': 'text'}]
  • model: claude-haiku-4-5-20251001
  • 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': 15, 'output_tokens': 18, 'server_tool_use': None, 'service_tier': 'standard'}

Code: 16 ()

c('Do you know what my name is? ')

Output: 487

I don't know your name. We haven't met before, and I don't have access to any information about you unless you share it with me in our conversation.

Feel free to tell me your name if you'd like!

  • id: msg_01WwGEtUvGo2DodUzGEBoRNY
  • content: [{'citations': None, 'text': "I don't know your name. We haven't met before, and I don't have access to any information about you unless you share it with me in our conversation.\n\nFeel free to tell me your name if you'd like!", 'type': 'text'}]
  • model: claude-haiku-4-5-20251001
  • 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': 16, 'output_tokens': 50, 'server_tool_use': None, 'service_tier': 'standard'}

Code: 10 ()

tools = [adder,multer]

Code: 15 ()

r = c(pr, tools=tools)
r

Output: 553

I'll solve this step by step using the available tools.

First, let me multiply 42427928 * 548749, then add 547892 to the result.

  • id: msg_01AWp8Tg3MC6ZAgkwC8anJa4
  • content: [{'citations': None, 'text': "I'll solve this step by step using the available tools.\n\nFirst, let me multiply 42427928 * 548749, then add 547892 to the result.", 'type': 'text'}, {'id': 'toolu_01JHJPX3NRbaNYzooWHSuwuj', 'input': {'a': 42427928, 'b': 548749}, 'name': 'multer', 'type': 'tool_use'}]
  • model: claude-haiku-4-5-20251001
  • 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': 680, 'output_tokens': 111, 'server_tool_use': None, 'service_tier': 'standard'}

Note: 343

Sample history:

Copied!
[{'role': 'user',
  'content': 'I want to test my tools. Can you use &`[adder,multer]` to solve 42427928*548749+547892? '},
 {'role': 'assistant',
  'content': [{'citations': None,
    'text': "I'll solve this step by step using the available tools.\n\nFirst, let me multiply 42427928 * 548749, then add 547892 to the result.",
    'type': 'text'},
   {'id': 'toolu_01VFcpBbBzDNVeotmtNsHi3R',
    'input': {'a': 42427928, 'b': 548749},
    'name': 'multer',
    'type': 'tool_use'}]},
 {'role': 'user',
  'content': [{'type': 'tool_result',
    'tool_use_id': 'toolu_01VFcpBbBzDNVeotmtNsHi3R',
    'content': '23282283062072'}]}
]

Code: 15 ()

tub = r.content[1]
tub

Output: 144

ToolUseBlock(id='toolu_01JHJPX3NRbaNYzooWHSuwuj', input={'a': 42427928, 'b': 548749}, name='multer', type='tool_use')

Code: 13 ()

func = globals()[tub.name]
func

Output: 43

<function __main__.multer(a: int, b: int)>

Code: 13 ()

res = func(**tub.input)
res

Output: 25

23282283062072

Code: 39 ()

trc = {'type': 'tool_result', 'tool_use_id': tub.id, 'content': dumps(res)}
trc

Output: 142

{'type': 'tool_result',
 'tool_use_id': 'toolu_01JHJPX3NRbaNYzooWHSuwuj',
 'content': '23282283062072'}

Code: 22 ()

msgs = mk_msgs([pr, r.content, [trc]])
msgs

Output: 583

[{'role': 'user',
  'content': 'I want to test my tools. Can you use &`[adder,multer]` to solve 42427928*548749+547892? '},
 {'role': 'assistant',
  'content': [TextBlock(citations=None, text="I'll solve this step by step using the available tools.\n\nFirst, let me multiply 42427928 * 548749, then add 547892 to the result.", type='text'),
   ToolUseBlock(id='toolu_01JHJPX3NRbaNYzooWHSuwuj', input={'a': 42427928, 'b': 548749}, name='multer', type='tool_use')]},
 {'role': 'user',
  'content': [{'type': 'tool_result',
    'tool_use_id': 'toolu_01JHJPX3NRbaNYzooWHSuwuj',
    'content': '23282283062072'}]}]

Note: 6

Completing the homework

Note: 37

I accidentally deleted the prompt + answer, but realised we MUST pass tools to the Client c() every time, unlike Chat!

Note: 142

Jeremy Howard: "The goal isn’t to replicate what is in Claudette, but for you to write something that’s truely your own!

The trick is to go in extremely small steps. Each step should add just a little to the previous step. By the end of the video, we were nearly done - try asking solveit what’s the simplest minimal set of steps to go from there to having a basic tool loop. Let it help you with that"

Code: 18 ()

r = c(msgs,tools=tools) 
r

Output: 475

Now I'll add 547892 to that result:

  • id: msg_01JWx9yxcPniwtoQtu8DymzM
  • content: [{'citations': None, 'text': "Now I'll add 547892 to that result:", 'type': 'text'}, {'id': 'toolu_01GkqiKsGbB2yGfiJrkNWPdo', 'input': {'a': 23282283062072, 'b': 547892}, 'name': 'adder', 'type': 'tool_use'}]
  • model: claude-haiku-4-5-20251001
  • 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': 808, 'output_tokens': 87, 'server_tool_use': None, 'service_tier': 'standard'}

Code: 3 ()

r.content

Output: 207

[TextBlock(citations=None, text="Now I'll add 547892 to that result:", type='text'),
 ToolUseBlock(id='toolu_01GkqiKsGbB2yGfiJrkNWPdo', input={'a': 23282283062072, 'b': 547892}, name='adder', type='tool_use')]

Code: 1 ()

msgs

Output: 583

[{'role': 'user',
  'content': 'I want to test my tools. Can you use &`[adder,multer]` to solve 42427928*548749+547892? '},
 {'role': 'assistant',
  'content': [TextBlock(citations=None, text="I'll solve this step by step using the available tools.\n\nFirst, let me multiply 42427928 * 548749, then add 547892 to the result.", type='text'),
   ToolUseBlock(id='toolu_01JHJPX3NRbaNYzooWHSuwuj', input={'a': 42427928, 'b': 548749}, name='multer', type='tool_use')]},
 {'role': 'user',
  'content': [{'type': 'tool_result',
    'tool_use_id': 'toolu_01JHJPX3NRbaNYzooWHSuwuj',
    'content': '23282283062072'}]}]

Prompt: 184

I think I want to now append the value of r.content (the latest llm response) to msgs to continue the conversation till I get a r.stop_reason indicating I have my final answer.

msgs is just a list of dictionaries, so I think it would look something like one of these options:

msgs.append(...)

or

msgs += mk_msgs(...)

or msgs = msgs + [...]

I'm building up to writing a do_loop() function that does all this in a loop, but want to get it working manually cell by cell in this REPL first to learn... Which of the above is the right approach?

Assistant: 193

Great thinking! You're on the right track. Let's figure out which approach works.

The key is understanding what format msgs needs. Look at what you have:

  • msgs is a list of message dictionaries
  • r.content is the assistant's response content

You need to add an assistant message to msgs. Try this:

Copied!
msgs.append({'role': 'assistant', 'content': r.content})

This matches the pattern you see in your existing msgs - each message is a dict with 'role' and 'content' keys.

Want to try that and see if it works?

Code: 21 ()

msgs.append({'role': 'assistant', 'content': r.content})

Code: 1 ()

msgs

Output: 825

[{'role': 'user',
  'content': 'I want to test my tools. Can you use &`[adder,multer]` to solve 42427928*548749+547892? '},
 {'role': 'assistant',
  'content': [TextBlock(citations=None, text="I'll solve this step by step using the available tools.\n\nFirst, let me multiply 42427928 * 548749, then add 547892 to the result.", type='text'),
   ToolUseBlock(id='toolu_01JHJPX3NRbaNYzooWHSuwuj', input={'a': 42427928, 'b': 548749}, name='multer', type='tool_use')]},
 {'role': 'user',
  'content': [{'type': 'tool_result',
    'tool_use_id': 'toolu_01JHJPX3NRbaNYzooWHSuwuj',
    'content': '23282283062072'}]},
 {'role': 'assistant',
  'content': [TextBlock(citations=None, text="Now I'll add 547892 to that result:", type='text'),
   ToolUseBlock(id='toolu_01GkqiKsGbB2yGfiJrkNWPdo', input={'a': 23282283062072, 'b': 547892}, name='adder', type='tool_use')]}]

Note: 51

OK so basically avoid complicating things... don't use the mk_msgs function. Not sure how well this will translate to a loop but let's see as I attempt the homework.

Note: 84

homework task

  • Create a function called do_loop()
  • (That doesn't call Claudette's tool loop)
  • Should only call c, our client
  • Pass it a prompt
  • and list of tools

Step 1. Start by importing Jeremy's dialogue

Code: 202 ()

def do_loop(prompt, tools):

    r = c(prompt, tools=tools)

    while r.stop_reason!='end_turn':

        tool_use_block = r.content[1] # the tool use bit of the agents response
        print(f"tool use block is: {tool_use_block}")

        func = globals()[tool_use_block.name]
        result = func(**tool_use_block.input)

        tool_result_content = {'type': 'tool_result', 'tool_use_id': tool_use_block.id, 'content': dumps(result)}

        msgs = mk_msgs([prompt, result.content, [tool_result_content]])

        r = c(msgs,tools=tools)

Code: 1 ()

pr

Output: 78

'I want to test my tools. Can you use &`[adder,multer]` to solve 42427928*548749+547892? '

Prompt: 81

OK so I started my atempt, but I'm not sure how to proceed.

Earlier we avoided using mk_msgs() for the subsequent messages, we just use append() which seemed simpler.

I feel like I'm very close but I'm missing something and my understanding isn't quite there.

Assistant: 232

You're really close! The issue is that you're rebuilding msgs from scratch each time in the loop, but you actually want to keep adding to it.

Think about what you did manually:

  1. Started with msgs (which had the prompt and first response)
  2. Appended the new assistant response
  3. Then would add the next tool result, etc.

Try this approach instead:

Copied!
def do_loop(prompt, tools):
    msgs = [{'role': 'user', 'content': prompt}]
    
    while True:
        r = c(msgs, tools=tools)
        # Now what do you need to append to msgs?

What two things do you think need to be appended to msgs in each iteration of the loop?

Prompt: 33

OK I get that - but first - should I just avoid using mk_msgs() and hardcode for now?

Assistant: 127

Yes, absolutely! Avoid mk_msgs() for now. Just use plain dictionaries and .append() like you did successfully before.

Keep it simple - you already know that works. Once you get the loop working, you can always refactor later if needed.

So back to my question: in each loop iteration, after you get r back from Claude, what two things need to be appended to msgs?

Prompt: 18

the assistant response content, and the next tool call result?

Assistant: 183

Exactly right!

  1. The assistant's response (with the tool use request)
  2. The tool result (after you execute the tool)

So your loop structure would be:

Copied!
r = c(msgs, tools=tools)
msgs.append({'role': 'assistant', 'content': r.content})
# ... execute the tool ...
msgs.append({'role': 'user', 'content': [tool_result_content]})

Notice the tool result goes in a 'user' message (that's how the API expects it).

Want to try filling in the middle part where you execute the tool?

Code: 246 ()

def do_loop(prompt, tools):
    msgs = [{'role': 'user', 'content': prompt}]

    while True:
        r = c(msgs, tools=tools)
        msgs.append({'role': 'assistant', 'content': r.content})

        tool_use_block = r.content[1] # the tool use bit of the agents response
        print(f"tool use block is: {tool_use_block}")
        func = globals()[tool_use_block.name]
        result = func(**tool_use_block.input)
        tool_result_content = {'type': 'tool_result', 'tool_use_id': tool_use_block.id, 'content': dumps(result)}

        msgs.appent({'role': 'user', 'content': [tool_result_content]})

        r = c(msgs,tools=tools)

Prompt: 81

OK I filled the middle part. I assume you did "while True" instead of "while r.stop_reason != "..." as a placeholder? Also I would think before doing tool_use_block = ... I should check whether there is one? Or use a try loop?

Assistant: 201

Good catches! Yes, two issues:

  1. The while condition: You need to check r.stop_reason and break out when it's 'end_turn'. You could either:

    • while r.stop_reason == 'tool_use':
    • Or keep while True: and add if r.stop_reason == 'end_turn': break
  2. Checking for tool use: Yes! Before accessing r.content[1], you should check if there actually is a tool use. What happens if Claude doesn't request a tool, or if there are multiple tool uses?

Which of these two issues do you want to tackle first?

Prompt: 28

If I used while r.stop_reason == 'tool_use' - wouldn't that solve both problems?

Assistant: 211

Almost! Using while r.stop_reason == 'tool_use' solves the first problem (knowing when to stop), but there's a subtle issue:

The first time through, r doesn't exist yet when the while condition is checked. You'd get a NameError.

You could handle this by doing the first call before the loop:

Copied!
r = c(msgs, tools=tools)
while r.stop_reason == 'tool_use':
    # ... rest of your code

But you still need to handle: what if r.content[1] doesn't exist or isn't a ToolUseBlock? Should you assume it's always at index 1?

Code: 264 ()

def do_loop(prompt, tools):

    msgs = [{'role': 'user', 'content': prompt}]

    r = c(msgs, tools=tools)
    display(r)

    while True:

        msgs.append({'role': 'assistant', 'content': r.content})

        tool_use_block = r.content[1] # the tool use bit of the agents response
        func = globals()[tool_use_block.name]
        result = func(**tool_use_block.input)
        tool_result_content = {'type': 'tool_result', 'tool_use_id': tool_use_block.id, 'content': dumps(result)}
        # display(tool_result_content)

        msgs.append({'role': 'user', 'content': [tool_result_content]})

        r = c(msgs,tools=tools)
        display(r)

        if r.stop_reason == 'end_turn':break

Code: 28 ()

do_loop("what is 17998 * 081028 + 882792", tools)

Output: 1,564

I'll calculate this step by step. First, I'll multiply 17998 * 081028, then add 882792 to the result.

  • id: msg_012EAag9abqkis6FKn4DqcVW
  • content: [{'citations': None, 'text': "I'll calculate this step by step. First, I'll multiply 17998 * 081028, then add 882792 to the result.", 'type': 'text'}, {'id': 'toolu_01NCmQBQdoksdJ1RgqdBnxy4', 'input': {'a': 17998, 'b': 81028}, 'name': 'multer', 'type': 'tool_use'}]
  • model: claude-haiku-4-5-20251001
  • 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': 661, 'output_tokens': 104, 'server_tool_use': None, 'service_tier': 'standard'}

Now I'll add 882792 to that result:

  • id: msg_011R8HskGzJ8NgGgnf34vECG
  • content: [{'citations': None, 'text': "Now I'll add 882792 to that result:", 'type': 'text'}, {'id': 'toolu_01TR52jejouiCUP7RQfTcuLX', 'input': {'a': 1458341944, 'b': 882792}, 'name': 'adder', 'type': 'tool_use'}]
  • model: claude-haiku-4-5-20251001
  • 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': 781, 'output_tokens': 86, 'server_tool_use': None, 'service_tier': 'standard'}

The answer is 1,459,224,736.

To break it down:

  • 17998 × 81028 = 1,458,341,944
  • 1,458,341,944 + 882,792 = 1,459,224,736
  • id: msg_01TrzUAbLfjfoq3d36cWoQ2d
  • content: [{'citations': None, 'text': 'The answer is **1,459,224,736**.\n\nTo break it down:\n- 17998 × 81028 = 1,458,341,944\n- 1,458,341,944 + 882,792 = **1,459,224,736**', 'type': 'text'}]
  • model: claude-haiku-4-5-20251001
  • 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': 883, 'output_tokens': 68, 'server_tool_use': None, 'service_tier': 'standard'}

Prompt: 18

How does that look? Anything to fix up or change?

Assistant: 186

That looks great! It's working correctly. A few things you could consider improving:

  1. Multiple tool uses: Right now you assume the tool use is at r.content[1]. What if Claude requests multiple tools at once, or the tool use is at a different index?

  2. Error handling: What if the tool call fails? Or if there's no tool use block?

  3. Return value: Should do_loop() return something (like the final answer or the full message history)?

Do any of these interest you, or are you happy with it as-is for now?

Note: 433

Other great classmate examples:

https://share.solve.it.com/d/23b2e71dcdb9522a61a3461b18a3fdab by @eetu6029 - super simple and sweet

https://share.solve.it.com/d/63636c6831b77ae245a754a516577b42 by @apollolukash. - builds a whole chat class from scratch!

https://share.solve.it.com/d/69d249b2088f3d509428a71e7b57c554 by @marcus_noelke - another person creating their own chat class - worth a go maybe?

https://share.solve.it.com/d/46788b0e98d47f46b547522275111953#i-am-doing-it-manually-with-no-chat by @pyrite_goblin - someone doing it with a very similar approach to mine. also the way they ask for help is great!

https://share.solve.it.com/d/89c1e154d1cee247c4369548d03875c2 by @dhidru - asks lots of great questions, comes up with an excellent solution

https://share.solve.it.com/d/6e9289d4d22502520eca158dad27634b#i-am-doing-it-manually-with-no-chat by @jeremyhoward of course...