Transforming AI from a "single-shot" responder into an autonomous, self-correcting, and goal-driven system.
AgentLoop is a minimal, observable agentic framework for code optimization tasks. It gives an LLM a goal, a tool, and an environment, then lets it loop until the goal is met or it runs out of attempts.
Built as the foundation for KernelScope, an AI-powered GPU kernel optimizer.
Task + Code + Goals
↓
[Agent thinks] ← LLM reasons about history
↓
[Agent acts] ← proposes a code modification
↓
[Environment runs] ← compiles + profiles the code
↓
[Agent reflects] ← reads feedback, updates memory
↓
Goal met? → Exit
else → repeat
Each component maps directly to a module:
| Module | Responsibility |
|---|---|
agent.py |
Think, act, reflect (the only file that calls the LLM) |
environment.py |
Compile, profile, return feedback |
memory.py |
Accumulate iteration history across the loop |
modifier.py |
Data model for a proposed code change |
feedback.py |
Data model for environment results |
loop.py |
Wire everything together, entry point |
loop.py
├── Environment(goal_metric)
├── Memory()
└── Agent(environment, memory, task, code, goal, llm_provider)
│
├── think() → builds prompt from task + memory.summary()
├── act() → parses LLM response into Modifier
├── reflect() → checks last feedback, decides to continue
└── run() → orchestrates the full loop
│
├── environment.run(modifier)
│ ├── _compile(code)
│ └── _profile(code)
│
└── memory.store(modifier, feedback)
AgentLoop is provider-agnostic. Switch LLMs with one word:
| Provider | Cost | Setup |
|---|---|---|
ollama |
Free, local | Install Ollama, run ollama pull llama3 |
groq |
Free tier | Get key at console.groq.com |
anthropic |
Paid | Get key at console.anthropic.com |
openai |
Paid | Get key at platform.openai.com |
git clone https://github.com/jagkt/agentloop
cd agentloop
pip install -e ".[all]"Copy the example env file:
cp .env.example .envAdd your keys to .env:
GROQ_API_KEY=your_key_here
ANTHROPIC_API_KEY=your_key_here
OPENAI_API_KEY=your_key_here
python loop.pyTo switch providers, edit the last line of loop.py:
provider="ollama" # local, free, no key needed
provider="groq" # free tier, fastest
provider="anthropic" # Claude
provider="openai" # GPT-4o[Agent] Starting loop. Max iterations: 5
[Agent] ---- Iteration 1 ----
[Agent] Thinking about iteration 1...
[Agent] Rationale: No previous attempts. Starting with shared memory tiling...
[Environment] Compiling code...
[Environment] Profiling kernel...
[Agent] Feedback: compiled=True, metric=67.3, goal_met=False
[Agent] Reflecting... metric=67.3, goal not yet met. Continuing.
[Agent] ---- Iteration 2 ----
[Agent] Thinking about iteration 2...
[Agent] Rationale: Previous attempt achieved 67.3%. Increasing tile size to improve reuse...
[Environment] Compiling code...
[Environment] Profiling kernel...
[Agent] Feedback: compiled=True, metric=84.1, goal_met=True
[Agent] Goal met at iteration 2 with metric=84.1. Stopping.
[Agent] Loop complete.
agentloop/ ← repo root
agentloop/ ← Python package
providers/
__init__.py
base.py # abstract LLM interface
groq_provider.py
ollama_provider.py
anthropic_provider.py
openai_provider.py
__init__.py
agent.py # LLM reasoning loop
environment.py # mock compile + profile
feedback.py # data model: environment results
loop.py # entry point
memory.py # iteration history
modifier.py # data model: proposed code change
.env.example
.gitignore
CONTRIBUTING.md
LICENSE
pyproject.toml
README.md
AgentLoop is designed to be extended. To build your own optimizer:
- Install AgentLoop:
pip install agentloop - Implement a custom environment:
from agentloop.feedback import Feedback
from agentloop.modifier import Modifier
class MyEnvironment:
def run(self, modifier: Modifier) -> Feedback:
# compile, profile, return feedback
...- Wire it into the loop:
from agentloop.agent import Agent
from agentloop.memory import Memory
agent = Agent(
environment=MyEnvironment(),
memory=Memory(),
task="...",
original_code="...",
goal_description="...",
llm_provider=GroqProvider(api_key="...")
)
agent.run(max_iterations=5)See KernelScope for a full real-world example.
- v0.2 - YAML task DSL
- v0.3 - Structured reward signals
- v0.4 - Parallel environment execution
- v1.0 - Plugin system for custom environments
See CONTRIBUTING.md for guidelines.
MIT