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.

All modules๐Ÿ“– Glossary

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

MiddlewareUse
PIIMiddlewareredact / mask PII
HumanInTheLoopMiddlewareapprove risky tools
ToolCallLimitMiddlewarestop loops
ModelFallbackMiddlewarebackup model
SummarizationMiddlewarelong chats
@before_model / @after_modelcustom 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