# Deterministic Broken LLM JSON Auto-Repair & AST Healer API [API-382]
> Repair truncated LLM JSON, trailing commas, unclosed quotes, markdown fences, and Python True/False/None literals in sub-1ms without paying for an LLM retry.

## Specifications
- Canonical Documentation: https://topaigateway.com/api/json-surgeon
- Production Endpoint: https://topaigateway.com/v1/tollbooth/json-surgeon
- Method: POST
- Category: FinOps & Structured Output Reliability
- Subscription Pack: Cursor & Claude Code Harness ($49 Once) / Pack 1 DevEx ($0 7-Day Trial) ($49 one-time (or $0 7-Day Free Trial on Pack 1))
- Latency Target: < 1 ms
- Free Tier: 100 free requests/month (no credit card required)

## Quickstart (cURL)
```bash
curl -X POST https://topaigateway.com/v1/tollbooth/json-surgeon \
  -H "Content-Type: application/json" \
  -d '{"broken_json": "{\"items\": [{\"id\": 1, \"active\": True, \"title\": \"Truncated LLM output"}'
```

## Sample Request
```json
{
  "broken_json": "```json\n{\"agent\": \"cursor-coder\", \"active\": True, \"nullable\": None, \"steps\": [{\"id\": 1, \"action\": \"unclosed string and trailing comma\",}"
}
```

## Sample Response
```json
{
  "status": "REPAIRED_RFC8259_VALID",
  "valid_json": true,
  "repaired_string": "{\"agent\": \"cursor-coder\", \"active\": true, \"nullable\": null, \"steps\": [{\"id\": 1, \"action\": \"unclosed string and trailing comma\"}]}",
  "parsed_object": {
    "agent": "cursor-coder",
    "active": true,
    "nullable": null,
    "steps": [
      {
        "id": 1,
        "action": "unclosed string and trailing comma"
      }
    ]
  },
  "llm_retry_cost_saved_usd": 0.03,
  "latency_ms": 0.42
}
```

## SDK Code Samples
### TypeScript / Node.js
```typescript
// Zero-LLM retry fallback when JSON.parse(llmOutput) throws SyntaxError
const res = await fetch("https://topaigateway.com/v1/tollbooth/json-surgeon", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ broken_json: rawLlmResponse })
});
const { parsed_object } = await res.json();
```

### Python
```python
import requests

# Also available via universal catalog route: /v1/api/382-json-surgeon
resp = requests.post(
    "https://topaigateway.com/v1/tollbooth/json-surgeon",
    json={"broken_json": '{\"items\": [{\"id\": 1, \"active\": True, \"title\": \"Truncated'}
)
data = resp.json()
print("Valid RFC 8259 JSON:", data["valid_json"], data["parsed_object"])
```

## Performance Benchmarks
- Repair Latency: 0.4 ms
- LLM Retry Saved: $0.03 / call
- Engine Type: Pushdown AST
- RFC 8259 Recovery: 99.8% Success

## Compliance & Governance
- European Union DORA (UE) 2022/2554 Article 30 Compliant
- Zero-Retention & Zero-PII Policy: Prompts are executed on memory-only V8 isolates
- 1-Click Upgrade Portal: https://topaigateway.com/#pricing
