Objectives
- Create custom tools using the
@tooldecorator. - Maintain global state to update content across loop executions.
- Build a loop of execution (agent -> tools -> agent) that terminates only when a specific tool is called.
Agent V: Custom Tool Agent
Goal
Build a document drafting agent (Drafter) that loops using custom tools (update and save) to modify and save content to a local file.
Sample Input
Sequential instructions:"Write a basic email draft saying I am sick.""Save the document to email.txt."
Sample Output
"Document updated successfully. Current content is: Hi, I am unable to attend today due to sickness.""Document has been saved successfully to 'email.txt'."
Plan
- Define the
AgentStateschema usingadd_messagesto append messages to history. - Create custom tools
@toolforupdate(updates content in global variable) andsave(saves content to a file). - Write the
our_agentnode function andshould_continueconditional router function. - Compile and run the agent.
Code Implementation
1. Define the State Schema
We define a state schema usingadd_messages annotation:
2. Create Custom Tools
We define two custom tools that operate on a global document content variable:3. Define Nodes and Routing Logic
We write the agent node (using a system prompt with current document content) and the router function that terminates execution when the save tool returns successfully:4. Build and Compile the Graph
We hook up the loops:Exercise: Todo List Manager Agent 🗃
Goal
Build a Todo List Manager Agent with custom tools to add items, mark them as complete, and save the todo list to a text file.Sample Input
Sequential inputs:"Add buy milk to my todo list.""Save my todo list to todos.txt."
Sample Output
"Todo list updated successfully. Current todos: ['buy milk']""Todo list has been saved successfully to 'todos.txt'."
Plan
- Create an
AgentStateschema for messages and a global listtodo_list = []to store items. - Create
@toolforadd_todo(todo: str)(appends to list) andsave_todos(filename: str)(writes list to file). - Initialize
StateGraph, register agent and tool nodes, add loops, compile, and run.
Solution
Solution
Practice & Exercises
To reinforce what you’ve learned in this section, practice with the interactive notebook:Practice & Exercises
Practice managing global variable states inside custom tools, executing loops until a termination response is triggered, and compiling custom tool workflows.💻 VS Code | 🚀 Colab | 📥 Download