Streaming review output for large PRs
On a 40-file PR, waiting for the full review before showing anything kills the feel. Streaming lets the first few comments appear in seconds while the model keeps going. Reviewers stay engaged and can act on early findings.
review_streaming.py
python
async def review_streaming(diff: str):
"""Yield ReviewComment objects as the model emits them."""
async with anthropic.messages.stream(
model="claude-sonnet-4",
max_tokens=4096,
system=REVIEW_SYSTEM,
messages=[{"role": "user", "content": diff}],
) as stream:
buffer = ""
async for chunk in stream.text_stream:
buffer += chunk
for comment in extract_completed_comments(buffer):
yield comment
buffer = trim_consumed(buffer)Each completed JSON comment object is yielded as soon as it parses cleanly. Reviewers see findings while later files are still being analyzed.
Two tradeoffs to know. Partial JSON parsing needs care: a half-emitted object will fail validation, so you only forward objects that close cleanly. And streaming inside MCP works best when the client supports incremental tool output. Claude Desktop and Cursor both do.
Quiz: Quiz
Loading practice…