The critic returns a score and feedback. What does it take to close the loop — write, score, rewrite if the score is too low?
A loop that keeps regenerating while score < threshold? Or one extra pass is enough most of the time?
Bounded loops, not infinite ones. A for _ in range(max_iters) with an early break on pass is the safe shape. The two agents stay narrow: one writes, one scores. Feedback feeds back as context:
writer = Agent(model)
critic = Agent(model, result_type=Score, system_prompt="Score 1-10 with one-sentence feedback.")
draft = writer.run_sync(topic).output
score = critic.run_sync(draft).outputAnd if the score is low, I feed the feedback into the next writer call — so the rewriter knows what to fix?
Exactly. The feedback becomes the next prompt. Loop a few times, stop when score passes, return the final draft and its score:
def refine_with_feedback(topic: str, threshold: int = 8, max_iters: int = 3) -> dict:
writer = Agent(model)
critic = Agent(model, result_type=Score, system_prompt="Score 1-10 with one-sentence feedback.")
draft = writer.run_sync(topic).output
for _ in range(max_iters):
score_obj = critic.run_sync(draft).output
if score_obj.score >= threshold:
break
draft = writer.run_sync(f"Rewrite to address: {score_obj.feedback}. Original: {draft}").output
final = {"draft": draft, "score": score_obj.score, "feedback": score_obj.feedback}
print(f"Agent refined: {final}")
return finalWhy cap the loop with max_iters? Cannot I just keep going until it passes?
Because every iteration costs tokens, and the model may plateau below your threshold. A cap bounds worst-case cost and latency. You ship the best draft you got, even if it did not cross the bar.
So refinement is a small state machine — draft, score, decide, rewrite — and the loop terminates on pass or on budget?
A tight state machine with a budget. This is the shape every production-quality self-improving agent takes. Three agents, one bounded loop, one final state.
TL;DR: write, score, rewrite — stop on pass or on iteration cap.
Score(score, feedback)f"Rewrite to address: {feedback}"max_iters cap — bounds cost and latency| Unbounded | Bounded |
|---|---|
| Can spin forever | Terminates on pass or budget |
| Unpredictable cost | Worst-case known |
Ship the best draft you got, inside a known budget — the contract every feedback loop needs.
The critic returns a score and feedback. What does it take to close the loop — write, score, rewrite if the score is too low?
A loop that keeps regenerating while score < threshold? Or one extra pass is enough most of the time?
Bounded loops, not infinite ones. A for _ in range(max_iters) with an early break on pass is the safe shape. The two agents stay narrow: one writes, one scores. Feedback feeds back as context:
writer = Agent(model)
critic = Agent(model, result_type=Score, system_prompt="Score 1-10 with one-sentence feedback.")
draft = writer.run_sync(topic).output
score = critic.run_sync(draft).outputAnd if the score is low, I feed the feedback into the next writer call — so the rewriter knows what to fix?
Exactly. The feedback becomes the next prompt. Loop a few times, stop when score passes, return the final draft and its score:
def refine_with_feedback(topic: str, threshold: int = 8, max_iters: int = 3) -> dict:
writer = Agent(model)
critic = Agent(model, result_type=Score, system_prompt="Score 1-10 with one-sentence feedback.")
draft = writer.run_sync(topic).output
for _ in range(max_iters):
score_obj = critic.run_sync(draft).output
if score_obj.score >= threshold:
break
draft = writer.run_sync(f"Rewrite to address: {score_obj.feedback}. Original: {draft}").output
final = {"draft": draft, "score": score_obj.score, "feedback": score_obj.feedback}
print(f"Agent refined: {final}")
return finalWhy cap the loop with max_iters? Cannot I just keep going until it passes?
Because every iteration costs tokens, and the model may plateau below your threshold. A cap bounds worst-case cost and latency. You ship the best draft you got, even if it did not cross the bar.
So refinement is a small state machine — draft, score, decide, rewrite — and the loop terminates on pass or on budget?
A tight state machine with a budget. This is the shape every production-quality self-improving agent takes. Three agents, one bounded loop, one final state.
TL;DR: write, score, rewrite — stop on pass or on iteration cap.
Score(score, feedback)f"Rewrite to address: {feedback}"max_iters cap — bounds cost and latency| Unbounded | Bounded |
|---|---|
| Can spin forever | Terminates on pass or budget |
| Unpredictable cost | Worst-case known |
Ship the best draft you got, inside a known budget — the contract every feedback loop needs.
Create a free account to get started. Paid plans unlock all tracks.