"""Print the uncapped rea runs that ended with an API error, with the largest tool result in each."""
import glob, json, re

for f in sorted(glob.glob("agent-r?/rea-mcp/*.json")):
    d = json.load(open(f))
    if d.get("api_error"):
        big = max(d["tool_calls"], key=lambda c: c["chars"])
        tokens = re.search(r"prompt is too long: (\d+) tokens > (\d+)", d["api_error"])
        print(f"{d['run']} {d['task']:<17} {big['name']} returned {big['chars']:>9,} characters")
        print(f"   next request: {int(tokens[1]):,} tokens, limit {int(tokens[2]):,}")
