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
  • SemanticSearchTool: Search over code using natural language queries
  • SemanticEditTool: Make intelligent edits to files
  • RevealSymbolTool: Analyze symbol dependencies and usages
  • MoveSymbolTool: Move symbols between files with import handling
  • And many moreā€¦
View the full code for the default tools and agent implementation in our examples repository

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.

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 (
    ViewFileTool,
    SemanticSearchTool, 
    SemanticEditTool,
    RevealSymbolTool,
    MoveSymbolTool,
    EditFileTool,
    CreateFileTool,
    DeleteFileTool,
    ListDirectoryTool,
    SearchTool,
)
View the full set of tools on Github

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

Adding Custom Tools

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="gpt-4o")

Was this page helpful?