reflection=True on an agent and it critiques its own answer before returning — improving quality within the same task.
Quick Start
1
Simple Usage
2
With Configuration
How It Works
After each response, the agent runs one or more reflection passes (betweenmin_iterations and max_iterations). Each pass asks whether the answer meets quality criteria; if not, the agent revises before returning.
If a reflection response cannot be parsed (for example, an OpenAI structured-output refusal, a content-filter block, or a truncation), the agent retries that pass. Retries are bounded by max_iterations — after that many failed passes, the agent returns the current draft un-reflected instead of continuing to retry.
Troubleshooting
Persistent parse failures terminate cleanly atmax_iterations instead of looping forever.
When the reflection LLM keeps returning an unparseable response (a structured-output refusal, a content-filter block, or a
finish_reason="length" truncation), the agent stops after max_iterations failed passes and returns the current draft un-reflected.With verbose=True, look for this log line on the terminating pass:Maximum reflection count reached after repeated parse errors, returning current responseIf you see it often:- Lower
max_iterationsto fail fast. - Switch the reflection LLM with
ReflectionConfig(llm=...)to one less prone to refusals. - Narrow the
promptso the reflection response fits the required JSON shape.
chat() returns None and the chat history is rolled back to the pre-turn state.Configuration Options
ReflectionConfig SDK Reference
Full parameter reference for ReflectionConfig
Choosing Reflection Strength
Best Practices
Disable for tool-heavy agents
Disable for tool-heavy agents
Reflection adds an extra LLM pass per turn. Set
reflection=False on agents that call tools frequently to keep latency down.Pair with a dedicated reflection model
Pair with a dedicated reflection model
Use
ReflectionConfig(llm="gpt-4o") when the main model is fast but you want a stronger critic.Do not confuse with self_improve
Do not confuse with self_improve
reflection improves this answer within the task. self_improve captures reusable skills for next time — they compose independently.Set max_iterations for cost control
Set max_iterations for cost control
Cap
max_iterations in production to avoid runaway loops on open-ended prompts. It also bounds parse-error retries — if the reflection LLM keeps returning an unparseable response, the agent stops after max_iterations failed passes and returns the current draft un-reflected.Related
Self Improve
Capture reusable skills after each task
Guardrails
Validate agent outputs with policies
Planning Mode
Let agents plan before acting
Execution Systems
Configure agent execution limits

