Agentic Context Engineering: Evolving Contexts for Self-Improving Language Models
ACE is a framework for building and evolving contexts that enable self-improving language models. It provides a structured approach to context engineering through three core components: Generator, Reflector, and Curator.
pip install -e .
Generator: Creates initial responses and outputs based on the current context.
Reflector: Analyzes performance and generates insights for context improvement.
Curator: Manages and evolves the context based on reflections and performance data.
ACE implements the grow-and-refine principle from the paper to maintain high-quality, comprehensive contexts:
from ace import Context, OpenAIEmbedder
# Initialize embedder for semantic similarity
embedder = OpenAIEmbedder(model="text-embedding-3-small")
# Create context with grow-and-refine
context = Context(
sections=["strategies", "common_mistakes", "insights"],
max_bullets=50, # Maximum total bullets
embedder=embedder, # Enable semantic de-duplication
similarity_threshold=0.85, # Threshold for duplicate detection
refinement_mode="lazy" # Refine only when max exceeded
)
# Add bullets (embeddings computed automatically)
context.add_bullet("strategies", "Always validate input before processing")
context.add_bullet("common_mistakes", "Forgetting to check edge cases")
# Manually trigger refinement to see stats
stats = context.refine()
print(f"Deduplicated: {stats['deduplicated']} bullets")
print(f"Pruned: {stats['pruned']} bullets")
ACE provides a flexible embedder abstraction supporting multiple backends:
OpenAI Embeddings: Production-ready embeddings via OpenAI API
from ace import OpenAIEmbedder
embedder = OpenAIEmbedder(
model="text-embedding-3-small", # or "text-embedding-3-large"
dimensions=512 # Optional: reduce dimensions
)
Future Support: The embedder abstraction is designed to support local models via Transformers in future releases.
from ace import Context, Generator, Reflector, Curator
from ace.llm import OpenAIClient
from ace.utils import PromptManager
# Initialize LLM client
llm = OpenAIClient(model="gpt-4")
# Load prompts
prompts = PromptManager.from_yaml("prompts.yml")
# Initialize components
context = Context()
generator = Generator(llm=llm, prompts=prompts)
reflector = Reflector(llm=llm, prompts=prompts)
curator = Curator(llm=llm, prompts=prompts)
# Generate and improve
output = generator.generate(context, task_input)
reflection = reflector.reflect(context, task_input, output)
context = curator.curate(context, reflection)
See the examples/ directory for complete examples:
examples/finer/): Financial Named Entity Recognition demonstrating:
Run the example:
cd examples/finer
python finer_example.py
ACE follows a modular abstraction pattern:
ace.llm.BaseLLM): Supports multiple LLM providers (OpenAI, Anthropic, local models)ace.embeddings.BaseEmbedder): Supports multiple embedding backends (OpenAI, Transformers)ace.Context): Structured, evolving knowledge base with grow-and-refineThis design allows easy switching between providers and models without changing application code.
Python 3.8 or higher
OpenAI API key (set in environment or .env file)
Dependencies:
openai - For LLM and embeddingsnumpy - For embedding operationspyyaml - For prompt managementpython-dotenv - For environment variablesThis project was developed using Cursor with Claude 4.5 Sonnet. The generated code required careful human review and multiple corrections.
Examples of issues that were identified and fixed:
These issues underscore the importance of human oversight when working with AI-generated code, even with state-of-the-art models. It is also questionable how much more productive I was with the help of cursor, as compared to me writing the codebase with small to little AI involvement, considering how much code I had to re-write. In my opinion, it was probably 25-50% faster vs coding without any llm help, which is still a decent improvement.
This implementation is based on the paper:
Agentic Context Engineering: Evolving Contexts for Self-Improving Language Models
Qizheng Zhang, Changran Hu, Shubhangi Upasani, Boyuan Ma, Fenglu Hong,
Vamsidhar Kamanuru, Jay Rainton, Chen Wu, Mengmeng Ji, Hanchen Li,
Urmish Thakker, James Zou, Kunle Olukotun
arXiv:2510.04618, 2024
Paper: https://arxiv.org/abs/2510.04618
Apache License 2.0