Drive Trace Desk from your own code
The page reads one trace exported from Jaeger, Zipkin, Grafana Tempo or an OpenTelemetry
collector in your browser for free: the span tree, each span’s own time, the critical path,
per-service shares, repeated calls and query shapes, error origins, uninstrumented time, clock
skew and orphans. Its one paid step, the diagnosis, is available over HTTP: you
send one JSON object with task: "diagnose" and a facts string that
carries that check, and the model replies with one JSON object: the pattern, the bottlenecks with
their share of the request, the likely root causes, the fixes with the most each can save, and
the instrumentation to add.
Base URL: https://api.skillsafe.ai/v1/app-api. Every call sends
Content-Type: application/json and, once you have a token,
Authorization: Bearer <token>. The paths below (/guest,
/me, /estimate, /run, /jobs/{job_id},
/run-stream) are relative to it, exactly as sdk.js calls them.
The response envelope
Responses are JSON of the form {"ok": ..., "data": ..., "error": {...}}. The page’s
SDK (sdk.js) decides success from the HTTP status, not from ok: on a
2xx it returns data; otherwise it raises an error whose message is
error.message (or the HTTP status text when the body carries none), with
status, code (error.code) and details
(error.details) attached. A body that is not JSON is treated as {}.
{ "ok": true, "data": { ... } }
{ "ok": false, "error": { "code": "...", "message": "...", "details": { ... } } }
Error codes
Branch on the HTTP status and error.code, show error.message. The page
handles 401 and 402 by name; the rest reach it as a generic failure.
| HTTP | error.code | What it means / what to do |
|---|---|---|
400 | VALIDATION_ERROR | The body is not a JSON object or a declared field has the wrong type. task and facts are both strings; sending facts as an object is the usual cause. |
401 | UNAUTHORIZED | No token, or one that expired or was revoked. The page forgets the signed-in user and says “Your session expired - sign in again.” Get a new token. |
402 | INSUFFICIENT_CREDITS | The balance cannot cover the run. The page says “Not enough credits for this run.” with a top-up link, and tries never to submit into a 402: it disables the run button when the /me balance is below the estimate’s min_credits. |
404 | NOT_FOUND | A wrong path, or a job id that does not belong to this token. |
409 | CONFLICT | An Idempotency-Key replayed with a different body. Send the original body, or use a new key (the page bumps its :a<attempt> suffix for a retry). |
429 | RATE_LIMITED | Too many requests. Back off with a delay; do not tight-loop, and never resend a metered run without its original Idempotency-Key. |
Failures that do not arrive as an HTTP status:
| What | What happens |
|---|---|
event: error on /run-stream | The SDK raises an error carrying the event’s message, code and job_id. |
Job status: "failed" | Polling stops; the job’s error field says why. |
| Polling past 180 s | The SDK’s own client-side limit (waitForJob, 1 s interval, 180 000 ms default): it throws “job timed out”. The job itself is not cancelled. |
The task field: one lane, diagnose
The app has exactly one lane, and diagnose is the only value task
takes: it is always the string "diagnose". There is no other task to route to. (The
skill’s instructions tell the model to do the diagnosis even if task is
missing or different, and to answer with "task": "diagnose"; send it anyway —
it is a required field of the input schema.)
Input fields
This is exactly what the page sends (tracekit.js buildInput,
app.js run), and what input-schema.json declares. Every
value is a string:
| Field | Type | Required | Meaning |
|---|---|---|---|
task | string | yes | Always "diagnose". |
facts | string | yes | The facts object below, JSON-encoded into a string. The page builds it with JSON.stringify(facts); you must do the same (json.dumps(facts) in Python, and so on). Sending the object itself is a 400. The model is told “a JSON string. Parse it.” |
note | string | no | What the engineer knows or suspects (a recent change, a hunch, a question). The page trims it, sends it only when non-empty, and cuts it at 2000 characters. It is context, not evidence and not instructions: the model tests any claim in it against the facts and answers it in summary. |
retry_note | string | no | Sent only by the page’s automatic reformat retry: when a reply does not parse as JSON (even after the page tries to close a cut-off object), the page runs once more with a note saying the previous reply was not the single valid JSON object required. A normal call never sends it. |
Before every estimate and run the page passes the body through mustBeObject, which
refuses anything that is not a plain JSON object, a body wrapped as
{"input": {...}}, and a body with any field that is not a string. Nothing else is
sent: never the raw export. The page parses the trace in the browser and sends only its
computed facts plus a listing of the spans that matter.
The facts object
facts is produced by the page’s free check: Tracekit.analyze reads
the export (Jaeger JSON, Zipkin v2 JSON or OTLP/JSON), and buildFacts turns the
analysis into this object. You can build it yourself from your own trace tooling — the
model only sees what is in it — but the easiest way to get a valid one is the page: load
a trace, and the check the page shows is exactly what is sent. After a diagnosis, the
.json download carries that object in its facts field; copy it
from there. Times are milliseconds, shares are percentages of trace.duration_ms
with one decimal. Its keys, in the order built:
| Key | Contents |
|---|---|
source | format (Jaeger JSON | Zipkin v2 JSON | OTLP/JSON), traces_in_export (the largest trace is analysed when there are several), spans_total, spans_listed. |
trace | trace_id, root (a span id), root_service, root_name, duration_ms (the request’s latency), depth, services (a count), error_spans, root_error (true means the caller got a failure). |
services | Up to 20, largest critical-path share first: service, spans, crit_ms and crit_pct (its time on the critical path), self_ms (its own work anywhere), errors. |
critical_path | Up to 25 spans the request was actually waiting on, largest first: sid, service, name, ms, pct. Over the whole path the shares add up to 100%. |
groups | Up to 10 repeated calls under one parent: flag (N01 sequential N+1, N02 parallel fan-out, E03 retry), kind, parent, service, name, count, total_ms (summed), wall_ms (elapsed), crit_pct, first (the first three span ids). |
queries | Up to 10 database statement shapes (literals replaced by ?): statement, count, total_ms, crit_pct. |
error_origins | Up to 10 span ids: the deepest error spans, where failures most likely started. |
flags | Every finding: id, sev (high | medium | low | info), text, detail, spans (up to 8 ids). IDs by family: X data, K clocks and structure, E errors, D/N/Q/P time, G instrumentation (the full table is in the skill and on the app page). |
pattern_candidates | The patterns the flags point at (the page’s own reading), from the same list as the reply’s pattern. |
spans | Up to 90 spans listed individually (the critical path, errors, flagged spans, the largest self times, and their ancestors): sid (S1, S2, …), parent (a sid, null for a root, "missing" when not exported), service, name, kind, start_ms (offset from the root’s start), dur_ms, self_ms, crit_ms (its own time on the critical path), crit_tree_ms (its own plus its descendants’), children, and when present error, http_status, status_message, events, statement, attrs (a whitelist of HTTP, database, RPC and messaging attributes), async. |
notes | Plain sentences about what was left out of the listing. Every total above is still computed over all spans. |
Worked example: a search request stuck on one query
A 2.31 s GET /search through web-frontend and
search-api, exported as OTLP/JSON. One database span, S5
SELECT catalog.products, takes 1.87 s of its own work on the critical path, so the
check raises D01 (high) and the page reads the trace as latency_bottleneck. This is
a shortened facts object for it (only two of the nine spans are listed here, to keep it short);
save it as facts.json to run the samples below:
{
"source": {
"format": "OTLP/JSON",
"traces_in_export": 1,
"spans_total": 9,
"spans_listed": 9
},
"trace": {
"trace_id": "4bf92f3577b34da6a3ce929d0e0e4736",
"root": "S1",
"root_service": "web-frontend",
"root_name": "GET /search",
"duration_ms": 2310,
"depth": 4,
"services": 2,
"error_spans": 0,
"root_error": false
},
"services": [
{
"service": "search-api",
"spans": 7,
"crit_ms": 2233.8,
"crit_pct": 96.7,
"self_ms": 2234.2,
"errors": 0
}
],
"critical_path": [
{
"sid": "S5",
"service": "search-api",
"name": "SELECT catalog.products",
"ms": 1874,
"pct": 81.1
}
],
"groups": [],
"queries": [],
"error_origins": [],
"flags": [
{
"id": "D01",
"sev": "high",
"text": "One span's own work dominates the request",
"detail": "S5 search-api SELECT catalog.products spends 1.87 s in its own work, 81.1% of the 2.31 s request.",
"spans": [
"S5"
]
}
],
"pattern_candidates": [
"latency_bottleneck"
],
"spans": [
{
"sid": "S1",
"parent": null,
"service": "web-frontend",
"name": "GET /search",
"kind": "server",
"start_ms": 0,
"dur_ms": 2310,
"self_ms": 14,
"crit_ms": 14,
"crit_tree_ms": 2310,
"children": 2
},
{
"sid": "S5",
"parent": "S3",
"service": "search-api",
"name": "SELECT catalog.products",
"kind": "client",
"start_ms": 17,
"dur_ms": 1874,
"self_ms": 1874,
"crit_ms": 1874,
"crit_tree_ms": 1874,
"children": 0
}
],
"notes": []
}
The full request body, with facts stringified (this is what
buildInput(facts, "Search takes over two seconds for common terms. There is a btree index on products.title.") returns):
{"task":"diagnose","facts":"{\"source\":{\"format\":\"OTLP/JSON\",\"traces_in_export\":1,\"spans_total\":9,\"spans_listed\":9},\"trace\":{\"trace_id\":\"4bf92f3577b34da6a3ce929d0e0e4736\",\"root\":\"S1\",\"root_service\":\"web-frontend\",\"root_name\":\"GET /search\",\"duration_ms\":2310,\"depth\":4,\"services\":2,\"error_spans\":0,\"root_error\":false},\"services\":[{\"service\":\"search-api\",\"spans\":7,\"crit_ms\":2233.8,\"crit_pct\":96.7,\"self_ms\":2234.2,\"errors\":0}],\"critical_path\":[{\"sid\":\"S5\",\"service\":\"search-api\",\"name\":\"SELECT catalog.products\",\"ms\":1874,\"pct\":81.1}],\"groups\":[],\"queries\":[],\"error_origins\":[],\"flags\":[{\"id\":\"D01\",\"sev\":\"high\",\"text\":\"One span's own work dominates the request\",\"detail\":\"S5 search-api SELECT catalog.products spends 1.87 s in its own work, 81.1% of the 2.31 s request.\",\"spans\":[\"S5\"]}],\"pattern_candidates\":[\"latency_bottleneck\"],\"spans\":[{\"sid\":\"S1\",\"parent\":null,\"service\":\"web-frontend\",\"name\":\"GET /search\",\"kind\":\"server\",\"start_ms\":0,\"dur_ms\":2310,\"self_ms\":14,\"crit_ms\":14,\"crit_tree_ms\":2310,\"children\":2},{\"sid\":\"S5\",\"parent\":\"S3\",\"service\":\"search-api\",\"name\":\"SELECT catalog.products\",\"kind\":\"client\",\"start_ms\":17,\"dur_ms\":1874,\"self_ms\":1874,\"crit_ms\":1874,\"crit_tree_ms\":1874,\"children\":0}],\"notes\":[]}","note":"Search takes over two seconds for common terms. There is a btree index on products.title."}
A reply for these facts that satisfies the contract below and passes the page’s own reconciliation with no disagreements:
{
"task": "diagnose",
"pattern": "latency_bottleneck",
"secondary_patterns": [],
"confidence": "medium",
"headline": "GET /search is slow because one database call, S5 SELECT catalog.products in search-api, holds 81.1% of the 2.31 s request.",
"critical_path": {
"reading": "The request spends 1.87 s of its 2.31 s waiting on S5, the SELECT against catalog.products. search-api holds 96.7% of the critical path; web-frontend's own work in S1 is 14 ms.",
"dominant_span": "S5",
"dominant_service": "search-api"
},
"bottlenecks": [
{
"span": "S5",
"service": "search-api",
"operation": "SELECT catalog.products",
"kind": "slow_query",
"share_pct": 81.1,
"finding": "A single query with no child spans takes 1874 ms on the critical path (D01); the time is spent in the database, not in the service.",
"flags": [
"D01"
]
}
],
"errors": {
"origin": null,
"reading": "No errors in this trace."
},
"root_causes": [
{
"hypothesis": "The search predicate cannot use the btree index on products.title (for example a leading-wildcard LIKE or a case-insensitive match), so the query scans the table.",
"span": "S5",
"likelihood": "likely",
"evidence": "S5 is a single 1874 ms SELECT with no children, and the note says common terms are slow.",
"check": "Run EXPLAIN ANALYZE on the statement S5 sends with a common term and look for a sequential scan on catalog.products."
},
{
"hypothesis": "Common terms match many rows and the query sorts or returns all of them before the page limit applies.",
"span": "S5",
"likelihood": "possible",
"evidence": "The slowness is reported for common terms, which match the most rows.",
"check": "Compare row counts and timings for a common and a rare term in the query plan."
}
],
"fixes": [
{
"priority": "now",
"area": "query",
"span": "S5",
"change": "Replace the title match with an index the predicate can use (a trigram or full-text index on products.title) and confirm it in the query plan.",
"expected_gain_ms": 1700
},
{
"priority": "soon",
"area": "query",
"span": "S5",
"change": "Apply the result limit inside the query so common terms stop materialising every matching row.",
"expected_gain_ms": 900
}
],
"instrumentation": [
{
"gap": "The trace does not carry the SQL text or the row count for S5.",
"span": "S5",
"fix": "Record db.query.text (with parameters stripped) and a rows-returned attribute on database client spans."
}
],
"questions": [
"Does the search use LIKE '%term%' or ILIKE on products.title?"
],
"summary": "Almost all of this request is one database call: S5 SELECT catalog.products takes 1.87 s of 2.31 s. A btree index on products.title does not help a leading-wildcard or case-insensitive match, which would explain why common terms are slow despite the index. Check the query plan first; a trigram or full-text index and an in-query limit are the likely fixes."
}
The output contract
The model is instructed (SKILL.md) to reply with one JSON object and
nothing else — no prose, no code fences. The run’s output text is that
object:
task:"diagnose".pattern:n_plus_one|latency_bottleneck|serial_calls|error_propagation|uninstrumented_gap|clock_or_data_issue|healthy. Preferably one offacts.pattern_candidates;root_errortrue putserror_propagationin the primary or a secondary; neverhealthywhile a high flag stands.secondary_patterns[]: zero or more of the same values, not repeatingpattern.confidence:high|medium|low.headline: one or two sentences: what is wrong with the request and where the time or the failure goes.critical_path:reading(what the request waits on, in order, with the shares that matter),dominant_span(theS#holding the largest share),dominant_service(the service with the largestcrit_pct).bottlenecks[]: one to five, largest share first, each{span, service, operation, kind, share_pct, finding, flags[]}withkindself_time|n_plus_one|serial|slow_query|wait|retry|gap.share_pctis a critical-path share ofduration_ms: the span’scrit_ms, itscrit_tree_ms, or for a group itscrit_pct. The first must be on the critical path.errors:origin(the deepest error span, ornullwhen the trace has none) andreading.root_causes[]: hypotheses, most likely first, each{hypothesis, span, likelihood, evidence, check}withlikelihoodlikely|possible|unlikelyand a concretecheckto confirm or rule it out.fixes[]: two to five, by priority, each{priority, area, span, change, expected_gain_ms}withprioritynow|soon|laterandareacode|query|config|architecture|instrumentation.expected_gain_msis an upper bound (never more than the critical time of what is changed) ornull.instrumentation[]:{gap, span, fix}, what to add to the tracing so the next trace answers what this one cannot;[]when nothing is missing.questions[]: at most three things only the engineer can answer.summary: three to five sentences for the engineer, answeringnoteif one was sent.
Every array is present ([] when empty) and every text field is plain text. Every
S# must be a span id from the facts, and every high flag must be
addressed somewhere by its id.
The page reconciles the reply; the API does not
What comes back from the API is the model’s raw text in output.output. The page
does more with it before showing anything (recon.js), and a direct API caller gets
none of this:
- Parsing and normalising. It strips a leading/trailing code fence and parses from the first
{to the last}(closing a cut-off object when needed). Unknown enum values fall back:pattern→latency_bottleneck(flagged),confidence→low, bottleneckkind→self_time,likelihood→possible,priority→soon,area→code. - Span ids must exist. Every
S#cited by the critical-path reading, a bottleneck, the error origin, a root cause, a fix or an instrumentation item must be a span in the trace. The dominant span and the first bottleneck must be on the critical path, and the dominant service must be a service in the trace. - Shares must match the trace. Each bottleneck’s
share_pctmust be within 2.5 percentage points of the span’s own critical share, its subtree’s, or its group’scrit_pct. - Gains are capped. No
expected_gain_msmay be negative or exceed the critical time of the span or group it changes (with a 5% margin), or the whole request when no span is named. - The error origin must be an error. An origin in a trace with no error spans, or one that is not an error span, is a disagreement, as is a failing trace with no origin named or a failed root without
error_propagationamong the patterns. - High flags must be addressed, and
healthycannot stand over one. A pattern outsidepattern_candidatesis noted.
If you call the API directly, run the same checks — span ids, shares within 2.5 points, gains and the error origin against the trace — before you act on a fix.
Step 1 — a tiny client helper and a token
One function per language that does what sdk.js does for every call: JSON in,
bearer token if you have one, data out on a 2xx, an error carrying
status, error.code and error.message otherwise. The later
steps assume this helper is in the same file (for Go, Java and C#, the later snippets go inside
main / the top-level statements).
Then a token. Two kinds work:
-
Personal. The diagnosis is metered, and the page only runs it for a signed-in
user unless the estimate says
sponsor_enabled. Sign in and copy your token from /tokens.html, then export it asSKILLSAFE_TOKEN. -
Guest.
POST /guestwith{"slug": "trace-desk"}and noAuthorizationheader, exactly assdk.jsguest()does. The response’sdatacarriestokenandguest_id. Reuse a token you already hold rather than minting a new one each time.
BASE="https://api.skillsafe.ai/v1/app-api"
TOKEN="${SKILLSAFE_TOKEN:-YOUR_TOKEN}" # see below
# api METHOD PATH [extra curl args...] -- prints the whole envelope; pipe to jq
api() {
method="$1"; path="$2"; shift 2
curl -sS -X "$method" "$BASE$path" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" "$@"
}
# success: jq '.data' failure: jq '.error | {code, message}'
# ---- get a token
# personal token: export SKILLSAFE_TOKEN=... (from /tokens.html) and skip this
# guest token: no Authorization header on this one call
TOKEN=$(curl -sS -X POST "$BASE/guest" \
-H "Content-Type: application/json" \
-d '{"slug":"trace-desk"}' | jq -r '.data.token')
echo "guest token: ${TOKEN:0:12}..."
import hashlib, json, os, time, urllib.parse
import requests # pip install requests
BASE = "https://api.skillsafe.ai/v1/app-api"
TOKEN = os.environ.get("SKILLSAFE_TOKEN", "YOUR_TOKEN") # see below
class ApiError(Exception):
def __init__(self, status, code, message, details=None):
super().__init__(f"{status} {code}: {message}")
self.status, self.code, self.details = status, code, details
def api(method, path, body=None, headers=None):
h = {"Content-Type": "application/json"}
if TOKEN:
h["Authorization"] = "Bearer " + TOKEN
h.update(headers or {})
r = requests.request(method, BASE + path, headers=h,
data=None if body is None else json.dumps(body))
try:
env = r.json()
except ValueError:
env = {}
if not r.ok:
err = env.get("error") or {}
raise ApiError(r.status_code, err.get("code"), err.get("message") or r.reason, err.get("details"))
return env.get("data")
# ---- get a token
# personal token: set SKILLSAFE_TOKEN (from /tokens.html) and skip this
if TOKEN in ("", "YOUR_TOKEN"):
TOKEN = "" # no Authorization header on /guest
guest = api("POST", "/guest", {"slug": "trace-desk"})
TOKEN = guest["token"]
print("guest", guest.get("guest_id"))
// diagnose.mjs -- Node 18+ (global fetch), run with: node diagnose.mjs
import { readFileSync } from "node:fs";
import { createHash } from "node:crypto";
const BASE = "https://api.skillsafe.ai/v1/app-api";
let TOKEN = "YOUR_TOKEN"; // see below
async function api(method, path, body, headers = {}) {
const h = { "Content-Type": "application/json", ...headers };
if (TOKEN) h.Authorization = "Bearer " + TOKEN;
const res = await fetch(BASE + path, {
method,
headers: h,
body: body === undefined ? undefined : JSON.stringify(body),
});
const json = await res.json().catch(() => ({}));
if (!res.ok) {
const err = new Error((json.error && json.error.message) || res.statusText);
err.status = res.status;
err.code = json.error && json.error.code;
err.details = json.error && json.error.details;
throw err;
}
return json.data;
}
// ---- get a token
// personal token: paste it from /tokens.html into TOKEN and skip this
if (!TOKEN || TOKEN === "YOUR_TOKEN") {
TOKEN = ""; // no Authorization header on /guest
const guest = await api("POST", "/guest", { slug: "trace-desk" });
TOKEN = guest.token;
console.log("guest", guest.guest_id);
}
package main
import (
"bufio"
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"
"strings"
"time"
)
const base = "https://api.skillsafe.ai/v1/app-api"
var token = os.Getenv("SKILLSAFE_TOKEN") // see below
type apiError struct {
Status int
Code string
Message string
}
func (e *apiError) Error() string { return fmt.Sprintf("%d %s: %s", e.Status, e.Code, e.Message) }
type envelope struct {
OK bool `json:"ok"`
Data json.RawMessage `json:"data"`
Error *struct {
Code string `json:"code"`
Message string `json:"message"`
} `json:"error"`
}
func newRequest(method, path string, body any, headers map[string]string) (*http.Request, error) {
var buf []byte
if body != nil {
b, err := json.Marshal(body)
if err != nil {
return nil, err
}
buf = b
}
req, err := http.NewRequest(method, base+path, bytes.NewReader(buf))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
if token != "" {
req.Header.Set("Authorization", "Bearer "+token)
}
for k, v := range headers {
req.Header.Set(k, v)
}
return req, nil
}
func errorFrom(res *http.Response, env envelope) error {
e := &apiError{Status: res.StatusCode, Message: http.StatusText(res.StatusCode)}
if env.Error != nil {
e.Code = env.Error.Code
if env.Error.Message != "" {
e.Message = env.Error.Message
}
}
return e
}
// api sends body as JSON and decodes the envelope's data into out (out may be nil).
func api(method, path string, body any, headers map[string]string, out any) error {
req, err := newRequest(method, path, body, headers)
if err != nil {
return err
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
var env envelope
_ = json.NewDecoder(res.Body).Decode(&env)
if res.StatusCode < 200 || res.StatusCode > 299 {
return errorFrom(res, env)
}
if out != nil && len(env.Data) > 0 {
return json.Unmarshal(env.Data, out)
}
return nil
}
func main() {
// the token code below and steps 2-5 go here; remove any import a step you skip no longer uses
_, _, _, _, _ = bufio.NewScanner, sha256.Sum256, hex.EncodeToString, url.PathEscape, strings.Index
_ = time.Second
}
// ---- inside main: get a token
// personal token: set SKILLSAFE_TOKEN (from /tokens.html) and skip this
if token == "" {
var guest struct {
Token string `json:"token"`
GuestID string `json:"guest_id"`
}
if err := api("POST", "/guest", map[string]string{"slug": "trace-desk"}, nil, &guest); err != nil {
panic(err)
}
token = guest.Token
fmt.Println("guest", guest.GuestID)
}
// TraceDesk.java -- Java 17+, needs com.fasterxml.jackson.core:jackson-databind on the classpath
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class TraceDesk {
static final String BASE = "https://api.skillsafe.ai/v1/app-api";
static String token = System.getenv().getOrDefault("SKILLSAFE_TOKEN", "YOUR_TOKEN"); // see below
static final HttpClient HTTP = HttpClient.newHttpClient();
static final ObjectMapper JSON = new ObjectMapper();
static class ApiException extends Exception {
final int status;
final String code;
ApiException(int status, String code, String message) {
super(status + " " + code + ": " + message);
this.status = status;
this.code = code;
}
}
static HttpRequest.Builder request(String method, String path, Object body, Map<String, String> headers) throws Exception {
HttpRequest.Builder b = HttpRequest.newBuilder(URI.create(BASE + path))
.header("Content-Type", "application/json")
.method(method, body == null ? HttpRequest.BodyPublishers.noBody()
: HttpRequest.BodyPublishers.ofString(JSON.writeValueAsString(body)));
if (token != null && !token.isEmpty()) b.header("Authorization", "Bearer " + token);
if (headers != null) headers.forEach(b::header);
return b;
}
static ApiException errorFrom(int status, JsonNode env) {
JsonNode err = env.path("error");
return new ApiException(status, err.path("code").asText(null), err.path("message").asText("HTTP " + status));
}
static JsonNode api(String method, String path, Object body, Map<String, String> headers) throws Exception {
HttpResponse<String> res = HTTP.send(request(method, path, body, headers).build(), HttpResponse.BodyHandlers.ofString());
JsonNode env;
try { env = JSON.readTree(res.body()); } catch (Exception e) { env = JSON.createObjectNode(); }
if (env == null) env = JSON.createObjectNode();
if (res.statusCode() / 100 != 2) throw errorFrom(res.statusCode(), env);
return env.path("data");
}
public static void main(String[] args) throws Exception {
// the token code below and steps 2-5 go here
}
}
// ---- inside main: get a token
// personal token: set SKILLSAFE_TOKEN (from /tokens.html) and skip this
if (token.isEmpty() || token.equals("YOUR_TOKEN")) {
token = ""; // no Authorization header on /guest
JsonNode guest = api("POST", "/guest", Map.of("slug", "trace-desk"), null);
token = guest.path("token").asText();
System.out.println("guest " + guest.path("guest_id").asText());
}
require "digest"
require "json"
require "net/http"
require "uri"
BASE = "https://api.skillsafe.ai/v1/app-api"
$token = ENV.fetch("SKILLSAFE_TOKEN", "YOUR_TOKEN") # see below
class ApiError < StandardError
attr_reader :status, :code
def initialize(status, code, message)
super("#{status} #{code}: #{message}")
@status = status
@code = code
end
end
def build_request(method, path, body = nil, headers = {})
uri = URI(BASE + path)
req = Net::HTTPGenericRequest.new(method, !body.nil?, true, uri.request_uri)
req["Content-Type"] = "application/json"
req["Authorization"] = "Bearer #{$token}" if $token && !$token.empty?
headers.each { |k, v| req[k] = v }
req.body = JSON.generate(body) unless body.nil?
[uri, req]
end
def api(method, path, body = nil, headers = {})
uri, req = build_request(method, path, body, headers)
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) { |h| h.request(req) }
env = (JSON.parse(res.body) rescue {})
env = {} unless env.is_a?(Hash)
unless res.is_a?(Net::HTTPSuccess)
err = env["error"] || {}
raise ApiError.new(res.code.to_i, err["code"], err["message"] || res.message)
end
env["data"]
end
# ---- get a token
# personal token: set SKILLSAFE_TOKEN (from /tokens.html) and skip this
if $token.empty? || $token == "YOUR_TOKEN"
$token = "" # no Authorization header on /guest
guest = api("POST", "/guest", { slug: "trace-desk" })
$token = guest["token"]
puts "guest #{guest["guest_id"]}"
end
<?php
// needs the curl extension
const BASE = "https://api.skillsafe.ai/v1/app-api";
$TOKEN = getenv("SKILLSAFE_TOKEN") ?: "YOUR_TOKEN"; // see below
class ApiError extends Exception {
public $status;
public $apiCode;
public function __construct($status, $code, $message) {
parent::__construct("$status $code: $message");
$this->status = $status;
$this->apiCode = $code;
}
}
function headers_for(array $extra = []): array {
global $TOKEN;
$h = ["Content-Type: application/json"];
if ($TOKEN) $h[] = "Authorization: Bearer $TOKEN";
foreach ($extra as $k => $v) $h[] = "$k: $v";
return $h;
}
function api(string $method, string $path, $body = null, array $headers = []) {
$ch = curl_init(BASE . $path);
curl_setopt_array($ch, [
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_HTTPHEADER => headers_for($headers),
CURLOPT_RETURNTRANSFER => true,
]);
if ($body !== null) curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
$raw = curl_exec($ch);
if ($raw === false) throw new RuntimeException(curl_error($ch));
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
curl_close($ch);
$env = json_decode($raw);
if (!is_object($env)) $env = new stdClass();
if ($status < 200 || $status > 299) {
$e = $env->error ?? new stdClass();
throw new ApiError($status, $e->code ?? null, $e->message ?? "HTTP $status");
}
return $env->data ?? null;
}
// ---- get a token
// personal token: set SKILLSAFE_TOKEN (from /tokens.html) and skip this
if ($TOKEN === "" || $TOKEN === "YOUR_TOKEN") {
$TOKEN = ""; // no Authorization header on /guest
$guest = api("POST", "/guest", ["slug" => "trace-desk"]);
$TOKEN = $guest->token;
echo "guest {$guest->guest_id}\n";
}
// Program.cs -- .NET 6+ console app with top-level statements; the token code and steps 2-5 go ABOVE this class
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
static class Api
{
public const string Base = "https://api.skillsafe.ai/v1/app-api";
public static string Token = Environment.GetEnvironmentVariable("SKILLSAFE_TOKEN") ?? "YOUR_TOKEN"; // see below
public static readonly HttpClient Http = new HttpClient();
public class ApiException : Exception
{
public int Status;
public string? Code;
public ApiException(int status, string? code, string message) : base($"{status} {code}: {message}")
{
Status = status;
Code = code;
}
}
public static HttpRequestMessage Request(string method, string path, object? body = null, IDictionary<string, string>? headers = null)
{
var req = new HttpRequestMessage(new HttpMethod(method), Base + path);
if (body != null) req.Content = new StringContent(JsonSerializer.Serialize(body), Encoding.UTF8, "application/json");
if (!string.IsNullOrEmpty(Token)) req.Headers.TryAddWithoutValidation("Authorization", "Bearer " + Token);
if (headers != null) foreach (var kv in headers) req.Headers.TryAddWithoutValidation(kv.Key, kv.Value);
return req;
}
public static ApiException ErrorFrom(HttpResponseMessage res, JsonNode? env)
{
var err = env?["error"];
return new ApiException((int)res.StatusCode, err?["code"]?.GetValue<string>(),
err?["message"]?.GetValue<string>() ?? res.ReasonPhrase ?? "error");
}
public static async Task<JsonNode?> Call(string method, string path, object? body = null, IDictionary<string, string>? headers = null)
{
using var req = Request(method, path, body, headers);
using var res = await Http.SendAsync(req);
var text = await res.Content.ReadAsStringAsync();
JsonNode? env = null;
try { env = JsonNode.Parse(text); } catch (JsonException) { }
if (!res.IsSuccessStatusCode) throw ErrorFrom(res, env);
return env?["data"];
}
}
// ---- top-level statements (above the Api class): get a token
// personal token: set SKILLSAFE_TOKEN (from /tokens.html) and skip this
if (Api.Token == "" || Api.Token == "YOUR_TOKEN")
{
Api.Token = ""; // no Authorization header on /guest
var guest = await Api.Call("POST", "/guest", new { slug = "trace-desk" });
Api.Token = guest!["token"]!.GetValue<string>();
Console.WriteLine($"guest {guest["guest_id"]}");
}
Step 2 — GET /me
Who the token belongs to and what it can spend. sdk.js documents the shape as
{subject_type, subject_id, credits, profile?}. The page treats the caller as signed
in only when subject_type is "user", shows credits as the
balance (1 credit = $0.0001, so it divides by 10 000 for the dollar figure), and compares it
with the estimate in step 3 before it lets you run.
api GET /me | jq '.data // .error'
me = api("GET", "/me")
print(me["subject_type"], me.get("subject_id"), me.get("credits"))
signed_in = me["subject_type"] == "user"
const me = await api("GET", "/me");
console.log(me.subject_type, me.subject_id, me.credits);
const signedIn = me.subject_type === "user";
var me struct {
SubjectType string `json:"subject_type"`
SubjectID any `json:"subject_id"`
Credits any `json:"credits"`
}
if err := api("GET", "/me", nil, nil, &me); err != nil {
panic(err)
}
fmt.Println(me.SubjectType, me.SubjectID, me.Credits)
signedIn := me.SubjectType == "user"
_ = signedIn
JsonNode me = api("GET", "/me", null, null);
System.out.println(me.path("subject_type").asText() + " " + me.path("subject_id").asText() + " " + me.path("credits"));
boolean signedIn = me.path("subject_type").asText().equals("user");
me = api("GET", "/me")
puts [me["subject_type"], me["subject_id"], me["credits"]].join(" ")
signed_in = me["subject_type"] == "user"
$me = api("GET", "/me");
echo $me->subject_type, " ", $me->subject_id ?? "", " ", $me->credits ?? "", "\n";
$signedIn = $me->subject_type === "user";
var me = await Api.Call("GET", "/me");
Console.WriteLine($"{me?["subject_type"]} {me?["subject_id"]} {me?["credits"]}");
var signedIn = me?["subject_type"]?.GetValue<string>() == "user";
Step 3 — POST /estimate
Price the diagnosis before you run it. The body is the same input object you will send to
/run (task, facts as a string, optional
note); no job is created and nothing is charged. From the response the page reads:
| Field | Meaning |
|---|---|
hold_credits | A reservation, not a price: the worst case the platform holds while the run is in flight. The page shows it as “reserves N credits”. What you actually pay is the job’s charged_credits (step 4), normally less; the rest of the hold is released. A balance below the hold is allowed but the reply may be cut short (truncated). |
min_credits | The smallest balance that can start a run. Below it the page disables the run button, because the call would be a 402. |
sponsor_enabled | When true, a caller who is not signed in may run the diagnosis; otherwise the page asks them to sign in. |
warnings | Input warnings (for example a missing required field). They do not refuse the call, so check them before you run. |
The page re-prices whenever the trace or the note changes. The amounts move with the model’s
rates and grow with the size of facts, so read them from the call rather than
hard-coding them.
# facts.json = the facts object from the worked example; jq -c turns it into ONE string
BODY=$(jq -n --arg facts "$(jq -c . facts.json)" --arg q "Search takes over two seconds for common terms. There is a btree index on products.title." \
'{task: "diagnose", facts: $facts, note: $q}')
api POST /estimate -d "$BODY" \
| jq '.error // (.data | {hold_credits, min_credits, sponsor_enabled, warnings, model, model_alias, markup_bps})'
with open("facts.json", encoding="utf-8") as f:
facts = json.load(f)
inp = {
"task": "diagnose",
"facts": json.dumps(facts, separators=(",", ":"), ensure_ascii=False), # a STRING, like JSON.stringify
"note": "Search takes over two seconds for common terms. There is a btree index on products.title.", # optional; the page trims it and caps it at 2000 chars
}
est = api("POST", "/estimate", inp)
print("hold", est.get("hold_credits"), "min", est.get("min_credits"),
"sponsored", est.get("sponsor_enabled"), "warnings", est.get("warnings") or [])
const facts = JSON.parse(readFileSync("facts.json", "utf8"));
const input = {
task: "diagnose",
facts: JSON.stringify(facts), // a STRING, exactly as the page sends it
note: "Search takes over two seconds for common terms. There is a btree index on products.title.", // optional; the page trims it and caps it at 2000 chars
};
const est = await api("POST", "/estimate", input);
console.log("hold", est.hold_credits, "min", est.min_credits,
"sponsored", est.sponsor_enabled, "warnings", est.warnings || []);
raw, err := os.ReadFile("facts.json")
if err != nil {
panic(err)
}
var facts any
if err := json.Unmarshal(raw, &facts); err != nil {
panic(err)
}
factsJSON, _ := json.Marshal(facts) // compact; travels as a STRING
input := map[string]any{
"task": "diagnose",
"facts": string(factsJSON),
"note": "Search takes over two seconds for common terms. There is a btree index on products.title.", // optional; the page trims it and caps it at 2000 chars
}
var est struct {
HoldCredits any `json:"hold_credits"`
MinCredits any `json:"min_credits"`
SponsorEnabled bool `json:"sponsor_enabled"`
Warnings []any `json:"warnings"`
}
if err := api("POST", "/estimate", input, nil, &est); err != nil {
panic(err)
}
fmt.Printf("hold %v min %v sponsored %v warnings %v\n", est.HoldCredits, est.MinCredits, est.SponsorEnabled, est.Warnings)
JsonNode facts = JSON.readTree(Files.readString(Path.of("facts.json")));
Map<String, Object> input = new LinkedHashMap<>();
input.put("task", "diagnose");
input.put("facts", JSON.writeValueAsString(facts)); // a STRING, compact
input.put("note", "Search takes over two seconds for common terms. There is a btree index on products.title."); // optional; the page trims it and caps it at 2000 chars
JsonNode est = api("POST", "/estimate", input, null);
System.out.println("hold " + est.path("hold_credits") + " min " + est.path("min_credits")
+ " sponsored " + est.path("sponsor_enabled").asBoolean(false) + " warnings " + est.path("warnings"));
facts = JSON.parse(File.read("facts.json", encoding: "UTF-8"))
input = {
task: "diagnose",
facts: JSON.generate(facts), # a STRING, compact
note: "Search takes over two seconds for common terms. There is a btree index on products.title." # optional; the page trims it and caps it at 2000 chars
}
est = api("POST", "/estimate", input)
puts "hold #{est["hold_credits"]} min #{est["min_credits"]} " \
"sponsored #{est["sponsor_enabled"]} warnings #{(est["warnings"] || []).inspect}"
// decode to objects (not arrays) so an empty {} stays {} when re-encoded
$facts = json_decode(file_get_contents("facts.json"));
$input = [
"task" => "diagnose",
"facts" => json_encode($facts, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), // a STRING
"note" => "Search takes over two seconds for common terms. There is a btree index on products.title.", // optional; the page trims it and caps it at 2000 chars
];
$est = api("POST", "/estimate", $input);
echo "hold ", $est->hold_credits ?? "-", " min ", $est->min_credits ?? "-",
" sponsored ", json_encode($est->sponsor_enabled ?? false),
" warnings ", json_encode($est->warnings ?? []), "\n";
var facts = JsonNode.Parse(File.ReadAllText("facts.json"))!;
var input = new Dictionary<string, string>
{
["task"] = "diagnose",
["facts"] = facts.ToJsonString(), // a STRING, compact
["note"] = "Search takes over two seconds for common terms. There is a btree index on products.title.", // optional; the page trims it and caps it at 2000 chars
};
var est = await Api.Call("POST", "/estimate", input);
Console.WriteLine($"hold {est?["hold_credits"]} min {est?["min_credits"]} " +
$"sponsored {est?["sponsor_enabled"]} warnings {est?["warnings"]?.ToJsonString() ?? "[]"}");
Step 4 — POST /run, then poll the job
Send the same input to /run with an Idempotency-Key header. Its
data carries a job_id. Poll GET /jobs/{job_id} the way
sdk.js waitForJob does: every second until status is
succeeded or failed, giving up on the client side after 180 seconds.
On the finished job, read:
output.output— the model’s reply as text: the JSON object from the output contract. Parse it yourself. The samples print the pattern and headline, each bottleneck’s span and share, and each fix’s change and expected gain.truncated—truewhen the reply was cut short. The page then closes the cut-off JSON, shows what arrived as a partial result (“N of 11 sections recovered”), and says it was cut short by the available balance.charged_credits— the real cost of the run, as opposed to the estimate’shold_creditsreservation. The page re-reads/meafterwards for the new balance.error— on afailedjob.
Idempotency key. Resending a request with the same key must not start a second
paid run, so derive the key from the input, not from the clock. The page uses
trace-desk:diagnose:<hash>:a<attempt>, where <hash> is
a 32-bit djb2 hash of JSON.stringify(input) in hex, a dash, and the string’s
length in hex, and <attempt> is 1 for a normal run and
2 for the automatic reformat retry (which also adds retry_note). For
the worked example that is trace-desk:diagnose:91419689-5d3:a1. The format is the
page’s convention, not a requirement: any stable, unique string does. The samples below
use a SHA-256 prefix.
The page itself uses /run-stream (step 5), not /run; both take the
same body and header.
KEY="trace-desk:diagnose:$(printf '%s' "$BODY" | shasum -a 256 | cut -c1-16):a1"
JOB=$(api POST /run -H "Idempotency-Key: $KEY" -d "$BODY" | jq -r '.data.job_id')
for i in $(seq 180); do # 1 s interval, 180 s client-side limit
J=$(api GET "/jobs/$JOB")
S=$(printf '%s' "$J" | jq -r '.data.status')
if [ "$S" = "succeeded" ] || [ "$S" = "failed" ]; then break; fi
sleep 1
done
printf '%s' "$J" | jq '.data | {status, truncated, charged_credits, error}'
printf '%s' "$J" | jq -r '.data.output.output' | jq '{pattern, headline, bottlenecks: [(.bottlenecks // [])[] | {span, share_pct}], fixes: [(.fixes // [])[] | {change, expected_gain_ms}]}'
body = json.dumps(inp)
key = "trace-desk:diagnose:" + hashlib.sha256(body.encode()).hexdigest()[:16] + ":a1"
job_id = api("POST", "/run", inp, {"Idempotency-Key": key})["job_id"]
deadline = time.time() + 180 # 1 s interval, 180 s client-side limit
while True:
job = api("GET", "/jobs/" + urllib.parse.quote(job_id, safe=""))
if job["status"] in ("succeeded", "failed"):
break
if time.time() > deadline:
raise TimeoutError("job timed out")
time.sleep(1)
if job["status"] == "failed":
raise SystemExit(f"run failed: {job.get('error')}")
print("truncated:", job.get("truncated") is True, "charged:", job.get("charged_credits"))
text = (job.get("output") or {}).get("output") or ""
reply = json.loads(text[text.index("{"): text.rindex("}") + 1])
print(reply["pattern"], "-", reply["headline"])
for b in reply.get("bottlenecks", []):
print(b["span"], b["share_pct"], "%")
for fx in reply.get("fixes", []):
print(fx["change"], "-", fx["expected_gain_ms"], "ms")
const key = "trace-desk:diagnose:" +
createHash("sha256").update(JSON.stringify(input)).digest("hex").slice(0, 16) + ":a1";
const { job_id } = await api("POST", "/run", input, { "Idempotency-Key": key });
let job;
const deadline = Date.now() + 180000; // 1 s interval, 180 s client-side limit
for (;;) {
job = await api("GET", "/jobs/" + encodeURIComponent(job_id));
if (job.status === "succeeded" || job.status === "failed") break;
if (Date.now() > deadline) throw new Error("job timed out");
await new Promise((r) => setTimeout(r, 1000));
}
if (job.status === "failed") throw new Error("run failed: " + JSON.stringify(job.error));
console.log("truncated:", job.truncated === true, "charged:", job.charged_credits);
const text = (job.output && job.output.output) || "";
const reply = JSON.parse(text.slice(text.indexOf("{"), text.lastIndexOf("}") + 1));
console.log(reply.pattern, "-", reply.headline);
for (const b of reply.bottlenecks || []) console.log(b.span, b.share_pct, "%");
for (const fx of reply.fixes || []) console.log(fx.change, "-", fx.expected_gain_ms, "ms");
inputJSON, _ := json.Marshal(input)
sum := sha256.Sum256(inputJSON)
key := "trace-desk:diagnose:" + hex.EncodeToString(sum[:8]) + ":a1"
var started struct {
JobID string `json:"job_id"`
}
if err := api("POST", "/run", input, map[string]string{"Idempotency-Key": key}, &started); err != nil {
panic(err)
}
type jobT struct {
Status string `json:"status"`
Output *struct {
Output string `json:"output"`
} `json:"output"`
Truncated bool `json:"truncated"`
ChargedCredits any `json:"charged_credits"`
Error any `json:"error"`
}
var job jobT
deadline := time.Now().Add(180 * time.Second) // 1 s interval, 180 s client-side limit
for {
job = jobT{}
if err := api("GET", "/jobs/"+url.PathEscape(started.JobID), nil, nil, &job); err != nil {
panic(err)
}
if job.Status == "succeeded" || job.Status == "failed" {
break
}
if time.Now().After(deadline) {
panic("job timed out")
}
time.Sleep(time.Second)
}
if job.Status == "failed" {
panic(fmt.Sprintf("run failed: %v", job.Error))
}
fmt.Println("truncated:", job.Truncated, "charged:", job.ChargedCredits)
text := ""
if job.Output != nil {
text = job.Output.Output
}
var reply map[string]any
if a, b := strings.Index(text, "{"), strings.LastIndex(text, "}"); a >= 0 && b > a {
if err := json.Unmarshal([]byte(text[a:b+1]), &reply); err != nil {
panic(err)
}
}
fmt.Println(reply["pattern"], "-", reply["headline"])
bottlenecks, _ := reply["bottlenecks"].([]any)
for _, x := range bottlenecks {
b, _ := x.(map[string]any)
fmt.Println(b["span"], b["share_pct"], "%")
}
fixes, _ := reply["fixes"].([]any)
for _, x := range fixes {
fx, _ := x.(map[string]any)
fmt.Println(fx["change"], "-", fx["expected_gain_ms"], "ms")
}
byte[] digest = MessageDigest.getInstance("SHA-256").digest(JSON.writeValueAsBytes(input));
String key = "trace-desk:diagnose:" + HexFormat.of().formatHex(digest, 0, 8) + ":a1";
String jobId = api("POST", "/run", input, Map.of("Idempotency-Key", key)).path("job_id").asText();
JsonNode job;
long deadline = System.currentTimeMillis() + 180_000; // 1 s interval, 180 s client-side limit
while (true) {
job = api("GET", "/jobs/" + URLEncoder.encode(jobId, StandardCharsets.UTF_8), null, null);
String s = job.path("status").asText();
if (s.equals("succeeded") || s.equals("failed")) break;
if (System.currentTimeMillis() > deadline) throw new RuntimeException("job timed out");
Thread.sleep(1000);
}
if (job.path("status").asText().equals("failed")) throw new RuntimeException("run failed: " + job.path("error"));
System.out.println("truncated: " + job.path("truncated").asBoolean(false) + ", charged: " + job.path("charged_credits"));
String text = job.path("output").path("output").asText("");
JsonNode reply = JSON.readTree(text.substring(text.indexOf('{'), text.lastIndexOf('}') + 1));
System.out.println(reply.path("pattern").asText() + " - " + reply.path("headline").asText());
for (JsonNode b : reply.path("bottlenecks")) {
System.out.println(b.path("span").asText() + " " + b.path("share_pct") + " %");
}
for (JsonNode fx : reply.path("fixes")) {
System.out.println(fx.path("change").asText() + " - " + fx.path("expected_gain_ms") + " ms");
}
key = "trace-desk:diagnose:#{Digest::SHA256.hexdigest(JSON.generate(input))[0, 16]}:a1"
job_id = api("POST", "/run", input, { "Idempotency-Key" => key })["job_id"]
deadline = Time.now + 180 # 1 s interval, 180 s client-side limit
job = nil
loop do
job = api("GET", "/jobs/#{URI.encode_www_form_component(job_id)}")
break if %w[succeeded failed].include?(job["status"])
raise "job timed out" if Time.now > deadline
sleep 1
end
raise "run failed: #{job["error"].inspect}" if job["status"] == "failed"
puts "truncated: #{job["truncated"] == true}, charged: #{job["charged_credits"]}"
text = (job["output"] || {})["output"].to_s
reply = JSON.parse(text[text.index("{")..text.rindex("}")])
puts "#{reply["pattern"]} - #{reply["headline"]}"
(reply["bottlenecks"] || []).each { |b| puts "#{b["span"]} #{b["share_pct"]} %" }
(reply["fixes"] || []).each { |fx| puts "#{fx["change"]} - #{fx["expected_gain_ms"].inspect} ms" }
$key = "trace-desk:diagnose:" . substr(hash("sha256", json_encode($input)), 0, 16) . ":a1";
$jobId = api("POST", "/run", $input, ["Idempotency-Key" => $key])->job_id;
$deadline = time() + 180; // 1 s interval, 180 s client-side limit
while (true) {
$job = api("GET", "/jobs/" . rawurlencode($jobId));
if (in_array($job->status, ["succeeded", "failed"], true)) break;
if (time() > $deadline) throw new RuntimeException("job timed out");
sleep(1);
}
if ($job->status === "failed") throw new RuntimeException("run failed: " . json_encode($job->error ?? null));
echo "truncated: ", json_encode(($job->truncated ?? false) === true), ", charged: ", $job->charged_credits ?? "-", "\n";
$text = $job->output->output ?? "";
$reply = json_decode(substr($text, strpos($text, "{"), strrpos($text, "}") - strpos($text, "{") + 1));
echo $reply->pattern, " - ", $reply->headline, "\n";
foreach ($reply->bottlenecks ?? [] as $b) echo $b->span, " ", $b->share_pct, " %\n";
foreach ($reply->fixes ?? [] as $fx) echo $fx->change, " - ", json_encode($fx->expected_gain_ms ?? null), " ms\n";
var digest = System.Security.Cryptography.SHA256.HashData(Encoding.UTF8.GetBytes(JsonSerializer.Serialize(input)));
var key = "trace-desk:diagnose:" + Convert.ToHexString(digest, 0, 8).ToLowerInvariant() + ":a1";
var started = await Api.Call("POST", "/run", input, new Dictionary<string, string> { ["Idempotency-Key"] = key });
var jobId = started!["job_id"]!.GetValue<string>();
JsonNode job;
var deadline = DateTime.UtcNow.AddSeconds(180); // 1 s interval, 180 s client-side limit
while (true)
{
job = (await Api.Call("GET", "/jobs/" + Uri.EscapeDataString(jobId)))!;
var s = job["status"]?.GetValue<string>();
if (s == "succeeded" || s == "failed") break;
if (DateTime.UtcNow > deadline) throw new TimeoutException("job timed out");
await Task.Delay(1000);
}
if (job["status"]?.GetValue<string>() == "failed") throw new Exception($"run failed: {job["error"]?.ToJsonString()}");
var truncated = job["truncated"] is JsonValue t && t.TryGetValue(out bool tb) && tb;
Console.WriteLine($"truncated: {truncated}, charged: {job["charged_credits"]}");
var text = job["output"]?["output"]?.GetValue<string>() ?? "";
var reply = JsonNode.Parse(text[text.IndexOf('{')..(text.LastIndexOf('}') + 1)])!;
Console.WriteLine($"{reply["pattern"]} - {reply["headline"]}");
foreach (var b in reply["bottlenecks"]?.AsArray() ?? new JsonArray())
Console.WriteLine($"{b?["span"]} {b?["share_pct"]} %");
foreach (var fx in reply["fixes"]?.AsArray() ?? new JsonArray())
Console.WriteLine($"{fx?["change"]} - {fx?["expected_gain_ms"]?.ToJsonString() ?? "null"} ms");
Step 5 — POST /run-stream (Server-Sent Events)
The alternative to step 4, and what the page uses: same body, same
Idempotency-Key header, one request. Run one or the other for a given input, not
both. The response is text/event-stream. sdk.js splits it into frames
on a blank line, takes the event name from an event: line (default
message) and concatenates the data: lines, skips frames with no data
or with data that is not JSON, and handles these events:
| Event | Data | What the SDK does |
|---|---|---|
delta | {text} | Passes text to onDelta. |
job | job info | Passes it to onJob (the page uses it to advance its progress display). |
done | the final payload: {job_id, status, charged_credits, output} | Keeps it as the result. |
pending | a payload | Treated exactly like done. If it carries a job_id and no output, poll that job as in step 4. |
error | {message, code, job_id} | After the stream ends, raises an error with those fields. |
If the response is not text/event-stream, it is an ordinary JSON envelope:
the SDK returns its data (an idempotent replay returns this way) or raises the
error on a non-2xx status. Handle both shapes.
From the final payload the page reads output.output (falling back to the
concatenated delta text), truncated and charged_credits.
Browsers receive progress ticks rather than deltas from this endpoint, so the page relies on the
final payload; if the stream fails part-way, it tries to recover a partial reply from whatever
delta text arrived.
# -N turns off buffering so frames print as they arrive
api POST /run-stream -N -H "Idempotency-Key: $KEY" -d "$BODY"
# frames look like:
# event: job
# data: {...}
#
# event: done
# data: {"job_id":"...","status":"...","charged_credits":...,"output":{"output":"{\"task\":\"diagnose\",...}"}}
# keep only the final reply text:
api POST /run-stream -N -H "Idempotency-Key: $KEY" -d "$BODY" \
| awk '/^event:/{ev=$2} /^data:/ && ev=="done"{sub(/^data: */,""); print}' \
| jq -r '.output.output' | jq .
h = {"Content-Type": "application/json", "Authorization": "Bearer " + TOKEN, "Idempotency-Key": key}
done, failure, streamed = None, None, ""
with requests.post(BASE + "/run-stream", headers=h, data=json.dumps(inp), stream=True) as r:
if "text/event-stream" not in r.headers.get("content-type", ""):
try:
env = r.json()
except ValueError:
env = {}
if not r.ok:
err = env.get("error") or {}
raise ApiError(r.status_code, err.get("code"), err.get("message") or r.reason)
done = env.get("data")
else:
event, data = "message", ""
for line in r.iter_lines(decode_unicode=True):
if line == "": # a blank line ends a frame
if data:
try:
payload = json.loads(data)
except ValueError:
payload = None
if event == "delta" and payload is not None:
streamed += payload.get("text") or ""
elif event == "job":
print("job", payload)
elif event in ("done", "pending"):
done = payload
elif event == "error":
failure = payload
event, data = "message", ""
elif line.startswith("event:"):
event = line[6:].strip()
elif line.startswith("data:"):
data += line[5:].strip()
if failure:
raise SystemExit(f"{failure.get('code')}: {failure.get('message')} (job {failure.get('job_id')})")
done = done or {}
full = (done.get("output") or {}).get("output") or streamed
print("truncated:", done.get("truncated") is True, "charged:", done.get("charged_credits"))
print(json.loads(full[full.index("{"): full.rindex("}") + 1])["headline"])
const res = await fetch(BASE + "/run-stream", {
method: "POST",
headers: { "Content-Type": "application/json", Authorization: "Bearer " + TOKEN, "Idempotency-Key": key },
body: JSON.stringify(input),
});
let done = null, failure = null, streamed = "";
if (!(res.headers.get("content-type") || "").includes("text/event-stream")) {
const json = await res.json().catch(() => ({}));
if (!res.ok) throw new Error((json.error && json.error.message) || res.statusText);
done = json.data;
} else {
const reader = res.body.getReader();
const decoder = new TextDecoder();
let buffer = "";
for (;;) {
const chunk = await reader.read();
if (chunk.done) break;
buffer += decoder.decode(chunk.value, { stream: true });
let idx;
while ((idx = buffer.indexOf("\n\n")) >= 0) {
const frame = buffer.slice(0, idx);
buffer = buffer.slice(idx + 2);
let event = "message", dataStr = "";
for (const line of frame.split("\n")) {
if (line.startsWith("event:")) event = line.slice(6).trim();
else if (line.startsWith("data:")) dataStr += line.slice(5).trim();
}
if (!dataStr) continue;
let data;
try { data = JSON.parse(dataStr); } catch { continue; }
if (event === "delta") streamed += data.text || "";
else if (event === "job") console.log("job", data);
else if (event === "done" || event === "pending") done = data;
else if (event === "error") failure = data;
}
}
}
if (failure) throw new Error(`${failure.code}: ${failure.message} (job ${failure.job_id})`);
const full = (done && done.output && done.output.output) || streamed;
console.log("truncated:", !!(done && done.truncated), "charged:", done && done.charged_credits);
console.log(JSON.parse(full.slice(full.indexOf("{"), full.lastIndexOf("}") + 1)).headline);
sreq, err := newRequest("POST", "/run-stream", input, map[string]string{"Idempotency-Key": key})
if err != nil {
panic(err)
}
sres, err := http.DefaultClient.Do(sreq)
if err != nil {
panic(err)
}
defer sres.Body.Close()
var done, failure map[string]any
var streamed strings.Builder
if !strings.Contains(sres.Header.Get("Content-Type"), "text/event-stream") {
var env envelope
_ = json.NewDecoder(sres.Body).Decode(&env)
if sres.StatusCode < 200 || sres.StatusCode > 299 {
panic(errorFrom(sres, env))
}
_ = json.Unmarshal(env.Data, &done)
} else {
sc := bufio.NewScanner(sres.Body)
sc.Buffer(make([]byte, 64*1024), 16*1024*1024) // the done frame can be long
event, data := "message", ""
for sc.Scan() {
line := sc.Text()
switch {
case line == "": // a blank line ends a frame
if data != "" {
var p map[string]any
if json.Unmarshal([]byte(data), &p) == nil {
switch event {
case "delta":
if t, ok := p["text"].(string); ok {
streamed.WriteString(t)
}
case "job":
fmt.Println("job", p)
case "done", "pending":
done = p
case "error":
failure = p
}
}
}
event, data = "message", ""
case strings.HasPrefix(line, "event:"):
event = strings.TrimSpace(line[6:])
case strings.HasPrefix(line, "data:"):
data += strings.TrimSpace(line[5:])
}
}
}
if failure != nil {
panic(fmt.Sprintf("%v: %v (job %v)", failure["code"], failure["message"], failure["job_id"]))
}
full := streamed.String()
if out, ok := done["output"].(map[string]any); ok {
if s, ok := out["output"].(string); ok && s != "" {
full = s
}
}
fmt.Println("truncated:", done["truncated"] == true, "charged:", done["charged_credits"])
fmt.Println(full)
HttpRequest sreq = request("POST", "/run-stream", input, Map.of("Idempotency-Key", key)).build();
HttpResponse<Stream<String>> sres = HTTP.send(sreq, HttpResponse.BodyHandlers.ofLines());
JsonNode done = null, failure = null;
StringBuilder streamed = new StringBuilder();
if (!sres.headers().firstValue("content-type").orElse("").contains("text/event-stream")) {
JsonNode env = JSON.readTree(sres.body().collect(Collectors.joining("\n")));
if (env == null) env = JSON.createObjectNode();
if (sres.statusCode() / 100 != 2) throw errorFrom(sres.statusCode(), env);
done = env.path("data");
} else {
String event = "message";
StringBuilder data = new StringBuilder();
Iterator<String> it = sres.body().iterator();
while (it.hasNext()) {
String line = it.next();
if (line.isEmpty()) { // a blank line ends a frame
if (data.length() > 0) {
JsonNode p = null;
try { p = JSON.readTree(data.toString()); } catch (Exception ignored) { }
if (p != null) {
switch (event) {
case "delta": streamed.append(p.path("text").asText("")); break;
case "job": System.out.println("job " + p); break;
case "done": case "pending": done = p; break;
case "error": failure = p; break;
default: break;
}
}
}
event = "message";
data.setLength(0);
} else if (line.startsWith("event:")) event = line.substring(6).trim();
else if (line.startsWith("data:")) data.append(line.substring(5).trim());
}
}
if (failure != null) {
throw new RuntimeException(failure.path("code").asText() + ": " + failure.path("message").asText()
+ " (job " + failure.path("job_id").asText() + ")");
}
String full = done == null ? "" : done.path("output").path("output").asText("");
if (full.isEmpty()) full = streamed.toString();
System.out.println("truncated: " + (done != null && done.path("truncated").asBoolean(false))
+ ", charged: " + (done == null ? "-" : done.path("charged_credits").toString()));
System.out.println(full);
uri, req = build_request("POST", "/run-stream", input, { "Idempotency-Key" => key })
done = failure = nil
streamed = +""
Net::HTTP.start(uri.host, uri.port, use_ssl: true, read_timeout: 300) do |http|
http.request(req) do |res|
unless res["content-type"].to_s.include?("text/event-stream")
env = (JSON.parse(res.body) rescue {})
unless res.is_a?(Net::HTTPSuccess)
raise ApiError.new(res.code.to_i, env.dig("error", "code"), env.dig("error", "message") || res.message)
end
done = env["data"]
next
end
buf = +""
res.read_body do |chunk|
buf << chunk
while (i = buf.index("\n\n"))
frame = buf.slice!(0, i + 2)
event = "message"
data = +""
frame.split("\n").each do |line|
if line.start_with?("event:") then event = line[6..].strip
elsif line.start_with?("data:") then data << line[5..].strip
end
end
next if data.empty?
payload = (JSON.parse(data) rescue nil)
next if payload.nil?
case event
when "delta" then streamed << payload["text"].to_s
when "job" then puts "job #{payload.inspect}"
when "done", "pending" then done = payload
when "error" then failure = payload
end
end
end
end
end
raise "#{failure["code"]}: #{failure["message"]} (job #{failure["job_id"]})" if failure
done ||= {}
full = (done["output"] || {})["output"].to_s
full = streamed if full.empty?
puts "truncated: #{done["truncated"] == true}, charged: #{done["charged_credits"]}"
puts full
$done = null; $failure = null; $streamed = ""; $buf = ""; $ctype = "";
$handle = function (string $event, string $data) use (&$done, &$failure, &$streamed) {
$p = json_decode($data);
if ($p === null) return; // not JSON: skipped, as sdk.js does
switch ($event) {
case "delta": $streamed .= $p->text ?? ""; break;
case "job": echo "job ", json_encode($p), "\n"; break;
case "done": case "pending": $done = $p; break;
case "error": $failure = $p; break;
}
};
$ch = curl_init(BASE . "/run-stream");
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => headers_for(["Idempotency-Key" => $key]),
CURLOPT_POSTFIELDS => json_encode($input, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE),
CURLOPT_HEADERFUNCTION => function ($ch, $line) use (&$ctype) {
if (stripos($line, "content-type:") === 0) $ctype = strtolower(trim(substr($line, 13)));
return strlen($line);
},
CURLOPT_WRITEFUNCTION => function ($ch, $chunk) use (&$buf, &$ctype, $handle) {
$buf .= $chunk;
if (strpos($ctype, "text/event-stream") === false) return strlen($chunk); // plain JSON: parse at the end
while (($i = strpos($buf, "\n\n")) !== false) {
$frame = substr($buf, 0, $i);
$buf = substr($buf, $i + 2);
$event = "message"; $data = "";
foreach (explode("\n", $frame) as $line) {
if (strpos($line, "event:") === 0) $event = trim(substr($line, 6));
elseif (strpos($line, "data:") === 0) $data .= trim(substr($line, 5));
}
if ($data !== "") $handle($event, $data);
}
return strlen($chunk);
},
]);
if (curl_exec($ch) === false) throw new RuntimeException(curl_error($ch));
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
curl_close($ch);
if (strpos($ctype, "text/event-stream") === false) {
$env = json_decode($buf) ?? new stdClass();
if ($status < 200 || $status > 299) {
throw new ApiError($status, $env->error->code ?? null, $env->error->message ?? "HTTP $status");
}
$done = $env->data ?? null;
}
if ($failure) throw new RuntimeException(($failure->code ?? "") . ": " . ($failure->message ?? "") . " (job " . ($failure->job_id ?? "") . ")");
$full = $done->output->output ?? "";
if ($full === "") $full = $streamed;
echo "truncated: ", json_encode(($done->truncated ?? false) === true), ", charged: ", $done->charged_credits ?? "-", "\n";
echo $full, "\n";
using var sreq = Api.Request("POST", "/run-stream", input, new Dictionary<string, string> { ["Idempotency-Key"] = key });
using var sres = await Api.Http.SendAsync(sreq, HttpCompletionOption.ResponseHeadersRead);
JsonNode? done = null, failure = null;
var streamed = new StringBuilder();
if (sres.Content.Headers.ContentType?.MediaType != "text/event-stream")
{
JsonNode? env = null;
try { env = JsonNode.Parse(await sres.Content.ReadAsStringAsync()); } catch (JsonException) { }
if (!sres.IsSuccessStatusCode) throw Api.ErrorFrom(sres, env);
done = env?["data"];
}
else
{
using var reader = new StreamReader(await sres.Content.ReadAsStreamAsync());
var ev = "message";
var data = new StringBuilder();
string? line;
while ((line = await reader.ReadLineAsync()) != null)
{
if (line.Length == 0) // a blank line ends a frame
{
if (data.Length > 0)
{
JsonNode? p = null;
try { p = JsonNode.Parse(data.ToString()); } catch (JsonException) { }
if (p != null)
{
switch (ev)
{
case "delta": streamed.Append(p["text"]?.GetValue<string>() ?? ""); break;
case "job": Console.WriteLine($"job {p.ToJsonString()}"); break;
case "done": case "pending": done = p; break;
case "error": failure = p; break;
}
}
}
ev = "message";
data.Clear();
}
else if (line.StartsWith("event:")) ev = line[6..].Trim();
else if (line.StartsWith("data:")) data.Append(line[5..].Trim());
}
}
if (failure != null) throw new Exception($"{failure["code"]}: {failure["message"]} (job {failure["job_id"]})");
var full = done?["output"]?["output"]?.GetValue<string>();
if (string.IsNullOrEmpty(full)) full = streamed.ToString();
var cut = done?["truncated"] is JsonValue tv && tv.TryGetValue(out bool cutShort) && cutShort;
Console.WriteLine($"truncated: {cut}, charged: {done?["charged_credits"]}");
Console.WriteLine(full);
What the page does that you now do
- Parses the export in the browser and builds
factsfrom it (tracekit.js); you build the same object and stringify it. - Prices every input with
/estimateand refuses to run belowmin_credits. - Sends each attempt with an input-derived
Idempotency-Key, and retries once withretry_notewhen a reply does not parse. - Closes a cut-off JSON reply and shows what arrived when
truncatedis true or the stream ends early. - Reconciles the reply against the span tree (
recon.js): span ids, shares, gains, the error origin and the high flags.
Or skip all of this and use Trace Desk in the browser: the span tree, the waterfall, the critical path, the flags and the span CSV are free.