# `cluster_logs` — Cluster Noisy Logs via Drain Prefix-Tree

> Part of [Trace Diet — HAR & Log Context Distiller API](https://trace-diet.apikeg.com/llms.txt) (`com.apikeg/trace-diet`)

Folds multi-line stack traces (JS/Java 'at ...', Python Traceback, Go panics, Java 'Caused by:'), parses plain-text, Docker/Kubernetes CRI, and NDJSON logs, pre-masks <UUID>/<IP>/<HEX>/<ISO_TS>/<URL>/<NUM>, and runs a fixed-depth Drain prefix-tree clusterer to collapse 50,000+ log lines by 95–99% with errorClusters highlighted first.

**Why call instead of DIY:** Avoids naive grep/head truncation that misses rare fatal stack traces buried at line 41,000, cutting 500k-token log dumps to ~2k tokens in <50ms.

- **REST Endpoint:** `POST https://trace-diet.apikeg.com/v1/cluster-logs`
- **MCP Server:** `https://trace-diet.apikeg.com/mcp` (tool: `cluster_logs`)
- **Pricing:** $0.002 base (up to $0.008 max ($0.002 base + $0.001 per 2,000 log lines, capped at $0.008), x402 variable settlement)
- **Free Dry-Run:** `POST https://trace-diet.apikeg.com/v1/cluster-logs?dry_run=true`

## Input JSON Schema
```json
{
  "type": "object",
  "properties": {
    "logs": {
      "type": "string",
      "minLength": 1,
      "maxLength": 5000000,
      "description": "Raw plain-text, Docker/Kubernetes CRI, or newline-delimited JSON (ndjson) log stream."
    },
    "lines": {
      "type": "array",
      "items": {
        "type": "string",
        "maxLength": 32768
      },
      "minItems": 1,
      "maxItems": 100000,
      "description": "Pre-split array of log lines (alternative to passing a single 'logs' string)."
    },
    "similarityThreshold": {
      "type": "number",
      "default": 0.6,
      "minimum": 0.2,
      "maximum": 1,
      "description": "Drain token-similarity threshold [0.2-1.0] required to merge a log line into an existing canonical template."
    },
    "treeDepth": {
      "type": "integer",
      "default": 4,
      "minimum": 3,
      "maximum": 8,
      "description": "Fixed Drain prefix-tree depth (length -> leading tokens -> leaf templates)."
    },
    "maxClusters": {
      "type": "integer",
      "default": 300,
      "minimum": 10,
      "maximum": 1000,
      "description": "Maximum canonical Drain clusters maintained before routing outliers to overflow."
    },
    "maxReturnedClusters": {
      "type": "integer",
      "default": 100,
      "minimum": 1,
      "maximum": 500,
      "description": "Maximum ranked clusters returned in the response payload."
    },
    "maxStackTraceChars": {
      "type": "integer",
      "default": 1500,
      "minimum": 128,
      "maximum": 8192,
      "description": "Maximum characters retained per cluster sampleStackTrace."
    }
  },
  "additionalProperties": false
}
```

## Examples
### Cluster Kubernetes & Node.js crash stream
```bash
curl -X POST "https://trace-diet.apikeg.com/v1/cluster-logs" \
  -H "Content-Type: application/json" \
  -d '{"similarityThreshold":0.6,"treeDepth":4,"maxClusters":300,"maxReturnedClusters":100,"maxStackTraceChars":1500,"logs":"2026-11-30T08:00:01.100Z INFO [http] GET /healthz from 10.244.1.8 completed in 2ms\n2026-11-30T08:00:02.100Z INFO [http] GET /healthz from 10.244.1.9 completed in 3ms\n2026-11-30T08:00:03.250Z ERROR [db] Deadlock detected for tx 550e8400-e29b-41d4-a716-446655440000 after 3 retries\n    at PoolClient.query (/srv/app/db.ts:118:13)\n    at OrderRepo.commit (/srv/app/orders.ts:74:9)"}'
```
Expected output: Highlights 1 ERROR cluster (with folded stack trace) ahead of 1 INFO template (<IP>, <NUM> masked).
