Skip to content

Commit 8676104

Browse files
author
AgentPatterns
committed
feat(examples/python): add reflection-agent runnable example
1 parent 798b20d commit 8676104

6 files changed

Lines changed: 854 additions & 0 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Reflection Agent - Python Implementation
2+
3+
Runnable implementation of a Reflection agent with one controlled review pass,
4+
one optional patch-only revision, and strict stop reasons for auditability.
5+
6+
---
7+
8+
## Quick start
9+
10+
```bash
11+
# (optional) create venv
12+
python -m venv .venv && source .venv/bin/activate
13+
14+
# install dependencies
15+
pip install -r requirements.txt
16+
17+
# set API key
18+
export OPENAI_API_KEY="sk-..."
19+
20+
# run the agent
21+
python main.py
22+
```
23+
24+
## Full walkthrough
25+
26+
Read the complete implementation guide:
27+
https://agentpatterns.tech/en/agent-patterns/reflection-agent
28+
29+
## What's inside
30+
31+
- `Draft -> Review -> Revise -> Finalize` flow
32+
- Reflection review contract (`approve | revise | escalate`)
33+
- Policy vs execution split for reflection decisions
34+
- Patch guardrails (`no_new_facts`, max edit distance, one revision)
35+
- Run budgets (`max_seconds`, length limits)
36+
- Structured `trace` and `history`
37+
38+
## Project layout
39+
40+
```text
41+
examples/
42+
agent-patterns/
43+
reflection-agent/
44+
python/
45+
README.md
46+
main.py
47+
llm.py
48+
gateway.py
49+
context.py
50+
requirements.txt
51+
```
52+
53+
## Notes
54+
55+
- Code and README are English-only by design.
56+
- This demo keeps one in-memory run and does not include persistent storage.
57+
58+
## License
59+
60+
MIT
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from __future__ import annotations
2+
3+
from typing import Any
4+
5+
6+
def build_incident_context(*, report_date: str, region: str) -> dict[str, Any]:
7+
return {
8+
"report_date": report_date,
9+
"region": region,
10+
"incident": {
11+
"incident_id": "inc_payments_20260305",
12+
"severity": "P1",
13+
"status": "degraded",
14+
"affected_checkout_pct": 27,
15+
"failed_payment_rate": 0.034,
16+
"chargeback_alerts": 5,
17+
"eta_minutes": 45,
18+
},
19+
"policy_hints": {
20+
"avoid_absolute_guarantees": True,
21+
"required_sections": ["current_status", "customer_impact", "next_actions"],
22+
},
23+
"approved_actions": [
24+
"monitor payment failures every 15 minutes",
25+
"publish customer update via status page",
26+
"prepare support macro with workaround guidance",
27+
],
28+
}

0 commit comments

Comments
 (0)