← Building with AI

Project part 2: the bot

Connect the tools with a tool loop, and keep private details out.

Start this lesson

Now connect everything. study_bot(question) should first redact personal details, then run the tool loop: ask the model, run any tool it asks for, send the result back, and stop when it gives text. Keep a step limit. This is the same pattern real AI apps use, just with a pretend model.

def study_bot(question, max_steps=3):
    messages = [{"role": "user", "content": redact(question)}]
    for step in range(max_steps):
        reply = ask_model(messages, TOOL_SPECS)
        if reply["type"] == "text":
            return reply["text"]
        # ...run the tool and add a "tool" message...
    return "Sorry, I got stuck."

Redaction happens before the first message is built, so personal details never reach the model.

Notice how many lessons meet here: messages, tools, dispatch, the loop, step limits and privacy.