Skip to content

OneManCrew/self-extending-agent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🤖 The Self-Extending AI Agent

An AI agent that writes its own tools at runtime. When it encounters a task it has no tool for, it generates the tool in Python, validates it in a sandbox, registers it, and uses it — all without human intervention.

Full article: The Self-Extending AI Agent

Architecture

User Task
   ↓
Orchestrator Agent → missing tool? → Tool Generator (LLM)
                                         ↓
                                    Sandbox Validator
                                         ↓
                                    Tool Registry (persist to disk)
                                         ↓
                                    Orchestrator resumes → task complete ✅

Project Structure

self_extending_agent/
├── main.py            # Entry point — run demo tasks
├── orchestrator.py    # Main agent loop with self-extension
├── generator.py       # LLM-based tool code generator
├── validator.py       # AST + subprocess sandbox validator
├── registry.py        # Tool storage, persistence, and loading
├── requirements.txt
└── tools/             # Auto-generated tools stored here
    └── manifest.json

Quick Start

# Clone
git clone https://github.com/OneManCrew/self-extending-agent.git
cd self-extending-agent

# Install
pip install -r requirements.txt

# Set your API key
export OPENAI_API_KEY="your-key-here"

# Run
python main.py

What Happens

  1. Agent receives a task (e.g., "Convert 5000 USD to ILS")
  2. Checks tool registry — no fetch_exchange_rate tool exists
  3. Calls request_new_tool → LLM writes the Python function
  4. Validator checks syntax, banned patterns, runs import test in subprocess
  5. Tool registered and persisted to tools/ directory
  6. Agent uses the new tool to complete the task
  7. On next run, tool loads from disk — never generated again

Requirements

  • Python 3.10+
  • OpenAI API key (GPT-4o recommended)
  • See requirements.txt for packages

Safety

  • AST static analysis blocks dangerous patterns (eval, exec, os.system, etc.)
  • Subprocess isolation for validation
  • Max 3 retry attempts per tool generation
  • Production: use Docker containers for full sandboxing

License

MIT

Author

Levi Dorononemancrew.dev

About

The Self-Extending AI Agent — an agent that writes its own tools at runtime. Full working code from the blog post.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages