Code Assistant v3: Autonomous Bug Solver
Build a custom Agent Router to autonomously solve bugs.
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:
Given a prompt such as:
Our Code Assistant v3 will autonomously navigate through the codebase and fix the bug by updating the impacted files.
This new version relies on previously covered concepts such as Tools, Agents, and Networks but introduces the creation of a custom Router Agent bringing routing autonomy to the AI Agent.
Let’s learn these concepts in practice.
Setup
Similarly to the Code Assistant v2, perform the following steps to setup your project:
You are now set up, let’s implement our autonomous Code Assistant.
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
The Router Agent acts as the “brain” of our Code Assistant, deciding which Agent to use based on the current context and user request.
Let’s implement each component of our autonomous workflow.
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.
These Tools provide our Agents with the following capabilities:
writeFile
: Write content to a filereadFile
: Read content from a filesearchCode
: Search for patterns in project files
Let’s now create our task-specific Agents.
Creating the Task-Specific Agents
Our Code Assistant v3 relies on two specialized Agents:
Each Agent has a specific role:
plannerAgent
uses thesearchCode
Tool to analyze code and plan fixeseditorAgent
uses thereadFile
andwriteFile
Tools to implement fixes
Separating the Agents into two distinct roles will enable our AI Agent to better “divide and conquer” the problem to solve.
Let’s now implement the Router Agent that will bring the reasoning capabilities to autonomously orchestrate these Agents.
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 the createRoutingAgent
helper function:
Looking at the highlighted lines, we can see that a Router Agent mixes features from regular Agents and a function Router:
- 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
Let’s now dissect how this Router Agent works:
- 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)
This is it! Using this prompt, our Router Agent will orchestrate the Agents until the given bug is fixed.
Assembling the Network
Finally, assemble the Network of Agents and Router Agent:
Our Code Assistant v3 is now complete and ready to be used!
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:
Your Code Assistant is now running at 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