Claude API Clinic — API

Paste the Anthropic SDK code, get a structured Claude API integration review.

API tokens Open the app

Review your Claude API integration from your own scripts

Send the integration — client construction, messages.create and messages.stream calls, tool definitions and tool loops, batch jobs, token counting and the error handling around them, each file preceded by a # file: bot.py comment — and get back one JSON object: an API posture, the inventory of every client, call, model, parameter, tool, feature, dependency and config with its role, prioritized findings across model drift, deprecated usage, correctness, caching, cost, reliability and hygiene, each with a corrected fragment in the language you pasted, the ordered modernization plan, quick wins, and the focus areas to work through first. Everything this app does goes through the SkillSafe App API — plain JSON over HTTPS — so you can hang a review off any pull request that touches a messages.create call or a model id. Wire it into whatever ships your service: a pre-merge check, a scheduled sweep for retired model ids, or an editor command. Pick a language once and the whole page follows.

Basics

Base URL: https://api.skillsafe.ai/v1/app-api, app slug claude-api-clinic. Every request sends Authorization: Bearer <token> and JSON bodies with Content-Type: application/json. Responses are wrapped in an envelope: {"data": …} on success, {"error": {"code", "message"}} on failure. The review itself is produced by the gpt-terra model. Estimates are free; runs are metered against your credit balance. There is a single run task — one bundle of pasted source files in, one review out, no follow-up calls and no session state to carry. This is the SkillSafe App API that drives this reviewer; it is not Anthropic's Claude API, which is the subject the reviewer reads your code against.

StatusMeaning
401Missing or expired token — create a new session.
402Not enough credits — top up at skillsafe.ai/account/credits.
403The token isn't allowed to do this (e.g. a guest reviewing a very large paste).
404Unknown job or record id.
5xxTransient platform error — retry with backoff.

Browsers enforce CORS for this API, so run these examples from a server, script or terminal — not from another website's frontend.

Step 0 — A tiny client

Every task below is a single HTTP call, so start with a short helper that adds the auth header, sends JSON and unwraps the data envelope. The later steps reuse it.

export API="https://api.skillsafe.ai/v1/app-api"
export TOKEN="YOUR_TOKEN"      # see step 1

# every call looks like:
#   curl -s "$API/..." -H "Authorization: Bearer $TOKEN" [-d '{json}']
# jq is used below to pull fields out of the {"data": ...} envelope
import json, requests

API = "https://api.skillsafe.ai/v1/app-api"
TOKEN = "YOUR_TOKEN"  # see step 1 — read it from your shell environment in real code

def api(method, path, body=None, **headers):
    res = requests.request(method, API + path, json=body,
                           headers={"Authorization": f"Bearer {TOKEN}", **headers})
    payload = res.json()
    if not res.ok:
        raise RuntimeError(payload.get("error", {}).get("message", res.reason))
    return payload["data"]
// Node 18+ (built-in fetch)
const API = "https://api.skillsafe.ai/v1/app-api";
const TOKEN = "YOUR_TOKEN"; // see step 1 — read it from your shell environment in real code

async function api(method, path, body, extraHeaders = {}) {
  const res = await fetch(API + path, {
    method,
    headers: { Authorization: `Bearer ${TOKEN}`, "Content-Type": "application/json", ...extraHeaders },
    body: body === undefined ? undefined : JSON.stringify(body),
  });
  const json = await res.json();
  if (!res.ok) throw new Error(json.error?.message ?? res.statusText);
  return json.data;
}
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
	"os"
)

const API = "https://api.skillsafe.ai/v1/app-api"

var token = os.Getenv("SKILLSAFE_TOKEN") // see step 1

func call(method, path string, body, out any) error {
	var buf bytes.Buffer
	if body != nil {
		json.NewEncoder(&buf).Encode(body)
	}
	req, _ := http.NewRequest(method, API+path, &buf)
	req.Header.Set("Authorization", "Bearer "+token)
	req.Header.Set("Content-Type", "application/json")
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		return err
	}
	defer res.Body.Close()
	var env struct {
		Data  json.RawMessage `json:"data"`
		Error *struct{ Message string `json:"message"` } `json:"error"`
	}
	json.NewDecoder(res.Body).Decode(&env)
	if res.StatusCode >= 400 {
		return fmt.Errorf("api %s %s: %s", method, path, env.Error.Message)
	}
	if out == nil {
		return nil
	}
	return json.Unmarshal(env.Data, out)
}
// Java 17+, no dependencies. Pair with your JSON library (Jackson, Gson…)
// to read fields out of the returned envelope.
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class SkillSafe {
    static final String API = "https://api.skillsafe.ai/v1/app-api";
    static final String TOKEN = System.getenv("SKILLSAFE_TOKEN"); // see step 1
    static final HttpClient HTTP = HttpClient.newHttpClient();

    static String api(String method, String path, String jsonBody) throws Exception {
        var req = HttpRequest.newBuilder(URI.create(API + path))
            .header("Authorization", "Bearer " + TOKEN)
            .header("Content-Type", "application/json")
            .method(method, jsonBody == null
                ? HttpRequest.BodyPublishers.noBody()
                : HttpRequest.BodyPublishers.ofString(jsonBody))
            .build();
        var res = HTTP.send(req, HttpResponse.BodyHandlers.ofString());
        if (res.statusCode() >= 400) throw new RuntimeException(res.body());
        return res.body(); // envelope: {"data": …}
    }
}
require "net/http"
require "json"

API = "https://api.skillsafe.ai/v1/app-api"
TOKEN = ENV.fetch("SKILLSAFE_TOKEN") # see step 1

def api(method, path, body = nil)
  uri = URI(API + path)
  req = Net::HTTP.const_get(method.capitalize).new(uri)
  req["Authorization"] = "Bearer #{TOKEN}"
  req["Content-Type"] = "application/json"
  req.body = body.to_json if body
  res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) { |h| h.request(req) }
  payload = JSON.parse(res.body)
  raise (payload.dig("error", "message") || res.message) unless res.is_a?(Net::HTTPSuccess)
  payload["data"]
end
<?php
const API = "https://api.skillsafe.ai/v1/app-api";
$TOKEN = getenv("SKILLSAFE_TOKEN"); // see step 1

function api(string $method, string $path, ?array $body = null): mixed {
    global $TOKEN;
    $ch = curl_init(API . $path);
    curl_setopt_array($ch, [
        CURLOPT_CUSTOMREQUEST  => $method,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER     => [
            "Authorization: Bearer $TOKEN",
            "Content-Type: application/json",
        ],
        CURLOPT_POSTFIELDS     => $body === null ? null : json_encode($body),
    ]);
    $payload = json_decode(curl_exec($ch), true);
    $status  = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
    curl_close($ch);
    if ($status >= 400) {
        throw new Exception($payload["error"]["message"] ?? "HTTP $status");
    }
    return $payload["data"];
}
// .NET 8+
using System.Net.Http.Json;
using System.Text.Json;

static class SkillSafe
{
    const string Api = "https://api.skillsafe.ai/v1/app-api";
    static readonly HttpClient Http = new();

    static SkillSafe() =>
        Http.DefaultRequestHeaders.Authorization =
            new("Bearer", Environment.GetEnvironmentVariable("SKILLSAFE_TOKEN")); // see step 1

    public static async Task<JsonElement> ApiAsync(HttpMethod method, string path, object? body = null)
    {
        var req = new HttpRequestMessage(method, Api + path);
        if (body != null) req.Content = JsonContent.Create(body);
        var res = await Http.SendAsync(req);
        var json = await res.Content.ReadFromJsonAsync<JsonElement>();
        if (!res.IsSuccessStatusCode)
            throw new Exception(json.GetProperty("error").GetProperty("message").GetString());
        return json.GetProperty("data");
    }
}

Step 1 — Get a token

POST /guest

A guest token lets you check balances and estimate costs for free. For metered review runs billed to your own account, use your personal token: open the token page, sign in with SkillSafe, and press Copy shell export — it puts export SKILLSAFE_TOKEN="…" on your clipboard, which every example below reads. Treat the token like a password: it can spend your credits. For fully headless scripts, POST /guest mints a guest token with no browser involved.

curl -s -X POST "$API/guest" \
  -H "Content-Type: application/json" \
  -d '{"slug":"claude-api-clinic"}' | jq -r '.data.token'
token = api("POST", "/guest", {"slug": "claude-api-clinic"})["token"]
const { token } = await api("POST", "/guest", { slug: "claude-api-clinic" });
var guest struct{ Token string `json:"token"` }
err := call("POST", "/guest", map[string]string{"slug": "claude-api-clinic"}, &guest)
String envelope = api("POST", "/guest", """
    {"slug":"claude-api-clinic"}""");
// token is at data.token in the returned JSON
token = api("POST", "/guest", { slug: "claude-api-clinic" })["token"]
$token = api("POST", "/guest", ["slug" => "claude-api-clinic"])["token"];
var guest = await SkillSafe.ApiAsync(HttpMethod.Post, "/guest",
    new { slug = "claude-api-clinic" });
var token = guest.GetProperty("token").GetString();

The app stores this browser's token under the localStorage key skillsafe_app_token:claude-api-clinic, on the app's own origin. The token page reads and manages it for you — you never need to open developer tools.

Step 2 — Check who you are and your balance

GET /me

Returns subject_type ("user" or "guest"), subject_id and your credits balance. Check this before reviewing a large paste.

curl -s "$API/me" -H "Authorization: Bearer $TOKEN" | jq '.data'
me = api("GET", "/me")
print(me["subject_type"], me["credits"])
const me = await api("GET", "/me");
console.log(me.subject_type, me.credits);
var me struct {
	SubjectType string `json:"subject_type"`
	Credits     int64  `json:"credits"`
}
err := call("GET", "/me", nil, &me)
String envelope = api("GET", "/me", null);
// data.subject_type, data.credits
me = api("GET", "/me")
puts "#{me["subject_type"]}: #{me["credits"]} credits"
$me = api("GET", "/me");
echo "{$me['subject_type']}: {$me['credits']} credits\n";
var me = await SkillSafe.ApiAsync(HttpMethod.Get, "/me");
Console.WriteLine($"{me.GetProperty("subject_type")}: {me.GetProperty("credits")} credits");

Step 3 — Estimate the cost

POST /estimate

Send exactly the input you would send to /run; the response's hold_credits is the worst-case cost. Nothing is charged and no job is created, so estimating is free — useful when you are piping every module that touches the Anthropic SDK across a service and want a ceiling before spending credits.

Input fieldTypeNotes
sourcesstring, requiredThe pasted Claude API / Anthropic SDK integration: client setup, messages.create / messages.stream calls, tool definitions and tool loops, batch jobs, token counting and the configuration around them, in Python, TypeScript/JavaScript, Java/Kotlin, Go, Ruby, C#, PHP or raw HTTP. One file or several concatenated, each preceded by a # file: bot.py (or // file: bot.ts) comment. This is the model's only evidence — nothing is installed, run or called out to. Inputs longer than 100,000 characters are clipped middle-out, with a // [... clipped ...] comment showing where. At least 60 characters are needed for a review.
sdk_langstringpython | typescript | java | go | ruby | csharp | php | curl | unknown — changes what is idiomatic: TypeScript timeouts are milliseconds where Python's are seconds, Go reaches the tool runner through its own package, and the corrected snippets come back in the language you name.
concernstringgeneral | migration | caching | cost | reliability | tools — the review emphasis. It weights the findings and the summary, but it is emphasis and not exclusivity: a high-severity finding from another category is never suppressed.
contextstring, optionalExtra context: what the integration does, traffic volume, what the monthly bill looks like, measured latencies, which model tier you want to land on, what has already been tried, and any constraint you cannot move (a pinned SDK version, a latency budget, a compliance rule). Clipped at 20,000 characters.
prescan_factsobject, optionalWhat a client-side scanner mechanically matched in the code: {"resources": [], "flags": []}. Each entry is {id, label}. Resource ids look like res:model/claude-opus-5, res:sdk/python-anthropic, res:call/messages-create, res:tool/web_search_20260209, res:beta/fast-mode-2026-02-01 or res:file/bot.py; flag ids are <check>:<name>, one per check — retired-model:claude-3-5-sonnet-20241022, dated-model-id:claude-sonnet-4-6-20251114, budget-tokens:bot.py, deprecated-output-format:bot.py, sampling-param:temperature, assistant-prefill:bot.py, no-caching:all, tiktoken:bot.py, old-web-tool:web_search_20250305, fast-mode:on-4-7, thinking-disabled:bot.py, big-max-no-stream:100000, hardcoded-key:9, broad-catch:bot.py-33, legacy-tool-result:57, provider-mix:openai. Every flag id you send comes back in coverage_check. The web UI fills this from its own prescan; API callers may omit the field entirely (then coverage_check simply comes back empty) or send the two empty arrays.
retry_notestring, optionalOnly set by the app's automatic reformat retry when a first reply was not valid JSON. Leave it out.
cat > sources.txt <<'FILES'
# file: bot.py
import anthropic

client = anthropic.Anthropic()
resp = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=100000,
    temperature=0.7,
    thinking={"type": "enabled", "budget_tokens": 8000},
    system=SYSTEM_PROMPT,
    messages=[{"role": "user", "content": thread}],
)
FILES

jq -n --rawfile sources sources.txt \
  '{sources: $sources,
    sdk_lang: "python",
    concern: "migration",
    context: "Support bot, ~2,000 threads a day; we want to land on claude-sonnet-5.",
    prescan_facts: {resources: [], flags: []}}' > input.json

curl -s -X POST "$API/estimate" \
  -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -d @input.json | jq '.data.hold_credits'
SOURCES = r"""# file: bot.py
import anthropic

client = anthropic.Anthropic()
resp = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=100000,
    temperature=0.7,
    thinking={"type": "enabled", "budget_tokens": 8000},
    system=SYSTEM_PROMPT,
    messages=[{"role": "user", "content": thread}],
)
"""

payload = {
    "sources": SOURCES,
    "sdk_lang": "python",
    "concern": "migration",
    "context": "Support bot, ~2,000 threads a day; we want to land on claude-sonnet-5.",
    "prescan_facts": {"resources": [], "flags": []},
}

est = api("POST", "/estimate", payload)
print("worst case:", est.get("hold_credits", est.get("credits")), "credits")
// String.raw keeps the pasted code exactly as written, escapes and all
const sources = String.raw`# file: bot.py
import anthropic

client = anthropic.Anthropic()
resp = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=100000,
    temperature=0.7,
    thinking={"type": "enabled", "budget_tokens": 8000},
    system=SYSTEM_PROMPT,
    messages=[{"role": "user", "content": thread}],
)
`;

const payload = {
  sources,
  sdk_lang: "python",
  concern: "migration",
  context: "Support bot, ~2,000 threads a day; we want to land on claude-sonnet-5.",
  prescan_facts: { resources: [], flags: [] },
};

const est = await api("POST", "/estimate", payload);
console.log("worst case:", est.hold_credits ?? est.credits, "credits");
const sources = `# file: bot.py
import anthropic

client = anthropic.Anthropic()
resp = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=100000,
    temperature=0.7,
    thinking={"type": "enabled", "budget_tokens": 8000},
    system=SYSTEM_PROMPT,
    messages=[{"role": "user", "content": thread}],
)
`

payload := map[string]any{
	"sources":  sources,
	"sdk_lang": "python",
	"concern":  "migration",
	"context":  "Support bot, ~2,000 threads a day; we want to land on claude-sonnet-5.",
	"prescan_facts": map[string]any{
		"resources": []any{}, "flags": []any{},
	},
}

var est struct{ HoldCredits int64 `json:"hold_credits"` }
err := call("POST", "/estimate", payload, &est)
// A text block keeps the pasted code readable; the common indent is stripped.
String sources = """
    # file: bot.py
    import anthropic

    client = anthropic.Anthropic()
    resp = client.messages.create(
        model="claude-3-5-sonnet-20241022",
        max_tokens=100000,
        temperature=0.7,
        thinking={"type": "enabled", "budget_tokens": 8000},
        system=SYSTEM_PROMPT,
        messages=[{"role": "user", "content": thread}],
    )
    """;

String jsonPayload = """
    {"sources": %s,
     "sdk_lang": "python",
     "concern": "migration",
     "context": "Support bot, ~2,000 threads a day; we want to land on claude-sonnet-5.",
     "prescan_facts": {"resources": [], "flags": []}}
    """.formatted(toJsonString(sources));

String envelope = api("POST", "/estimate", jsonPayload);
// worst-case cost is at data.hold_credits
# the single-quoted heredoc keeps the pasted code literal - no interpolation
SOURCES = <<~'FILES'
  # file: bot.py
  import anthropic

  client = anthropic.Anthropic()
  resp = client.messages.create(
      model="claude-3-5-sonnet-20241022",
      max_tokens=100000,
      temperature=0.7,
      thinking={"type": "enabled", "budget_tokens": 8000},
      system=SYSTEM_PROMPT,
      messages=[{"role": "user", "content": thread}],
  )
FILES

payload = { sources: SOURCES,
            sdk_lang: "python",
            concern: "migration",
            context: "Support bot, ~2,000 threads a day; we want to land on claude-sonnet-5.",
            prescan_facts: { resources: [], flags: [] } }

est = api("POST", "/estimate", payload)
puts "worst case: #{est["hold_credits"] || est["credits"]} credits"
$sources = <<<'FILES'
# file: bot.py
import anthropic

client = anthropic.Anthropic()
resp = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=100000,
    temperature=0.7,
    thinking={"type": "enabled", "budget_tokens": 8000},
    system=SYSTEM_PROMPT,
    messages=[{"role": "user", "content": thread}],
)
FILES;

$payload = [
    "sources"       => $sources,
    "sdk_lang"      => "python",
    "concern"       => "migration",
    "context"       => "Support bot, ~2,000 threads a day; we want to land on claude-sonnet-5.",
    "prescan_facts" => ["resources" => [], "flags" => []],
];

$est = api("POST", "/estimate", $payload);
echo "worst case: " . ($est["hold_credits"] ?? $est["credits"]) . " credits\n";
// a raw string literal keeps the pasted code exactly as written, quotes and all
var sources = """
    # file: bot.py
    import anthropic

    client = anthropic.Anthropic()
    resp = client.messages.create(
        model="claude-3-5-sonnet-20241022",
        max_tokens=100000,
        temperature=0.7,
        thinking={"type": "enabled", "budget_tokens": 8000},
        system=SYSTEM_PROMPT,
        messages=[{"role": "user", "content": thread}],
    )
    """;

var payload = new {
    sources,
    sdk_lang = "python",
    concern = "migration",
    context = "Support bot, ~2,000 threads a day; we want to land on claude-sonnet-5.",
    prescan_facts = new {
        resources = Array.Empty<object>(), flags = Array.Empty<object>(),
    },
};

var est = await SkillSafe.ApiAsync(HttpMethod.Post, "/estimate", payload);
Console.WriteLine($"worst case: {est.GetProperty("hold_credits")} credits");

prescan_facts.flags is how you make the review answer for things you already know about. Send {"resources": [{"id": "res:model/claude-3-5-sonnet-20241022", "label": "Model/claude-3-5-sonnet-20241022"}], "flags": [{"id": "budget-tokens:bot.py", "label": "a fixed thinking budget in bot.py"}]} and every flag id comes back in coverage_check — addressed by a finding, or set aside with the reason. Nothing you flag is silently dropped, which makes it the field to assert on in a CI check. Omit the field and coverage_check comes back empty; the rest of the review is unaffected.

Step 4 — Run the review and wait for the result

POST /run
GET /jobs/{job_id}

/run takes the same input as /estimate, places a credit hold and returns a job_id. Poll /jobs/{job_id} every 1–2 seconds until status is succeeded or failed (a run typically takes 30–90 s, since every finding carries a corrected code fragment and the plan is ordered afterwards). Always send an Idempotency-Key header so a network retry can't start a second, double-charged run. The review is in output — usually nested as output.output, and as a JSON string, so parse defensively. The samples below print the posture, the inventory, the prioritized findings and the ordered plan, then save the whole object to review.json.

JOB_ID=$(curl -s -X POST "$API/run" \
  -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -H "Idempotency-Key: cac-$(date +%s)" \
  -d @input.json | jq -r '.data.job_id')

while :; do
  JOB=$(curl -s "$API/jobs/$JOB_ID" -H "Authorization: Bearer $TOKEN")
  STATUS=$(echo "$JOB" | jq -r '.data.status')
  [ "$STATUS" = "succeeded" ] || [ "$STATUS" = "failed" ] && break
  sleep 2
done

# unwrap the review once, then read it
echo "$JOB" | jq -r '.data.output.output' > review.json

jq -r '
  "\(.review_name) [\(.posture)]: \(.verdict)",
  "",
  "INVENTORY",
  (.inventory[] | "  \(.kind)/\(.name) in \(.scope) - \(.role)"),
  "",
  "FINDINGS",
  (.findings[] | "  [\(.priority)] \(.id) \(.category) \(.resource): \(.problem)"),
  "",
  "PLAN",
  (.migration_steps[] | "  \(.step) - \(.action) [\(.command)]"),
  "",
  "QUICK WINS",
  (.quick_wins[] | "  - \(.)"),
  "",
  "FOCUS AREAS",
  (.focus_areas[] | "  \(.area) - \(.why)"),
  "",
  "COVERAGE",
  (.coverage_check[] | "  \(.id): \(if .addressed then "ok" else "SET ASIDE" end) - \(.note)")' \
  review.json

# fail the pipeline on anything critical
jq -e '[.findings[] | select(.priority == "critical")] | length == 0' review.json > /dev/null \
  || { echo "critical findings present"; exit 1; }
import time

job_id = api("POST", "/run", payload,
             **{"Idempotency-Key": "cac-001"})["job_id"]

while True:
    job = api("GET", f"/jobs/{job_id}")
    if job["status"] in ("succeeded", "failed"):
        break
    time.sleep(1.5)

if job["status"] == "failed":
    raise RuntimeError(job.get("error", "run failed"))

raw = job["output"]
if isinstance(raw, dict) and "output" in raw:
    raw = raw["output"]
review = json.loads(raw) if isinstance(raw, str) else raw

print(f'{review["review_name"]} [{review["posture"]}]: {review["verdict"]}')
for r in review["inventory"]:
    print(f'  {r["kind"]}/{r["name"]:<24} in={r["scope"] or "-":<20} {r["role"]}')
for f in review["findings"]:
    print(f'  [{f["priority"]:>8}] {f["id"]} {f["category"]} {f["resource"]}')
    print(f'      L:{f["likelihood"]}/S:{f["severity"]} {f["problem"]}')
    print(f'      fix: {f["fix"]}')
    if f["snippet"]:
        print("      snippet:", f["snippet"].splitlines()[0], "...")
for i, s in enumerate(review["migration_steps"], 1):
    print(f'  {i}. {s["step"]} - {s["action"]}')
    if s["command"]:
        print(f'     $ {s["command"]}')
for w in review["quick_wins"]:
    print("  win:", w)
for a in review["focus_areas"]:
    print(f'  focus {a["area"]} {a["finding_ids"]} - {a["why"]}')
for c in review["coverage_check"]:
    print(f'  {c["id"]}: {"ok" if c["addressed"] else "SET ASIDE"} - {c["note"]}')

with open("review.json", "w", encoding="utf-8") as fh:
    json.dump(review, fh, indent=2)

critical = [f for f in review["findings"] if f["priority"] == "critical"]
if critical:
    raise SystemExit(f"{len(critical)} critical finding(s)")
import { writeFileSync } from "node:fs";

const { job_id } = await api("POST", "/run", payload,
  { "Idempotency-Key": crypto.randomUUID() });

let job;
do {
  await new Promise((r) => setTimeout(r, 1500));
  job = await api("GET", `/jobs/${job_id}`);
} while (job.status !== "succeeded" && job.status !== "failed");

if (job.status === "failed") throw new Error(job.error ?? "run failed");

const raw = job.output?.output ?? job.output;
const review = typeof raw === "string" ? JSON.parse(raw) : raw;

console.log(`${review.review_name} [${review.posture}]: ${review.verdict}`);
for (const r of review.inventory) {
  console.log(`  ${r.kind}/${r.name} (${r.scope || "-"}): ${r.role}`);
}
for (const f of review.findings) {
  console.log(`  [${f.priority}] ${f.id} ${f.category} ${f.resource}`);
  console.log(`      L:${f.likelihood}/S:${f.severity} - ${f.fix}`);
}
review.migration_steps.forEach((s, i) => {
  console.log(`  ${i + 1}. ${s.step} - ${s.action}${s.command ? `\n     $ ${s.command}` : ""}`);
});
for (const w of review.quick_wins) console.log(`  win: ${w}`);
for (const a of review.focus_areas) {
  console.log(`  focus ${a.area} (${a.finding_ids.join(", ")}): ${a.why}`);
}
for (const c of review.coverage_check) {
  console.log(`  ${c.id}: ${c.addressed ? "ok" : "SET ASIDE"} - ${c.note}`);
}

writeFileSync("review.json", JSON.stringify(review, null, 2));

const critical = review.findings.filter((f) => f.priority === "critical");
if (critical.length) process.exitCode = 1;
var started struct{ JobID string `json:"job_id"` }
if err := call("POST", "/run", payload, &started); err != nil {
	log.Fatal(err)
}

var job struct {
	Status string          `json:"status"`
	Error  string          `json:"error"`
	Output json.RawMessage `json:"output"`
}
for {
	if err := call("GET", "/jobs/"+started.JobID, nil, &job); err != nil {
		log.Fatal(err)
	}
	if job.Status == "succeeded" || job.Status == "failed" {
		break
	}
	time.Sleep(1500 * time.Millisecond)
}

// job.Output is {"output": "<json string>"} — unwrap, then unmarshal:
type Review struct {
	ReviewName  string `json:"review_name"`
	Posture     string `json:"posture"`
	Verdict     string `json:"verdict"`
	ExecSummary string `json:"exec_summary"`
	Assumptions   []string `json:"assumptions"`
	OpenQuestions []string `json:"open_questions"`
	Inventory []struct {
		Kind, Name, Scope, Role string
	} `json:"inventory"`
	Findings []struct {
		ID, Category, Severity, Likelihood, Priority string
		Resource, Problem, Impact, Fix, Snippet      string
	} `json:"findings"`
	MigrationSteps []struct {
		Step, Action, Why, Command string
	} `json:"migration_steps"`
	CoverageCheck []struct {
		ID, Note  string
		Addressed bool
	} `json:"coverage_check"`
	QuickWins  []string `json:"quick_wins"`
	FocusAreas []struct {
		Area, Why  string
		FindingIDs []string `json:"finding_ids"`
	} `json:"focus_areas"`
	Summary string `json:"summary"`
}
var wrapper struct{ Output string `json:"output"` }
json.Unmarshal(job.Output, &wrapper)
var review Review
json.Unmarshal([]byte(wrapper.Output), &review)

fmt.Printf("%s [%s]: %s\n", review.ReviewName, review.Posture, review.Verdict)
for _, r := range review.Inventory {
	fmt.Printf("  %s/%s (%s): %s\n", r.Kind, r.Name, r.Scope, r.Role)
}
for _, f := range review.Findings {
	fmt.Printf("  [%s] %s %s %s: %s\n", f.Priority, f.ID, f.Category, f.Resource, f.Problem)
}
for i, s := range review.MigrationSteps {
	fmt.Printf("  %d. %s - %s [%s]\n", i+1, s.Step, s.Action, s.Command)
}
for _, a := range review.FocusAreas {
	fmt.Printf("  focus %s %v: %s\n", a.Area, a.FindingIDs, a.Why)
}
os.WriteFile("review.json", []byte(wrapper.Output), 0o644)
String envelope = api("POST", "/run", jsonPayload);
String jobId = /* data.job_id via your JSON library */;

while (true) {
    String job = api("GET", "/jobs/" + jobId, null);
    String status = /* data.status */;
    if (status.equals("succeeded") || status.equals("failed")) break;
    Thread.sleep(1500);
}
// The review is at data.output.output as a JSON string — parse it again, then read
// review_name, posture, verdict, exec_summary, assumptions[], open_questions[],
// inventory[] (kind/name/scope/role),
// findings[] (id/category/severity/likelihood/priority/resource/problem/impact/fix/snippet),
// migration_steps[] (step/action/why/command), coverage_check[] (id/addressed/note),
// quick_wins[], focus_areas[] (area/why/finding_ids[]) and summary.
// Finally keep the review on disk:
//   Files.writeString(Path.of("review.json"), reviewJson);
started = api("POST", "/run", payload)

job = nil
loop do
  job = api("GET", "/jobs/#{started["job_id"]}")
  break if %w[succeeded failed].include?(job["status"])
  sleep 1.5
end
raise (job["error"] || "run failed") if job["status"] == "failed"

raw = job["output"].is_a?(Hash) ? job["output"].fetch("output", job["output"]) : job["output"]
review = raw.is_a?(String) ? JSON.parse(raw) : raw

puts "#{review["review_name"]} [#{review["posture"]}]: #{review["verdict"]}"
review["inventory"].each { |r| puts "  #{r["kind"]}/#{r["name"]} (#{r["scope"]}): #{r["role"]}" }
review["findings"].each do |f|
  puts "  [#{f["priority"]}] #{f["id"]} #{f["category"]} #{f["resource"]}"
  puts "      L:#{f["likelihood"]}/S:#{f["severity"]} - #{f["fix"]}"
end
review["migration_steps"].each_with_index do |s, i|
  puts "  #{i + 1}. #{s["step"]} - #{s["action"]}"
  puts "     $ #{s["command"]}" unless s["command"].to_s.empty?
end
review["quick_wins"].each { |w| puts "  win: #{w}" }
review["focus_areas"].each { |a| puts "  focus #{a["area"]} #{a["finding_ids"].join(", ")}" }
review["coverage_check"].each { |c| puts "  #{c["id"]}: #{c["addressed"] ? "ok" : "SET ASIDE"}" }

File.write("review.json", JSON.pretty_generate(review))
exit 1 if review["findings"].any? { |f| f["priority"] == "critical" }
$started = api("POST", "/run", $payload);

do {
    sleep(2);
    $job = api("GET", "/jobs/" . $started["job_id"]);
} while (!in_array($job["status"], ["succeeded", "failed"]));

if ($job["status"] === "failed") {
    throw new Exception($job["error"] ?? "run failed");
}

$raw = is_array($job["output"]) ? ($job["output"]["output"] ?? $job["output"]) : $job["output"];
$review = is_string($raw) ? json_decode($raw, true) : $raw;

echo "{$review['review_name']} [{$review['posture']}]: {$review['verdict']}\n";
foreach ($review["inventory"] as $r) {
    echo "  {$r['kind']}/{$r['name']} ({$r['scope']}): {$r['role']}\n";
}
foreach ($review["findings"] as $f) {
    echo "  [{$f['priority']}] {$f['id']} {$f['category']} {$f['resource']}\n";
    echo "      L:{$f['likelihood']}/S:{$f['severity']} - {$f['fix']}\n";
}
foreach ($review["migration_steps"] as $i => $s) {
    echo "  " . ($i + 1) . ". {$s['step']} - {$s['action']}\n";
    if ($s["command"] !== "") { echo "     $ {$s['command']}\n"; }
}
foreach ($review["quick_wins"] as $w) {
    echo "  win: $w\n";
}
foreach ($review["focus_areas"] as $a) {
    echo "  focus {$a['area']}: " . implode(", ", $a["finding_ids"]) . "\n";
}
foreach ($review["coverage_check"] as $c) {
    echo "  {$c['id']}: " . ($c["addressed"] ? "ok" : "SET ASIDE") . "\n";
}

file_put_contents("review.json", json_encode($review, JSON_PRETTY_PRINT));
var started = await SkillSafe.ApiAsync(HttpMethod.Post, "/run", payload);
var jobId = started.GetProperty("job_id").GetString();

JsonElement job;
while (true)
{
    job = await SkillSafe.ApiAsync(HttpMethod.Get, $"/jobs/{jobId}");
    var status = job.GetProperty("status").GetString();
    if (status is "succeeded" or "failed") break;
    await Task.Delay(1500);
}

var rawText = job.GetProperty("output").GetProperty("output").GetString();
using var doc = JsonDocument.Parse(rawText!);
var review = doc.RootElement;

Console.WriteLine($"{review.GetProperty("review_name")} " +
                  $"[{review.GetProperty("posture")}]: {review.GetProperty("verdict")}");
foreach (var r in review.GetProperty("inventory").EnumerateArray())
{
    Console.WriteLine($"  {r.GetProperty("kind")}/{r.GetProperty("name")}: {r.GetProperty("role")}");
}
foreach (var f in review.GetProperty("findings").EnumerateArray())
{
    Console.WriteLine($"  [{f.GetProperty("priority")}] {f.GetProperty("id")} " +
                      $"{f.GetProperty("category")} {f.GetProperty("resource")} " +
                      $"(L:{f.GetProperty("likelihood")}/S:{f.GetProperty("severity")})");
}
foreach (var s in review.GetProperty("migration_steps").EnumerateArray())
{
    Console.WriteLine($"  {s.GetProperty("step")}: {s.GetProperty("action")} " +
                      $"[{s.GetProperty("command")}]");
}
foreach (var a in review.GetProperty("focus_areas").EnumerateArray())
{
    Console.WriteLine($"  focus {a.GetProperty("area")}: {a.GetProperty("why")}");
}

await File.WriteAllTextAsync("review.json", rawText!);

The model is asked for one JSON object and nothing else, but a stray code fence or preamble is always possible. Strip a leading ```json fence, take the text between the first { and the last }, and only then parse — that is what the app does before it falls back to a retry_note reformat run.

The review object — output schema

One JSON object, always the same shape. Every array is present, and the review is grounded in the pasted code alone: findings cite only files, calls, models, parameters, tools and dependencies that actually appear in sources, and something that is simply absent (no error handling, no cache_control on a large stable system prompt, no streaming on a 128K request) is reported against the closest real construct or against (missing from the pasted code). Where the code is silent on something that changes the verdict you get an entry in assumptions and, if it would change the ranking, in open_questions. Expect five to fifteen findings on a typical integration — a well-maintained one may honestly yield two or three, and findings is never empty.

FieldTypeMeaning
review_namestringA short title naming the integration, taken from the code's own naming — e.g. support-bot — Claude API review.
posturestringapi-current | update-recommended | breakage-risk. See the table below.
verdictstringOne sentence justifying the posture and naming the single most important change.
exec_summarystringTwo or three paragraphs, separated by blank lines, on the dominant themes across the integration.
assumptionsstring[]Explicit assumptions filling gaps the code left open. Read these first — a wrong assumption invalidates the findings built on it.
open_questionsstring[]Questions whose answers would change the ranking.
inventoryarray{kind, name, scope, role} — every Client, Call, Model, Param, Tool, Feature, Dependency and Config the review parsed out of the code and the part it plays. scope is the file or call site it appears in.
findingsarrayThe prioritized findings table — ids AC-001, AC-002, … in sequence, at least one entry. Columns are listed below.
migration_stepsarray{step, action, why, command} — the ordered modernization plan from where the integration is to where the review says it should be, first step first, each independently deployable. Two to eight steps. For an api-current integration these are the next improvements, not migration chores.
coverage_checkarray{id, addressed, note} — one entry per prescan_facts.flags id you sent, each appearing exactly once. See the semantics below.
quick_winsstring[]One-line changes worth doing immediately, ahead of any planning. May be empty when nothing here is a one-liner.
focus_areasarray{area, why, finding_ids} — what to work through first, one sentence tied to the review, and the finding ids that motivate it. Every id in finding_ids exists in findings.
summarystringClosing paragraph: what to fix first, and what risk remains after that.

The three posture values:

postureWhat it means
api-currentCurrent model ids, no removed parameters, caching and streaming where they belong. Findings still exist, but they are improvements — placing a cache_control breakpoint better, tuning output_config.effort, moving offline work to the Batches API, adding server-side fallbacks — not breakage. Genuinely well-maintained integrations land here rather than having severity manufactured for them.
update-recommendedNothing returns an error today, but named costs should be addressed: a stale model tier, a large stable system prompt re-sent with no cache_control, non-latency-sensitive work paying full price instead of running on batches, weak error handling that retries what retrying cannot fix.
breakage-riskSomething in the code returns an error on the model in use or on the stated migration target — a retired model id, budget_tokens or a sampling param on a model that removed it, an assistant prefill turn, speed: "fast" on 4.7 — or a credential is exposed in source. This is the posture to gate a pipeline on.

Each entry in findings:

ColumnMeaning
idSequential AC-001, AC-002, … — the stable handle referenced from focus_areas[].finding_ids.
categorymodel-drift | deprecated-usage | correctness | caching | cost | reliability | hygiene. Weighted by the concern you sent, but never restricted to it. The categories keep the three kinds of problem apart on purpose: what will break (a 400 on the model in use), what costs money (missing caching, the wrong tier, no batches) and what mis-handles an edge (an unguarded refusal, a broad catch, string-matched tool input).
severitylow | medium | high — how bad it is when it bites.
likelihoodlow | medium | high — how likely it is to bite.
prioritycritical | high | medium | low — severity by likelihood. critical is reserved for something that returns an error on the model in use, silently corrupts results (a dropped tool_result, string-matched tool input) or exposes a credential, so sort on this field and work top-down. This is also a field to gate a pipeline on.
resourceThe Model/claude-3-5-sonnet-20241022, Call/messages.create (bot.py), Param/budget_tokens, Tool/web_search_20250305 or file this is about — always something that appears in sources, or the literal (missing from the pasted code) when the finding is about something absent.
problemWhat is wrong, in this code specifically.
impactWhat it costs — a 400 on the next deploy, dollars per million tokens, a dropped tool result, a leaked key.
fixThe concrete change to make — not "modernize the call".
snippetA corrected fragment in the language of the pasted code you can paste: the fixed block, correctly indented, matching the file it belongs to, not the whole file. Empty string when a snippet would add nothing. Secret values are never echoed — a placeholder appears instead.

Each entry in migration_steps:

ColumnMeaning
stepThe short name of the step, e.g. Move to a current model id.
actionWhat to actually do, in this integration's files.
whyOne sentence tying the step to the findings.
commandA real shell command when one exists (pip install -U anthropic, npm i @anthropic-ai/sdk@latest), and the empty string when the step is a code edit. This is what the app's "plan as a shell script" export is built from — code-edit steps become comments.

coverage_check semantics:

CaseWhat you get
Every flag id you sentEach prescan_facts.flags id appears in coverage_check exactly once. Nothing you flagged is silently dropped, which makes this the field to assert on in a CI check. Ids in prescan_facts.resources are not reconciled here — they shape the inventory instead.
addressed: trueThe flag is covered by the review; note names the finding id that covers it.
addressed: falseThe flag was deliberately set aside; note gives the reason — a check that fired but is not a real problem for this integration (a temperature on the one path that legitimately still runs on the 4.6 pair, an assistant message that is echoed history rather than a prefill).
Nothing sentOmit prescan_facts, or send the two empty arrays, and coverage_check comes back empty. The rest of the review is unaffected.

A small, realistic result for the input above, trimmed for length:

{
  "review_name": "support-bot — Claude API review",
  "posture": "breakage-risk",
  "verdict": "The one call site names a retired model and carries two parameters the stated target
              rejects; land on claude-sonnet-5 with adaptive thinking and the rest is tuning.",
  "exec_summary": "bot.py calls claude-3-5-sonnet-20241022, a retired model line, so the request
                   shape around it was written for behaviour current models no longer have. The
                   stated target is claude-sonnet-5, and two of the parameters on this call are
                   removed there: temperature and the fixed thinking budget.

                   The rest is cost and plumbing. A ~6k-token system prompt is re-sent on every
                   request with no cache_control breakpoint, and max_tokens is 100000 on a
                   non-streaming call, which the SDKs cannot hold inside one request-response.",
  "assumptions": [
    "SYSTEM_PROMPT is the stable policy text it looks like, not per-request content.",
    "No wrapper module outside the pasted file adds retries or a different model id."
  ],
  "open_questions": [
    "Do replies actually approach 100000 tokens, or was the cap copied in as a ceiling?",
    "Is any traffic latency-sensitive, or could the bulk of it run on the Batches API?"
  ],
  "inventory": [
    { "kind": "Model", "name": "claude-3-5-sonnet-20241022", "scope": "bot.py",
      "role": "Answers support threads; a retired, date-suffixed id." },
    { "kind": "Call", "name": "messages.create", "scope": "bot.py",
      "role": "The single non-streaming request path." },
    { "kind": "Param", "name": "thinking.budget_tokens", "scope": "bot.py",
      "role": "Fixed thinking budget, copied from a 2024-era example." },
    { "kind": "Client", "name": "anthropic.Anthropic", "scope": "bot.py",
      "role": "Default client; credentials resolved from the environment." }
  ],
  "findings": [
    { "id": "AC-001", "category": "model-drift",
      "severity": "high", "likelihood": "high", "priority": "critical",
      "resource": "Model/claude-3-5-sonnet-20241022 (bot.py)",
      "problem": "The model id names the retired claude-3 line and carries a date suffix; current
                 ids are complete as-is.",
      "impact": "Requests naming it fail outright once the line is fully withdrawn — and every
                 other choice on this call was tuned for a model that no longer behaves that way.",
      "fix": "Move to claude-sonnet-5, the stated target, and re-read the request shape around it.",
      "snippet": "MODEL = \"claude-sonnet-5\"" },
    { "id": "AC-002", "category": "deprecated-usage",
      "severity": "high", "likelihood": "high", "priority": "critical",
      "resource": "Param/budget_tokens (bot.py)",
      "problem": "thinking is configured as {\"type\": \"enabled\", \"budget_tokens\": 8000}, and
                 budget_tokens is rejected with a 400 on Sonnet 5.",
      "impact": "The very deploy that fixes the model id starts 400ing on every request unless
                 this changes in the same commit.",
      "fix": "Send adaptive thinking; tune depth with output_config.effort if it is a lever here.",
      "snippet": "resp = client.messages.create(\n    model=MODEL,\n    max_tokens=16000,\n    thinking={\"type\": \"adaptive\"},\n    messages=messages,\n)" },
    { "id": "AC-003", "category": "deprecated-usage",
      "severity": "medium", "likelihood": "high", "priority": "high",
      "resource": "Param/temperature (bot.py)",
      "problem": "temperature=0.7 is sent alongside a migration target that removed sampling
                 params.",
      "impact": "A second 400 on the same request, from the same deploy as AC-002.",
      "fix": "Drop temperature; if reply variety is the goal, ask for it in the system prompt.",
      "snippet": "" },
    { "id": "AC-004", "category": "caching",
      "severity": "medium", "likelihood": "high", "priority": "high",
      "resource": "Param/system (bot.py)",
      "problem": "A large stable system prompt is re-sent on every call and no cache_control
                 breakpoint appears anywhere in the pasted code.",
      "impact": "Full input price on the same ~6k tokens for every one of the ~2,000 daily threads
                 — the single biggest unused cost lever here.",
      "fix": "Put an ephemeral cache_control breakpoint on the system block and watch
              usage.cache_read_input_tokens to confirm it hits.",
      "snippet": "system=[{\n    \"type\": \"text\",\n    \"text\": SYSTEM_PROMPT,\n    \"cache_control\": {\"type\": \"ephemeral\"},\n}]" },
    { "id": "AC-005", "category": "reliability",
      "severity": "medium", "likelihood": "medium", "priority": "medium",
      "resource": "Param/max_tokens (bot.py)",
      "problem": "max_tokens is 100000 on a non-streaming call.",
      "impact": "Output caps this large sit inside one HTTP request-response and hit the SDK
                 timeout; the practical ceiling is ~16K non-streaming, ~64K streaming.",
      "fix": "Either lower the cap to what replies actually need, or switch to .stream() and read
              the result with get_final_message().",
      "snippet": "with client.messages.stream(\n    model=MODEL,\n    max_tokens=64000,\n    messages=messages,\n) as stream:\n    resp = stream.get_final_message()" }
  ],
  "migration_steps": [
    { "step": "Move to a current model id",
      "action": "Set MODEL to claude-sonnet-5 in bot.py.",
      "why": "The retired line is the root of every other finding on this call.",
      "command": "pip install -U anthropic" },
    { "step": "Fix the request shape in the same commit",
      "action": "Replace the budget_tokens config with {\"type\": \"adaptive\"} and delete
                 temperature, as in AC-002 and AC-003.",
      "why": "Both parameters are 400s on the new model, so they cannot ship a deploy later.",
      "command": "" },
    { "step": "Cache the system prompt",
      "action": "Send system as a block list with an ephemeral cache_control breakpoint and log
                 usage.cache_read_input_tokens for a day.",
      "why": "It is the same ~6k tokens on every request at full price today.",
      "command": "" },
    { "step": "Right-size the output cap",
      "action": "Lower max_tokens to what replies need, or move the call to .stream().",
      "why": "100000 on a non-streaming call cannot complete inside one request-response.",
      "command": "" }
  ],
  "coverage_check": [
    { "id": "retired-model:claude-3-5-sonnet-20241022", "addressed": true, "note": "AC-001." },
    { "id": "budget-tokens:bot.py", "addressed": true, "note": "AC-002." },
    { "id": "sampling-param:temperature", "addressed": true, "note": "AC-003." },
    { "id": "no-caching:all", "addressed": true, "note": "AC-004." },
    { "id": "big-max-no-stream:100000", "addressed": true, "note": "AC-005." }
  ],
  "quick_wins": [
    "Delete temperature=0.7 — it is a 400 on the target model, not a tuning knob.",
    "Put a cache_control breakpoint on the system prompt before touching anything else."
  ],
  "focus_areas": [
    { "area": "Unbreak the call on the target model",
      "why": "The model id and two parameters have to move in one commit or the deploy 400s.",
      "finding_ids": ["AC-001", "AC-002", "AC-003"] },
    { "area": "Stop paying for the same prefix twice a second",
      "why": "The stable system prompt is re-sent uncached on every thread.",
      "finding_ids": ["AC-004"] }
  ],
  "summary": "Move the model id, the thinking config and temperature together, and this bot is on a
              current model within one commit. Cache the system prompt next — it is the largest
              recurring cost visible here. Error handling was not judged: only the one call site
              was pasted, so send the wrapper module if retry behaviour is the question."
}

This is AI-generated review from source text, not a sign-off: it sees only what you sent, never a real request, a real bill or the rest of the repository. Check assumptions and open_questions before you act on the rankings, run every snippet against your own tests and staging keys, and keep a human reviewer in the loop.

Step 5 — Stream the review as it is written

POST /run-stream

/run-stream takes exactly the same body as /run but answers with server-sent events, so you can show progress instead of a spinner — useful here because a full findings table with corrected code plus an ordered plan makes for a long reply. This app's own progress panel is this endpoint. Events are separated by a blank line; each has an event: line and a data: line carrying JSON.

EventPayloadMeaning
job{job_id, status}Sent once, when the job is accepted — show "starting".
delta{text}A chunk of the reply, in order. Append it; the accumulated length is your only progress signal (the total is not known in advance). The app advances its step list by watching for the "review_name", "inventory", "findings", "migration_steps", "coverage_check" and "focus_areas" keys as they arrive.
done{job_id, status, charged_credits, output}The final, authoritative result — read the review from output.output rather than trusting concatenated deltas, and the settled price from charged_credits.
error{code, message}Replaces done when the run fails.
# -N disables buffering so events print as they arrive
curl -N -s -X POST "$API/run-stream" \
  -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -H "Idempotency-Key: cac-$(date +%s)" \
  -d @input.json

# event: job
# data: {"job_id":"job_...","status":"running"}
#
# event: delta
# data: {"text":"{\"review_name\":\"supp"}
# ...
# event: done
# data: {"job_id":"job_...","status":"succeeded","charged_credits":648,"output":{"output":"{...}"}}
import json, requests

result = None
with requests.post(
    API + "/run-stream",
    headers={"Authorization": f"Bearer {TOKEN}",
             "Idempotency-Key": "cac-001"},
    json=payload,
    stream=True,
) as r:
    r.raise_for_status()
    event = None
    for line in r.iter_lines(decode_unicode=True):
        if not line:
            continue
        if line.startswith("event:"):
            event = line[len("event:"):].strip()
        elif line.startswith("data:"):
            data = json.loads(line[len("data:"):].strip())
            if event == "delta":
                print(".", end="", flush=True)          # live progress
            elif event == "done":
                result = data
            elif event == "error":
                raise RuntimeError(data.get("message", "run failed"))

review = json.loads(result["output"]["output"])          # authoritative
print("charged:", result["charged_credits"], "-", review["review_name"])
print("posture:", review["posture"])
for f in review["findings"]:
    print(f'  [{f["priority"]}] {f["id"]} {f["resource"]}: {f["problem"]}')
with open("review.json", "w", encoding="utf-8") as fh:
    json.dump(review, fh, indent=2)
const res = await fetch(API + "/run-stream", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${TOKEN}`,
    "Content-Type": "application/json",
    "Idempotency-Key": crypto.randomUUID(),
  },
  body: JSON.stringify(payload),
});

const reader = res.body.getReader();
const decoder = new TextDecoder();
let buf = "", done = null;

for (;;) {
  const chunk = await reader.read();
  if (chunk.done) break;
  buf += decoder.decode(chunk.value, { stream: true });
  const frames = buf.split("\n\n");
  buf = frames.pop();
  for (const frame of frames) {
    const name = /^event:\s*(.+)$/m.exec(frame)?.[1];
    const body = /^data:\s*(.+)$/m.exec(frame)?.[1];
    if (!name || !body) continue;
    const data = JSON.parse(body);
    if (name === "delta") process.stdout.write(".");   // live progress
    if (name === "done") done = data;
    if (name === "error") throw new Error(data.message ?? "run failed");
  }
}

const review = JSON.parse(done.output.output);
console.log(`\n${done.charged_credits} credits - ${review.review_name} [${review.posture}]`);
for (const f of review.findings) console.log(`  [${f.priority}] ${f.id} ${f.resource}`);
writeFileSync("review.json", JSON.stringify(review, null, 2));
body, _ := json.Marshal(payload)
req, _ := http.NewRequest("POST", API+"/run-stream", bytes.NewReader(body))
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Idempotency-Key", "cac-001")

res, err := http.DefaultClient.Do(req)
if err != nil {
	log.Fatal(err)
}
defer res.Body.Close()

var event string
var final map[string]any
sc := bufio.NewScanner(res.Body)
sc.Buffer(make([]byte, 0, 64*1024), 4*1024*1024)
for sc.Scan() {
	line := sc.Text()
	switch {
	case strings.HasPrefix(line, "event:"):
		event = strings.TrimSpace(strings.TrimPrefix(line, "event:"))
	case strings.HasPrefix(line, "data:"):
		var data map[string]any
		json.Unmarshal([]byte(strings.TrimPrefix(line, "data:")), &data)
		switch event {
		case "delta":
			fmt.Print(".") // live progress
		case "done":
			final = data
		case "error":
			log.Fatal(data["message"])
		}
	}
}
// final["output"].(map[string]any)["output"].(string) is the review JSON —
// unmarshal it into the Review struct from step 4, then write it to review.json.
// Java 17+ — read the stream line by line instead of buffering the body.
var req = HttpRequest.newBuilder(URI.create(API + "/run-stream"))
    .header("Authorization", "Bearer " + TOKEN)
    .header("Content-Type", "application/json")
    .header("Idempotency-Key", "cac-001")
    .POST(HttpRequest.BodyPublishers.ofString(jsonPayload))
    .build();

var res = HTTP.send(req, HttpResponse.BodyHandlers.ofLines());
String event = null, done = null;
for (String line : (Iterable<String>) res.body()::iterator) {
    if (line.startsWith("event:")) {
        event = line.substring(6).trim();
    } else if (line.startsWith("data:")) {
        String data = line.substring(5).trim();
        if ("delta".equals(event)) System.out.print(".");   // live progress
        else if ("done".equals(event)) done = data;
        else if ("error".equals(event)) throw new RuntimeException(data);
    }
}
// parse `done`, then parse data.output.output again — it is a JSON string holding
// review_name, posture, verdict, inventory[], findings[], migration_steps[],
// coverage_check[], quick_wins[], focus_areas[] and the rest.
require "net/http"
require "json"

uri = URI(API + "/run-stream")
req = Net::HTTP::Post.new(uri)
req["Authorization"] = "Bearer #{TOKEN}"
req["Content-Type"] = "application/json"
req["Idempotency-Key"] = "cac-001"
req.body = payload.to_json

event = nil
done = nil
Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
  http.request(req) do |res|
    res.read_body do |chunk|
      chunk.each_line do |line|
        line = line.strip
        if line.start_with?("event:")
          event = line.delete_prefix("event:").strip
        elsif line.start_with?("data:")
          data = JSON.parse(line.delete_prefix("data:").strip)
          case event
          when "delta" then print "."           # live progress
          when "done"  then done = data
          when "error" then raise (data["message"] || "run failed")
          end
        end
      end
    end
  end
end

review = JSON.parse(done["output"]["output"])
puts "\n#{done["charged_credits"]} credits - #{review["review_name"]} [#{review["posture"]}]"
review["findings"].each { |f| puts "  [#{f["priority"]}] #{f["id"]} #{f["resource"]}" }
File.write("review.json", JSON.pretty_generate(review))
$event = null;
$done  = null;

$ch = curl_init(API . "/run-stream");
curl_setopt_array($ch, [
    CURLOPT_POST       => true,
    CURLOPT_HTTPHEADER => [
        "Authorization: Bearer $TOKEN",
        "Content-Type: application/json",
        "Idempotency-Key: cac-001",
    ],
    CURLOPT_POSTFIELDS => json_encode($payload),
    CURLOPT_WRITEFUNCTION => function ($ch, $chunk) use (&$event, &$done) {
        foreach (explode("\n", $chunk) as $line) {
            $line = trim($line);
            if (str_starts_with($line, "event:")) {
                $event = trim(substr($line, 6));
            } elseif (str_starts_with($line, "data:")) {
                $data = json_decode(trim(substr($line, 5)), true);
                if ($event === "delta") { echo "."; }        // live progress
                elseif ($event === "done") { $done = $data; }
                elseif ($event === "error") { throw new Exception($data["message"] ?? "run failed"); }
            }
        }
        return strlen($chunk);
    },
]);
curl_exec($ch);
curl_close($ch);

$review = json_decode($done["output"]["output"], true);
echo "\n{$done['charged_credits']} credits - {$review['review_name']} [{$review['posture']}]\n";
foreach ($review["findings"] as $f) {
    echo "  [{$f['priority']}] {$f['id']} {$f['resource']}\n";
}
file_put_contents("review.json", json_encode($review, JSON_PRETTY_PRINT));
var req = new HttpRequestMessage(HttpMethod.Post, Api + "/run-stream") {
    Content = JsonContent.Create(payload),
};
req.Headers.Add("Idempotency-Key", "cac-001");

using var res = await Http.SendAsync(req, HttpCompletionOption.ResponseHeadersRead);
using var reader = new StreamReader(await res.Content.ReadAsStreamAsync());

string? evt = null, done = null;
while (await reader.ReadLineAsync() is { } line)
{
    if (line.StartsWith("event:")) evt = line[6..].Trim();
    else if (line.StartsWith("data:"))
    {
        var data = line[5..].Trim();
        if (evt == "delta") Console.Write(".");            // live progress
        else if (evt == "done") done = data;
        else if (evt == "error") throw new Exception(data);
    }
}

using var final = JsonDocument.Parse(done!);
var text = final.RootElement.GetProperty("output").GetProperty("output").GetString();
using var reviewDoc = JsonDocument.Parse(text!);
var review = reviewDoc.RootElement;
Console.WriteLine($"{review.GetProperty("review_name")} [{review.GetProperty("posture")}]");
foreach (var f in review.GetProperty("findings").EnumerateArray())
    Console.WriteLine($"  [{f.GetProperty("priority")}] {f.GetProperty("id")} {f.GetProperty("resource")}");
await File.WriteAllTextAsync("review.json", text!);

In a browser, the native EventSource only speaks GET, and this endpoint is a POST — read the fetch response body incrementally, as the JavaScript sample above does. On an idempotent replay the server may answer with a plain JSON envelope instead of an event stream; check the Content-Type before you start parsing frames.

Step 6 — A CI gate on the posture

The last mile: read the review JSON and turn it into an exit code. The rule below fails the job when posture is breakage-risk — something in the code returns an error on the model in use or on the stated migration target, or a credential is exposed — or when any finding is critical, and warns without failing on update-recommended. If you send prescan_facts.flags, also assert that every id you sent came back in coverage_check: that is your guarantee that nothing you already knew about was quietly dropped.

The Idempotency-Key here is derived from the input — a hash of the exact payload — rather than a timestamp or a random UUID. The same bot.py and the same context therefore produce the same key, so a re-run of a red CI job, a retried flaky step or two jobs racing on the same commit all replay the first result instead of paying for a second review. Change a byte of the input and the key changes with it, which is exactly when you do want a fresh run.

# input.json is the payload built in step 3
KEY="cac-$(shasum -a 256 input.json | cut -c1-32)"   # same input, same key, no double charge

JOB_ID=$(curl -s -X POST "$API/run" \
  -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -H "Idempotency-Key: $KEY" \
  -d @input.json | jq -r '.data.job_id')

while :; do
  JOB=$(curl -s "$API/jobs/$JOB_ID" -H "Authorization: Bearer $TOKEN")
  STATUS=$(echo "$JOB" | jq -r '.data.status')
  [ "$STATUS" = "succeeded" ] || [ "$STATUS" = "failed" ] && break
  sleep 2
done
[ "$STATUS" = "succeeded" ] || { echo "run failed"; exit 2; }

echo "$JOB" | jq -r '.data.output.output' > review.json

POSTURE=$(jq -r '.posture' review.json)
CRIT=$(jq '[.findings[] | select(.priority == "critical")] | length' review.json)
UNSEEN=$(jq -r '[.coverage_check[] | select(.addressed | not) | .id] | join(", ")' review.json)

echo "posture: $POSTURE   critical: $CRIT"
[ -n "$UNSEEN" ] && echo "set aside: $UNSEEN"
jq -r '.migration_steps[] | "next: \(.step) - \(.action)"' review.json | head -3

if [ "$POSTURE" = "breakage-risk" ] || [ "$CRIT" -gt 0 ]; then
  jq -r '"BLOCKED: \(.verdict)"' review.json
  exit 1
fi
[ "$POSTURE" = "update-recommended" ] && jq -r '"warning: \(.verdict)"' review.json
exit 0
import hashlib, json, sys, time

# The key is a hash of the payload itself: identical input replays, changed input re-runs.
canonical = json.dumps(payload, sort_keys=True, separators=(",", ":"))
key = "cac-" + hashlib.sha256(canonical.encode("utf-8")).hexdigest()[:32]

job_id = api("POST", "/run", payload, **{"Idempotency-Key": key})["job_id"]
while True:
    job = api("GET", f"/jobs/{job_id}")
    if job["status"] in ("succeeded", "failed"):
        break
    time.sleep(1.5)
if job["status"] != "succeeded":
    sys.exit(2)

raw = job["output"]
if isinstance(raw, dict) and "output" in raw:
    raw = raw["output"]
review = json.loads(raw) if isinstance(raw, str) else raw

critical = [f for f in review["findings"] if f["priority"] == "critical"]
set_aside = [c["id"] for c in review["coverage_check"] if not c["addressed"]]
sent = {f["id"] for f in payload["prescan_facts"]["flags"]}
missing = sent - {c["id"] for c in review["coverage_check"]}

print(f'posture: {review["posture"]}   critical: {len(critical)}')
if set_aside:
    print("set aside:", ", ".join(set_aside))
if missing:
    print("NOT RECONCILED:", ", ".join(sorted(missing)))
for s in review["migration_steps"][:3]:
    print(f'next: {s["step"]} - {s["action"]}')

if review["posture"] == "breakage-risk" or critical or missing:
    sys.exit(f'BLOCKED: {review["verdict"]}')
if review["posture"] == "update-recommended":
    print("warning:", review["verdict"])
import { createHash } from "node:crypto";

// The key is a hash of the payload itself: identical input replays, changed input re-runs.
const canonical = JSON.stringify(payload, Object.keys(payload).sort());
const key = "cac-" + createHash("sha256").update(canonical).digest("hex").slice(0, 32);

const { job_id } = await api("POST", "/run", payload, { "Idempotency-Key": key });

let job;
do {
  await new Promise((r) => setTimeout(r, 1500));
  job = await api("GET", `/jobs/${job_id}`);
} while (job.status !== "succeeded" && job.status !== "failed");
if (job.status !== "succeeded") process.exit(2);

const raw = job.output?.output ?? job.output;
const review = typeof raw === "string" ? JSON.parse(raw) : raw;

const critical = review.findings.filter((f) => f.priority === "critical");
const setAside = review.coverage_check.filter((c) => !c.addressed).map((c) => c.id);
const seen = new Set(review.coverage_check.map((c) => c.id));
const missing = payload.prescan_facts.flags.map((f) => f.id).filter((id) => !seen.has(id));

console.log(`posture: ${review.posture}   critical: ${critical.length}`);
if (setAside.length) console.log("set aside:", setAside.join(", "));
if (missing.length) console.log("NOT RECONCILED:", missing.join(", "));
for (const s of review.migration_steps.slice(0, 3)) {
  console.log(`next: ${s.step} - ${s.action}`);
}

if (review.posture === "breakage-risk" || critical.length || missing.length) {
  console.error(`BLOCKED: ${review.verdict}`);
  process.exit(1);
}
if (review.posture === "update-recommended") console.warn(`warning: ${review.verdict}`);
// The key is a hash of the payload itself: identical input replays, changed input re-runs.
raw, _ := json.Marshal(payload)
sum := sha256.Sum256(raw)
key := "cac-" + hex.EncodeToString(sum[:])[:32]

body := bytes.NewReader(raw)
req, _ := http.NewRequest("POST", API+"/run", body)
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Idempotency-Key", key)
// ...send it, read data.job_id, then poll /jobs/{id} exactly as in step 4.

// review is the Review struct from step 4:
critical := 0
for _, f := range review.Findings {
	if f.Priority == "critical" {
		critical++
	}
}
for _, c := range review.CoverageCheck {
	if !c.Addressed {
		fmt.Println("set aside:", c.ID, "-", c.Note)
	}
}
fmt.Printf("posture: %s   critical: %d\n", review.Posture, critical)
for i, s := range review.MigrationSteps {
	if i < 3 {
		fmt.Printf("next: %s - %s\n", s.Step, s.Action)
	}
}
if review.Posture == "breakage-risk" || critical > 0 {
	fmt.Fprintln(os.Stderr, "BLOCKED: "+review.Verdict)
	os.Exit(1)
}
// The key is a hash of the payload itself: identical input replays, changed input re-runs.
import java.security.MessageDigest;
import java.util.HexFormat;

byte[] digest = MessageDigest.getInstance("SHA-256")
    .digest(jsonPayload.getBytes(java.nio.charset.StandardCharsets.UTF_8));
String key = "cac-" + HexFormat.of().formatHex(digest).substring(0, 32);

var req = HttpRequest.newBuilder(URI.create(API + "/run"))
    .header("Authorization", "Bearer " + TOKEN)
    .header("Content-Type", "application/json")
    .header("Idempotency-Key", key)
    .POST(HttpRequest.BodyPublishers.ofString(jsonPayload))
    .build();
// send it, read data.job_id, poll /jobs/{id} as in step 4, then parse data.output.output.

// With the review parsed (your JSON library of choice):
//   String posture = review.get("posture");
//   long critical  = count of findings[] whose priority equals "critical";
//   every coverage_check[] entry with addressed == false is a set-aside prescan flag.
// if (posture.equals("breakage-risk") || critical > 0) {
//     System.err.println("BLOCKED: " + review.get("verdict"));
//     System.exit(1);
// }
// migration_steps[] is the ordered to-do list to print above the failure.
require "digest"

# The key is a hash of the payload itself: identical input replays, changed input re-runs.
key = "cac-" + Digest::SHA256.hexdigest(JSON.generate(payload))[0, 32]

started = api("POST", "/run", payload) # add the header in your api() helper:
                                       #   req["Idempotency-Key"] = key
job = nil
loop do
  job = api("GET", "/jobs/#{started["job_id"]}")
  break if %w[succeeded failed].include?(job["status"])
  sleep 1.5
end
exit 2 unless job["status"] == "succeeded"

raw = job["output"].is_a?(Hash) ? job["output"].fetch("output", job["output"]) : job["output"]
review = raw.is_a?(String) ? JSON.parse(raw) : raw

critical  = review["findings"].count { |f| f["priority"] == "critical" }
set_aside = review["coverage_check"].reject { |c| c["addressed"] }.map { |c| c["id"] }
seen      = review["coverage_check"].map { |c| c["id"] }
missing   = payload[:prescan_facts][:flags].map { |f| f[:id] } - seen

puts "posture: #{review["posture"]}   critical: #{critical}"
puts "set aside: #{set_aside.join(", ")}" unless set_aside.empty?
puts "NOT RECONCILED: #{missing.join(", ")}" unless missing.empty?
review["migration_steps"].first(3).each { |s| puts "next: #{s["step"]} - #{s["action"]}" }

if review["posture"] == "breakage-risk" || critical > 0 || !missing.empty?
  warn "BLOCKED: #{review["verdict"]}"
  exit 1
end
warn "warning: #{review["verdict"]}" if review["posture"] == "update-recommended"
<?php
// The key is a hash of the payload itself: identical input replays, changed input re-runs.
$key = "cac-" . substr(hash("sha256", json_encode($payload)), 0, 32);

// pass $key through to the request as the Idempotency-Key header, then poll as in step 4
$started = api("POST", "/run", $payload);
do {
    sleep(2);
    $job = api("GET", "/jobs/" . $started["job_id"]);
} while (!in_array($job["status"], ["succeeded", "failed"]));
if ($job["status"] !== "succeeded") { exit(2); }

$raw = is_array($job["output"]) ? ($job["output"]["output"] ?? $job["output"]) : $job["output"];
$review = is_string($raw) ? json_decode($raw, true) : $raw;

$critical = count(array_filter($review["findings"], fn($f) => $f["priority"] === "critical"));
$setAside = array_column(array_filter($review["coverage_check"], fn($c) => !$c["addressed"]), "id");
$seen     = array_column($review["coverage_check"], "id");
$missing  = array_diff(array_column($payload["prescan_facts"]["flags"], "id"), $seen);

echo "posture: {$review['posture']}   critical: $critical\n";
if ($setAside) { echo "set aside: " . implode(", ", $setAside) . "\n"; }
if ($missing)  { echo "NOT RECONCILED: " . implode(", ", $missing) . "\n"; }
foreach (array_slice($review["migration_steps"], 0, 3) as $s) {
    echo "next: {$s['step']} - {$s['action']}\n";
}

if ($review["posture"] === "breakage-risk" || $critical > 0 || $missing) {
    fwrite(STDERR, "BLOCKED: {$review['verdict']}\n");
    exit(1);
}
if ($review["posture"] === "update-recommended") {
    fwrite(STDERR, "warning: {$review['verdict']}\n");
}
using System.Security.Cryptography;

// The key is a hash of the payload itself: identical input replays, changed input re-runs.
var bytes = JsonSerializer.SerializeToUtf8Bytes(payload);
var key = "cac-" + Convert.ToHexString(SHA256.HashData(bytes)).ToLowerInvariant()[..32];

var run = new HttpRequestMessage(HttpMethod.Post, Api + "/run") {
    Content = JsonContent.Create(payload),
};
run.Headers.Add("Idempotency-Key", key);
// send it, read data.job_id, poll /jobs/{id} as in step 4, then:

var reviewText = job.GetProperty("output").GetProperty("output").GetString();
using var reviewDoc = JsonDocument.Parse(reviewText!);
var review = reviewDoc.RootElement;

var posture = review.GetProperty("posture").GetString();
var critical = review.GetProperty("findings").EnumerateArray()
    .Count(f => f.GetProperty("priority").GetString() == "critical");

Console.WriteLine($"posture: {posture}   critical: {critical}");
foreach (var c in review.GetProperty("coverage_check").EnumerateArray())
    if (!c.GetProperty("addressed").GetBoolean())
        Console.WriteLine($"set aside: {c.GetProperty("id")} - {c.GetProperty("note")}");
foreach (var s in review.GetProperty("migration_steps").EnumerateArray().Take(3))
    Console.WriteLine($"next: {s.GetProperty("step")} - {s.GetProperty("action")}");

if (posture == "breakage-risk" || critical > 0)
{
    Console.Error.WriteLine($"BLOCKED: {review.GetProperty("verdict")}");
    Environment.Exit(1);
}

Gate on posture and priority, not on the finding count — a thorough review of a healthy integration can list a dozen improvements and still be api-current. And keep the review artifact: review.json plus migration_steps is the ticket description for whatever the gate just blocked.