> ## Documentation Index
> Fetch the complete documentation index at: https://codegeninc-tawsif-change-additional-tools-to-tools.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Building Code Agents

This guide demonstrates how to build an intelligent code agent that can analyze and manipulate codebases.

```python
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

<Info>View the full code for the default tools and agent implementation in our [examples repository](https://github.com/codegen-sh/codegen-sdk/tree/develop/src/codegen/extensions/langchain/tools)</Info>

# Basic Usage

The following example shows how to create and run a `CodeAgent`:

```python
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")
```

<Note>Your `ANTHROPIC_API_KEY` must be set in your env.</Note>

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.

```python
agent = CodeAgent(
    codebase=codebase,
    model_provider="openai",
    model_name="gpt-4o",
)
```

<Note>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.</Note>

# Available Tools

The agent comes with a comprehensive set of tools for code analysis and manipulation. Here are some key tools:

```python
from codegen.extensions.langchain.tools import (
    CreateFileTool,
    DeleteFileTool,
    EditFileTool,
    ListDirectoryTool,
    MoveSymbolTool,
    RenameFileTool,
    ReplacementEditTool,
    RevealSymbolTool,
    SearchTool,
    SemanticEditTool,
    ViewFileTool,
)
```

<Note>View the full set of [tools on Github](https://github.com/codegen-sh/codegen-sdk/blob/develop/src/codegen/extensions/langchain/tools.py)</Note>

Each tool provides specific capabilities:

# Extensions

## GitHub Integration

The agent includes tools for GitHub operations like PR management. Set up GitHub access with:

```bash
CODEGEN_SECRETS__GITHUB_TOKEN="..."
```

Import the GitHub tools:

```python
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

<Note>View all Github tools on [Github](https://github.com/codegen-sh/codegen-sdk/blob/develop/src/codegen/extensions/langchain/tools.py)</Note>

## Linear Integration

The agent can interact with Linear for issue tracking and project management. To use Linear tools, set the following environment variables:

```bash
LINEAR_ACCESS_TOKEN="..."
LINEAR_TEAM_ID="..."
LINEAR_SIGNING_SECRET="..."
```

Import and use the Linear tools:

```python
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

<Note>View all Linear tools on [Github](https://github.com/codegen-sh/codegen-sdk/blob/develop/src/codegen/extensions/langchain/tools.py)</Note>

## Adding Custom Tools

You can extend the agent with custom tools:

```python
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")
```
