Overview
Our Code Assistant v2 introduced some limited reasoning capabilities through Tools and a Network of Agents. This third version will transform our Code Assistant into a semi-autonomous AI Agent that can solve bugs and improve code. Our AI Agent will operate over an Express API project containing bugs:Setup
Similarly to the Code Assistant v2, perform the following steps to setup your project:1. Initialize your project
1. Initialize your project
2. Install the required dependencies
2. Install the required dependencies
3. Add TypeScript support
3. Add TypeScript support
package.json
:Implementing our Code Assistant v3
Overview of the autonomous workflow
Our Code Assistant v3 introduces autonomy through a specialized Router Agent that orchestrates two task-specific Agents:plannerAgent
: Analyzes code and plans fixes using code search capabilitieseditorAgent
: Implements the planned fixes using file system operations
Implementing the Tools
Our Code Assistant v3 needs to interact with the file system and search through code. Let’s implement these capabilities as Tools:Some notes on the highlighted lines:
- As noted in the “Building Effective Agents” article from Anthropic, Tools based on file system operations are most effective when provided with absolute paths.
- Tools performing action such as
writeFile
should always return a value to inform the Agent that the action has been completed.
writeFile
: Write content to a filereadFile
: Read content from a filesearchCode
: Search for patterns in project files
Creating the Task-Specific Agents
Our Code Assistant v3 relies on two specialized Agents:plannerAgent
uses thesearchCode
Tool to analyze code and plan fixeseditorAgent
uses thereadFile
andwriteFile
Tools to implement fixes
Implementing the Router Agent
The Router Agent is the “brain” of our Code Assistant, deciding which Agent to use based on the context. The router developed in the Code Assistant v2 was a function that decided which Agent to call next based on the progress of the workflow. Such router made a Agent deterministic, but lacked the reasoning capabilities to autonomously orchestrate the Agents. In this version, we will provide an Agent as a router, called a Router Agent. By doing so, we can leverage the reasoning capabilities of the LLM to autonomously orchestrate the Agents around a given goal (here, fixing the bug). Creating a Router Agent is done by using thecreateRoutingAgent
helper function:
- A Router Agent is a regular Agent with a
system
function that returns a prompt - A Router Agent can use Tools to interact with the environment
- Finally, a Router Agent can also define lifecycle callbacks, like Agents do
- The
system
function is used to define the prompt dynamically based on the Agents available in the Network- You will notice that the prompt explicitly ask to call a “finished” tool when the user issue has been fixed
- The
select_agent
Tool is used to validate that the Agent selected is available in the Network- The tool ensures that the “finished” edge case is handled
- The
onRoute
lifecycle callback is used to determine which Agent to call next- This callback stops the conversation when the user issue has been fixed (when the “finished” Agent is called)
Assembling the Network
Finally, assemble the Network of Agents and Router Agent:Running our Code Assistant v3
First, go to your Anthropic dashboard and create a new API key. Then, run the following command to start the server:http://localhost:3010
and ready to help fix bugs in your TypeScript projects!
What we’ve learned so far
Let’s recap what we’ve learned so far:- Autonomous AI Agents can be built by using Router Agents, which act as the “brain” of an autonomous system by orchestrating other Agents
- Tools provide Agents with capabilities to interact with their environment