A simple AI coding agent built in Go that can read, list, and edit files using Claude 3.7 Sonnet. This agent demonstrates how AI can interact with your local filesystem to help you with coding tasks.
- Go installed on your system (version 1.21+ recommended)
- An Anthropic API key for Claude 3.7 Sonnet
-
Clone this repository:
git clone https://github.com/yourusername/go-coding-agent.git cd go-coding-agent
-
Set your Anthropic API key in one of these ways:
-
Edit the
.env
file with your API key:ANTHROPIC_API_KEY="your-api-key-here"
-
Or set it as an environment variable:
export ANTHROPIC_API_KEY="your-api-key-here"
-
-
Install dependencies:
go mod tidy
-
Run the agent:
./run.sh
Or directly with Go:
go run main.go
This agent can:
- List files (
list_files
): View files and directories in your workspace - Read files (
read_file
): View the contents of any text file - Edit files (
edit_file
): Modify existing files or create new ones
Once the agent is running, you can ask it to perform tasks like:
What files are in this directory?
The agent will use the list_files
tool to show you all files in the current directory.
Create a file called hello.js that prints "Hello, world!" to the console
The agent will create a new JavaScript file with appropriate code.
Show me what's in the main.go file
The agent will read and display the contents of main.go.
Edit the README.md file to add a license section at the end
The agent will append a license section to the README.md file.
Create a Python script that calculates Fibonacci numbers
The agent will create a new Python file with a Fibonacci implementation.
The agent will automatically decide which tools to use to accomplish each task based on your request.
For more examples, check out the examples/example.md
file in this repository.
The coding agent is built with:
-
Tool-Based Architecture:
- Each capability (reading files, listing directories, editing files) is implemented as a "tool"
- The agent receives a description of available tools and decides when to use them
- Each tool has a name, description, input schema, and function implementation
-
Claude Integration:
- Uses Claude 3.7 Sonnet via the Anthropic SDK for Go
- The model autonomously decides when to use tools based on your requests
- Conversation history is maintained throughout the session
-
File System Interaction:
- File operations are performed safely with error handling
- The agent can create, read, and modify files in your local filesystem
- String replacement is used for file editing to keep the implementation simple
main.go
- Main application code including tool definitions and Claude integrationgo.mod
andgo.sum
- Go module definition and dependencies.env
- Environment variables for API keysrun.sh
- Convenience script to run the agent
The current implementation is intentionally minimal (under 400 lines of code). Here are potential areas for enhancement:
-
Additional Tools:
- Add a
run_code
tool to execute scripts and display output - Add a
search_code
tool to find patterns across multiple files - Add git integration for version control operations
- Add a
-
Better File Editing:
- Implement more sophisticated editing capabilities beyond string replacement
- Add support for structural code transformations
- Add a diff preview before applying changes
-
Enhanced Security:
- Add a confirmation prompt before file modifications
- Implement sandbox restrictions for file operations
- Add allowlist/denylist for file locations and operations
-
UI Improvements:
- Create a web interface or terminal UI
- Add syntax highlighting for code display
- Implement a better conversation history viewer
-
Performance Enhancements:
- Add caching for model responses
- Implement streaming responses for large file operations
- Optimize token usage for longer conversations
-
Multi-Agent Coordination:
- Allow multiple specialized agents to collaborate
- Implement a coordinator agent to delegate tasks
MIT
This project was inspired by the "How to Build an Agent" guide by Thorsten Ball and the team at Amp.