Spaces:
Sleeping
Sleeping
File size: 8,992 Bytes
116524e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | <img src="https://framerusercontent.com/images/XBGa12hY8xKYI6KzagBxpbgY4.png" alt="Kayba Logo" width="1080"/>
# Agent Prompt Optimizer

[](https://discord.gg/mqCqH7sTyK)
[](https://twitter.com/kaybaai)
[](https://badge.fury.io/py/ace-framework)
[](https://www.python.org/downloads/)

## The Problem with Manual System Prompting
- Time-consuming iteration cycles of trial and error
- Prompt drift and regression as you patch edge cases
- No systematic learning from agent failures
- Knowledge stays in your head instead of in the prompt
- Hard to manage as prompts scale
## The Solution
ACE (Agentic Context Engine) automatically optimizes your agent's system prompt by learning from execution. It observes agent runs, analyzes what strategies worked and what failed, then generates actionable insights for the system prompt.
`Traces / Conversations` **→** `ACE` **→** `Prompt Suggestions`
You put in past traces or conversations. ACE handles agentic system prompting by learning from mistakes. You receive improved system prompt suggestions.
**How it works:**
0. **Prepare your data** - Export/convert your agent conversations to `.md` or `.toon` files and place them in a directory. To convert JSON to TOON, use the included `convert.py` script or the toon library directly. The more detailed your traces, the better the insights.
1. **ReplayAgent** - Simulates an agent for offline learning from your trace/conversation
2. **Reflector** - Analyzes each conversation to identify what worked, what failed, and why
3. **SkillManager** - Transforms reflections into atomic, actionable prompt strategies/insights
4. **Deduplicator** - Consolidates similar strategies/insights using embeddings to keep the output clean
5. **Skillbook** - Output file stores all prompt strategies/insights in a human-readable format you can review and implement
The output is a **human-readable skillbook** where each insight contains:
- **Prompt suggestion** - The recommended text to add to your system prompt
- **Justification** - Why this change would help based on the analysis
- **Evidence** - What actually happened in the trace that led to this insight
You review each suggestion and decide what to copy into your system prompt. ACE may even suggest strategies that contradict your current prompt when it identifies flaws in the original design.
## Setup
### Installation
```bash
pip install ace-framework
```
Or for development:
```bash
git clone https://github.com/kayba-ai/agentic-context-engine
cd agentic-context-engine
uv sync
uv pip install -e . # Required to run examples
```
### API Keys
**Requirements:**
- LLM API key for analysis (e.g., `OPENAI_API_KEY`, `ANTHROPIC_API_KEY`, etc.)
- `OPENAI_API_KEY` for deduplication (uses OpenAI embeddings)
Create a `.env` file in the project root:
```bash
# Required for analysis (choose one)
OPENAI_API_KEY=your-openai-key
# OR
ANTHROPIC_API_KEY=your-anthropic-key
# Required for deduplication (uses OpenAI embeddings)
OPENAI_API_KEY=your-openai-key
```
## Implementation
### Agentic System Prompting = ACE Offline Adapter
Process past traces and conversations in batch to generate insights **without the agent running**.
**Use case:** Periodic automated system prompt revision. Feed historical data, let ACE analyze patterns, then have a human review and choose what to implement.
#### Quick Start (CLI)
```bash
# Basic usage
python agentic_system_prompting.py /path/to/traces
# With options
python agentic_system_prompting.py /path/to/traces --model gpt-4o --epochs 2
python agentic_system_prompting.py /path/to/traces --input-skillbook existing.json
python agentic_system_prompting.py /path/to/traces --output-dir ./results --threshold 0.8
```
**CLI Options:**
- `traces_dir` - Required: path to directory containing `.md` or `.toon` trace files
- `-m, --model` - LLM model for analysis (default: `claude-haiku-4-5-20251001`)
- `-e, --epochs` - Number of training epochs (default: 1)
- `-t, --threshold` - Deduplication similarity threshold 0.0-1.0 (default: 0.7)
- `-i, --input-skillbook` - Continue learning from an existing skillbook
- `-o, --output-dir` - Output directory for results (default: script directory)
**Outputs:**
- `skillbook_{timestamp}.json` - The learned skillbook
- `skills_{timestamp}.md` - Human-readable skills grouped by section
- `external_agent_injection_{timestamp}.txt` - Ready-to-inject prompt text for external agents
#### Python API
```python
from ace import (
Skillbook,
Sample,
OfflineACE,
Reflector,
SkillManager,
ReplayAgent,
SimpleEnvironment,
)
from ace.llm_providers.litellm_client import LiteLLMClient, LiteLLMConfig
from ace.prompts_v3 import PromptManager
# 1. Initialize LLM client
config = LiteLLMConfig(
model="claude-sonnet-4-5-20250929",
max_tokens=8192,
temperature=0.1,
)
llm = LiteLLMClient(config=config)
prompt_mgr = PromptManager()
# 2. Create ACE components
skillbook = Skillbook()
agent = ReplayAgent() # Dummy agent that replays conversations
reflector = Reflector(llm=llm, prompt_template=prompt_mgr.get_reflector_prompt())
skill_manager = SkillManager(llm=llm, prompt_template=prompt_mgr.get_skill_manager_prompt())
# 3. Load past conversations as samples
samples = [
Sample(
question="Your task description here",
context="The full conversation/trace content",
ground_truth="", # Empty for analysis tasks
metadata={"source": "conversation_1"}
),
# ... more historical data
]
# 4. Create adapter and run
environment = SimpleEnvironment()
adapter = OfflineACE(
skillbook=skillbook,
agent=agent,
reflector=reflector,
skill_manager=skill_manager,
)
results = adapter.run(samples, environment, epochs=1)
# 5. Review and save generated skills
print(adapter.skillbook.as_prompt())
adapter.skillbook.save_to_file("offline_adapter_skillbook.json")
```
**Tip:** Enable deduplication to automatically consolidate similar skills during learning. This keeps the skillbook clean.
```python
from ace import DeduplicationConfig
dedup_config = DeduplicationConfig(
enabled=True,
similarity_threshold=0.85,
)
adapter = OfflineACE(
skillbook=skillbook,
agent=agent,
reflector=reflector,
skill_manager=skill_manager,
dedup_config=dedup_config,
)
```
#### Async Mode
For large batches, enable async learning so the Reflector and SkillManager process in the background:
```python
adapter = OfflineACE(
skillbook=skillbook,
agent=agent,
reflector=reflector,
skill_manager=skill_manager,
async_learning=True,
max_reflector_workers=3,
)
results = adapter.run(samples, environment)
```
#### Checkpoints
Save skillbook periodically during long training runs:
```python
results = adapter.run(
samples=samples,
environment=environment,
epochs=3,
checkpoint_interval=10, # Save every 10 samples
checkpoint_dir="./checkpoints",
)
```
### Agentic Prompting at Runtime = Online Adapter
Fully autonomous self-improving agents at runtime. The agent learns from every interaction, generates insights, and injects them into future contexts automatically - no manual intervention required.
**Use case:** Continuous improvement in production where agents get better with every run.
See the [Quick Start Guide](../../docs/QUICKSTART.md) for setup instructions.
- **LiteLLM:** [`examples/litellm/`](../litellm/) - Make a new agent that self-learns
- **LangChain:** [`examples/langchain/`](../langchain/) - Wrap your existing agent with self-improving
## FAQ
**Can I combine manual prompts with ACE skills?**
Yes. ACE skills complement your base prompts. Start with manual prompts and let ACE build domain-specific expertise on top.
**What if ACE suggests something that contradicts my system prompt?**
Review it. ACE may have identified a flaw in your original design. The skillbook is human-readable JSON - you decide what to keep.
**Can I share skills between agents?**
Yes. Skillbooks are portable JSON files with human readable text.
## Next Steps
- Explore [examples](../) in this repository
- Read the [main documentation](https://github.com/kayba-ai/agentic-context-engine)
- Join our [Discord](https://discord.gg/mqCqH7sTyK) for tips and support
|