Agentic Context Engineering (ACE) middleware for LangChain v1 agents
Project description
๐ฆ๏ธ๐ langchain-continuous-learning
ACE (Agentic Context Engineering) middleware for LangChain agents that enables self-improvement through evolving playbooks.
Overview
ACE is a technique developed at Stanford that enables agents to self-improve by treating context as an evolving playbook. This playbook accumulates and refines strategies through a process of reflection and curation.
Based on the research paper: Agentic Context Engineering: Evolving Contexts for Self-Improving Language Models
Installation
pip install langchain-continuous-learning
For development:
pip install langchain-continuous-learning[test]
Quick Start
from langchain.agents import create_agent
from ace import ACEMiddleware
from langchain_core.messages import HumanMessage
# Create ACE middleware
ace = ACEMiddleware(
reflector_model="gpt-4o-mini", # Analyzes agent responses
curator_model="gpt-4o-mini", # Curates the playbook
curator_frequency=10, # Update playbook every 10 interactions
)
# Create agent with ACE middleware
agent = create_agent(
model="gpt-4o",
tools=[calculator, search],
middleware=[ace],
)
# Use the agent - it will self-improve over time
result = agent.invoke({
"messages": [HumanMessage(content="Calculate the NPV of...")]
})
How It Works
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Agent Loop โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โ
โ โ Playbook โโโโโถโ Model โโโโโถโ Response โ โ
โ โ (injected) โ โ Call โ โ โ โ
โ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โโโโโโโโฌโโโโโโโโ โ
โ โฒ โ โ
โ โ โผ โ
โ โโโโโโโโดโโโโโโโโ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โ
โ โ Curator โโโโโโ Reflector โโโโโโ Trajectory โ โ
โ โ (periodic) โ โ (analyze) โ โ Analysis โ โ
โ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Three-Role Architecture
| Role | Purpose | When It Runs |
|---|---|---|
| Generator | Uses playbook to enhance responses | Every model call |
| Reflector | Analyzes trajectories and tags bullets | After each response |
| Curator | Adds new insights to playbook | Every N interactions |
The Playbook
The playbook is a structured document with bullets organized by section:
## strategies_and_insights
[str-00001] helpful=5 harmful=0 :: Always verify data types before processing
[str-00002] helpful=3 harmful=1 :: Consider edge cases in financial data
## common_mistakes_to_avoid
[mis-00001] helpful=6 harmful=0 :: Don't forget timezone conversions
Each bullet tracks:
- ID: Unique identifier for tracking
- Counts:
helpful=X harmful=Yupdated by the reflector - Content: The actual advice or strategy
Training with Ground Truth
When you have ground truth answers, pass them for faster learning:
# Training with ground truth
for item in training_data:
result = agent.invoke({
"messages": [HumanMessage(content=item["question"])],
"ground_truth": item["answer"], # Enables precise feedback
})
# Inference without ground truth
result = agent.invoke({
"messages": [HumanMessage(content="What is 20% of 150?")]
})
Configuration
ace = ACEMiddleware(
# Models (required)
reflector_model="gpt-4o-mini", # Or BaseChatModel instance
curator_model="gpt-4o-mini", # Or BaseChatModel instance
# Playbook settings
initial_playbook=None, # Custom starting playbook
curator_frequency=5, # Curate every N interactions
playbook_token_budget=80000, # Max tokens for playbook
# Pruning settings
auto_prune=False, # Auto-remove harmful bullets
prune_threshold=0.5, # Harmful ratio threshold
prune_min_interactions=3, # Min interactions before pruning
# Training progress
expected_interactions=100, # For progress tracking
)
Playbook Sections
| Section | Slug | Purpose |
|---|---|---|
strategies_and_insights |
str |
General approaches and tactics |
formulas_and_calculations |
cal |
Mathematical formulas |
code_snippets_and_templates |
cod |
Reusable code patterns |
common_mistakes_to_avoid |
mis |
Known pitfalls |
problem_solving_heuristics |
heu |
Decision-making rules |
context_clues_and_indicators |
ctx |
Problem type signals |
others |
oth |
Miscellaneous insights |
API Reference
ACEMiddleware
The main middleware class that implements the ACE framework.
Playbook Utilities
from ace import (
ACEPlaybook, # Dataclass for playbook state
SectionName, # Enum of section names
initialize_empty_playbook,
parse_playbook_line,
format_playbook_line,
extract_bullet_ids,
update_bullet_counts,
get_playbook_stats,
limit_playbook_to_budget,
)
Features Beyond Base Implementation
- Fresh bullet protection: Newly curated bullets survive at least one round
- Token budget enforcement: Automatic playbook trimming with priority-based selection
- Multi-turn support: Works across conversation turns
- Tool integration: Full support for tool-using agents
References
- Paper: Agentic Context Engineering
- Original Implementation: github.com/ace-agent/ace
License
MIT License - see LICENSE for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file langchain_continuous_learning-0.1.0.tar.gz.
File metadata
- Download URL: langchain_continuous_learning-0.1.0.tar.gz
- Upload date:
- Size: 152.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b3707165854e22d2ec5413b5509955f463286a7cebc51ab39fb4048d890a9f50
|
|
| MD5 |
43461e903a36c534c9569619450ddb29
|
|
| BLAKE2b-256 |
ed22228b4acc27c41a24c353b31aeb0dc5994f8807db1130b06e4046343d6254
|
Provenance
The following attestation bundles were made for langchain_continuous_learning-0.1.0.tar.gz:
Publisher:
release.yml on dkondo/langchain-continuous-learning
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
langchain_continuous_learning-0.1.0.tar.gz -
Subject digest:
b3707165854e22d2ec5413b5509955f463286a7cebc51ab39fb4048d890a9f50 - Sigstore transparency entry: 844442470
- Sigstore integration time:
-
Permalink:
dkondo/langchain-continuous-learning@0ed44465cd31fe0ea77eeeb641f29c908b4fa932 -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/dkondo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@0ed44465cd31fe0ea77eeeb641f29c908b4fa932 -
Trigger Event:
push
-
Statement type:
File details
Details for the file langchain_continuous_learning-0.1.0-py3-none-any.whl.
File metadata
- Download URL: langchain_continuous_learning-0.1.0-py3-none-any.whl
- Upload date:
- Size: 26.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1b0ac7d45bae0004e226ba8284093aa5248eca16e1566fe223d5d6b194dee2ad
|
|
| MD5 |
4e86c6a7da3b7e9aa5df891e47ab39dd
|
|
| BLAKE2b-256 |
618adc5326d6300ce7006700dd1bb0493b035b3d5e4ebfc2476ebeaa8ab79ab1
|
Provenance
The following attestation bundles were made for langchain_continuous_learning-0.1.0-py3-none-any.whl:
Publisher:
release.yml on dkondo/langchain-continuous-learning
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
langchain_continuous_learning-0.1.0-py3-none-any.whl -
Subject digest:
1b0ac7d45bae0004e226ba8284093aa5248eca16e1566fe223d5d6b194dee2ad - Sigstore transparency entry: 844442476
- Sigstore integration time:
-
Permalink:
dkondo/langchain-continuous-learning@0ed44465cd31fe0ea77eeeb641f29c908b4fa932 -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/dkondo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@0ed44465cd31fe0ea77eeeb641f29c908b4fa932 -
Trigger Event:
push
-
Statement type: