Start learning free

LESSON 116 OF 129 · AI ENGINEERING: CAPSTONE

The Assistant End to End, 2: Tools

Practice

The assistant answers from the shop's documents. But orders are not in docs/: they live in the shop's database. To answer a question about an order, the assistant needs tools: functions of yours that the model may ask for.

The model never runs a tool. It stops and says which tool it wants, and with what input. Your code runs the function, sends the result back and asks the model again. That repeat is the tool loop. For example, with a clock tool:

You:    "What time is it in Tokyo?"  (the tools are on offer)
Model:  stops with stop_reason "tool_use": get_time {"city": "Tokyo"}
You:    run get_time, send back {"time": "09:40"}
Model:  "It is 9:40 in Tokyo."

The starting code already holds the two order tools, lookup_order and find_orders, with their descriptions. Nobody offers them to the model yet, and nobody runs them. When you are done, the program prints:

Customer: Can I get my money back?
Assistant: {'answer': "Yes. You can return any item within 14 days of delivery, …", 'sources': ['returns.md'], 'tools': []}
Customer: Can you look up order 4 for me?
Assistant: {'answer': 'Order 4 was placed on 2026-04-18 and it has shipped.', 'sources': [], 'tools': ['lookup_order']}
Customer: My order 4 arrived damaged. Can I get a refund?
Assistant: {'answer': "Order 4 has shipped. Since it arrived damaged, …", 'sources': ['returns.md'], 'tools': ['lookup_order']}
Customer: What's the weather tomorrow?
Assistant: {'answer': "I don't know, let me find someone who does.", 'sources': [], 'tools': []}
Model calls: 6

Nobody writes a rule that says which question goes where. Retrieval puts the passages in front of the model, the tools are on offer, and the model picks what the question needs.

Your task: offer the tools to the model in ask_model, write run_tools (it runs every tool the model asked for, notes its name and adds the request and the results to the conversation), and add the tool loop to answer.


Assignment
Follow the Coding Tutorial to offer the order tools, write run_tools and add the tool loop to answer.


Hint
Every tool result must come right after the request it answers: first the assistant message with the model's reply, then the user message with your results. The loop goes on while reply.stop_reason is "tool_use".


STEP 1 OF 11
    Ctrl + Enter to check