Write agents as reconciliation loops. Details here: https://rednafi.com/ai/agent-reconciliation-loop/ A developer published a script that structures an LLM agent as a reconciliation loop, splitting work into observe, decide, apply and report phases. The example fetches Hacker News front-page stories and comments, uses a GPT-6 Luna Decisions API call to score thread controversy and pick the top stories without changing state, then has a Strands agent on GPT-6.1 Terra read the selected threads through an hn_comments tool and return a digest. Dependencies are injected as keyword arguments, with main() acting as the composition root, and the script runs via uv using PEP 723 inline metadata. | | /usr/bin/env -S uv run --script | | | /// script | | | requires-python = " =3.14" | | | dependencies = | | | "httpx =0.29", | | | "openai =3.62", | | | "pydantic =2.14", | | | "strands-agents openai =1.68", | | | | | | /// | | | """Summarize the most controversial stories on the Hacker News front page. | | | The run follows observe - decide - apply - report: | | | - observe: fetch the front page and the comments of each story as State . | | | - decide: GPT-6 Luna Decisions API scores how heated each discussion is and | | | returns a Plan with the top stories. Changes nothing. | | | - apply: a Strands agent on GPT-6.1 Terra reads the picked threads through the | | | hn comments tool and returns a Result holding the plan and digest. | | | - report: print the result. | | | Every function gets its dependencies as keyword arguments. main is the | | | composition root: it builds the clients, the tool and the agent, and wires | | | them together. | | | Run it with uv, which reads the inline metadata above PEP 723 , creates an | | | isolated environment and installs the dependencies on first run: | | | export OPENAI API KEY=sk-... | | | uv run agent.py | | | Or run it directly, through the shebang: | | | chmod +x agent.py | | | ./agent.py | | | For repeatable installs, lock the dependencies next to the script. uv picks up | | | agent.py.lock automatically on later runs: | | | uv lock --script agent.py | | | """ | | | import asyncio | | | import os | | | import sys | | | from dataclasses import asdict, dataclass | | | from html.parser import HTMLParser | | | from typing import Protocol, TextIO | | | import httpx | | | from openai import AsyncOpenAI | | | from pydantic import BaseModel | | | from strands import Agent, tool | | | from strands.models.openai responses import OpenAIResponsesModel | | | HN API = "https://hn.algolia.com/api/v1" | | | SUMMARY MODEL = "gpt-6.1-terra" | | | DECISION MODEL = "gpt-6-luna" | | | FRONT PAGE SIZE = 30 | | | COMMENTS PER STORY = 50 | | | MAX COMMENT CHARS = 600 | | | TOP N = 5 | | | CONTROVERSY LEVELS = | | | { | | | "label": "Calm", | | | "description": "Commenters mostly agree or just share information.", | | | }, | | | { | | | "label": "Mixed", | | | "description": "Some disagreement, but the tone stays friendly.", | | | }, | | | { | | | "label": "Heated", | | | "description": "Clear camps argue, with sharp pushback.", | | | }, | | | { | | | "label": "Flame war", | | | "description": "Hostile, polarized arguing dominates the thread.", | | | }, | | | | | | SYSTEM PROMPT = """\ | | | You summarize Hacker News discussions for a busy reader. | | | Read each story's comments with the hn comments tool before you write about it. | | | """ | | | SUMMARY PROMPT = """\ | | | Summarize these stories, in this order: | | | {stories} | | | For each one, write one sentence on what it's about. Then write two sentences | | | on what commenters disagree about and the mood of the thread. | | | """ | | | --------------------------------------------------------------------------- | | | Data | | | --------------------------------------------------------------------------- | | | @dataclass frozen=True | | | class Story: | | | """A front-page story and the comments observe read for it.""" | | | id: str | | | title: str | | | comments: list str | | | @property | | | def url self - str: | | | return f"https://news.ycombinator.com/item?id={self.id}" | | | @dataclass frozen=True | | | class State: | | | """The current state observe hands to decide.""" | | | stories: list Story | | | @dataclass frozen=True | | | class Verdict: | | | """Luna's controversy reading for one story.""" | | | story: Story | | | Probability-weighted level index, from 0 calm to the top level. | | | score: float | | | The single most likely level, like "Heated". | | | level: str | | | confidence: float | | | @dataclass frozen=True | | | class Plan: | | | """What decide hands to apply: the stories to summarize, most controversial first.""" | | | picks: list Verdict | | | The highest level index, so a score can be shown as "2.3 / 3". | | | max score: int | | | def verdict for self, , story id: str - Verdict: | | | return next v for v in self.picks if v.story.id == story id | | | class StorySummary BaseModel : | | | story id: str | | | title: str | | | One sentence on what the story is about. | | | about: str | | | Two sentences on the disagreement and the mood of the thread. | | | debate: str | | | class Digest BaseModel : | | | stories: list StorySummary | | | @dataclass frozen=True | | | class Result: | | | """What apply hands to report: the plan and the digest built from it.""" | | | plan: Plan | | | digest: Digest | | | class FetchComments Protocol : | | | """Fetches up to N comments of one story. The hn comments tool has this signature.""" | | | async def call self, , story id: str, limit: int = 50 - list str : ... | | | --------------------------------------------------------------------------- | | | Tools | | | --------------------------------------------------------------------------- | | | def make hn comments , http: httpx.AsyncClient, max comment chars: int : | | | """Build the hn comments tool around an injected HTTP client.""" | | | @tool | | | async def hn comments , story id: str, limit: int = 50 - list str : | | | """Return up to limit comments on a Hacker News story, replies included, in thread order. | | | Args: | | | story id: The Hacker News item ID of the story. | | | limit: The maximum number of comments to return. | | | """ | | | resp = await http.get f"/items/{story id}" | | | resp.raise for status | | | story = resp.json | | | Walk the comment tree depth-first, so each reply stays next to its parent. | | | comments = | | | stack = list reversed story "children" | | | while stack and len comments < limit: | | | node = stack.pop | | | if node.get "text" : | | | comments.append | | | strip html text=node "text" , max chars=max comment chars | | | | | | stack.extend reversed node "children" | | | return comments | | | return hn comments | | | class TextExtractor HTMLParser : | | | """Collect the text of an HTML fragment. Entities are decoded for us.""" | | | def init self - None: | | | super . init convert charrefs=True | | | self.parts: list str = | | | def handle data self, data: str - None: | | | self.parts.append data | | | def strip html , text: str, max chars: int - str: | | | parser = TextExtractor | | | parser.feed text | | | parser.close | | | HN separates paragraphs with bare