Module 3 cheat sheet: Tools, Agents & Guardrails
The LLM asks for tools, your code runs them; create_agent automates the loop and middleware keeps it safe.
Key ideas
@tool: name + docstring + type hints = schema. The LLM only asks. Lab 12 โcreate_agent(model, tools, system_prompt, checkpointer, response_format). Lab 13 โ- Middleware runs around model and tool calls. Lab 14 โ
The manual tool loop
while True:
ai = llm_with_tools.invoke(messages); messages.append(ai)
if not ai.tool_calls: break
for c in ai.tool_calls:
r = tools_by_name[c["name"]].invoke(c["args"])
messages.append(ToolMessage(str(r), tool_call_id=c["id"]))Agent memory
agent = create_agent(llm, tools, checkpointer=InMemorySaver())
cfg = {"configurable": {"thread_id": "ajeet-1"}}
agent.invoke({"messages": [...]}, cfg)Middleware
| Middleware | Use |
|---|---|
| PIIMiddleware | redact / mask PII |
| HumanInTheLoopMiddleware | approve risky tools |
| ToolCallLimitMiddleware | stop loops |
| ModelFallbackMiddleware | backup model |
| SummarizationMiddleware | long chats |
| @before_model / @after_model | custom checks |
Common mistakes
- Vague tool docstrings.
- No limit on tool calls.
- Money tools without human approval.
- Asking customers for OTP / PIN in chat.
- Forgetting the checkpointer for HITL.
Created by Ajeetkumar ยท EasyLearnSpace