This guide demonstrates how to build an intelligent code agent that can analyze and manipulate codebases.
from codegen import CodeAgent, Codebase
# Grab a repo from Github
codebase = Codebase.from_repo('fastapi/fastapi')
# Create a code agent with read/write codebase access
agent = CodeAgent(codebase)
# Run the agent with a prompt
agent.run("Tell me about this repo")
The agent has access to powerful code viewing and manipulation tools powered by Codegen, including:
ViewFileTool
: View contents and metadata of files
SemanticEditTool
: Make intelligent edits to files
RevealSymbolTool
: Analyze symbol dependencies and usages
MoveSymbolTool
: Move symbols between files with import handling
ReplacementEditTool
: Make regex-based replacement editing on files
ListDirectoryTool
: List directory contents
SearchTool
: Search for files and symbols
CreateFileTool
: Create new files
DeleteFileTool
: Delete files
RenameFileTool
: Rename files
EditFileTool
: Edit files
Basic Usage
The following example shows how to create and run a CodeAgent
:
from codegen import CodeAgent, Codebase
# Grab a repo from Github
codebase = Codebase.from_repo('fastapi/fastapi')
# Create a code agent with read/write codebase access
agent = CodeAgent(codebase)
# Run the agent with a prompt
agent.run("Tell me about this repo")
Your ANTHROPIC_API_KEY
must be set in your env.
The default implementation uses anthropic/claude-3-5-sonnet-latest
for the model but this can be changed through the model_provider
and model_name
arguments.
agent = CodeAgent(
codebase=codebase,
model_provider="openai",
model_name="gpt-4o",
)
If using a non-default model provider, make sure to set the appropriate API key (e.g., OPENAI_API_KEY
for OpenAI models) in your env.
Available Tools
The agent comes with a comprehensive set of tools for code analysis and manipulation. Here are some key tools:
from codegen.extensions.langchain.tools import (
CreateFileTool,
DeleteFileTool,
EditFileTool,
ListDirectoryTool,
MoveSymbolTool,
RenameFileTool,
ReplacementEditTool,
RevealSymbolTool,
SearchTool,
SemanticEditTool,
ViewFileTool,
)
Each tool provides specific capabilities:
Extensions
GitHub Integration
The agent includes tools for GitHub operations like PR management. Set up GitHub access with:
CODEGEN_SECRETS__GITHUB_TOKEN="..."
Import the GitHub tools:
from codegen.extensions.langchain.tools import (
GithubCreatePRTool,
GithubViewPRTool,
GithubCreatePRCommentTool,
GithubCreatePRReviewCommentTool
)
These tools enable:
- Creating pull requests
- Viewing PR contents and diffs
- Adding general PR comments
- Adding inline review comments
View all Github tools on
Github
Linear Integration
The agent can interact with Linear for issue tracking and project management. To use Linear tools, set the following environment variables:
LINEAR_ACCESS_TOKEN="..."
LINEAR_TEAM_ID="..."
LINEAR_SIGNING_SECRET="..."
Import and use the Linear tools:
from codegen.extensions.langchain.tools import (
LinearGetIssueTool,
LinearGetIssueCommentsTool,
LinearCommentOnIssueTool,
LinearSearchIssuesTool,
LinearCreateIssueTool,
LinearGetTeamsTool
)
These tools allow the agent to:
- Create and search issues
- Get issue details and comments
- Add comments to issues
- View team information
View all Linear tools on
Github
You can extend the agent with custom tools:
from langchain.tools import BaseTool
from pydantic import BaseModel, Field
from codegen import CodeAgent
class CustomToolInput(BaseModel):
"""Input schema for custom tool."""
param: str = Field(..., description="Parameter description")
class CustomCodeTool(BaseTool):
"""A custom tool for the code agent."""
name = "custom_tool"
description = "Description of what the tool does"
args_schema = CustomToolInput
def _run(self, param: str) -> str:
# Tool implementation
return f"Processed {param}"
# Add custom tool to agent
tools.append(CustomCodeTool())
agent = CodebaseAgent(codebase, tools=tools, model_name="claude-3-5-sonnet-latest")