Asynchronous chat jobs
Create a job when a generation may outlast an HTTP request. Creation returns an identifier immediately. Reading status or results neither starts another generation nor charges credits again.
All routes require Authorization: Bearer km_your_token_here. Only the valid API token that created the job can access it, including when another token belongs to the same team.
Routes
| Method and path | Response |
|---|---|
POST /v1/chat/jobs | 202, job metadata, status URL in Location and Retry-After: 2 |
GET /v1/chat/jobs/:id | 200, current metadata and Retry-After: 2 |
GET /v1/chat/jobs/:id/result | 200, complete chat completion or tool-approval response |
GET /v1/chat/jobs/:id/stream | 200, completion SSE chunks, heartbeat comments and data: [DONE] |
POST /v1/chat/jobs/:id/cancel | 202, id and status: "cancelling", or already-cancelled state |
POST /v1/chat/jobs/:id/resume | 202, a new job after tool approval |
:id must be a UUID. Reads and cancellation have no request body.
Creation and idempotency
Creation accepts the fields and limits of chat completions. Omit stream or set it to false. stream: true and keep_alive: true are rejected on asynchronous creation.
curl -i https://api.kitemesh.com/v1/chat/jobs \
-H 'Authorization: Bearer km_your_token_here' \
-H 'Content-Type: application/json' \
-H 'Idempotency-Key: summary-001' \
-d '{"model":"custom:123e4567-e89b-12d3-a456-426614174000","messages":[{"role":"user","content":"Summarize this document."}]}'Example response, with other metadata omitted:
{
"id": "123e4567-e89b-12d3-a456-426614174001",
"status": "queued",
"status_url": "/v1/chat/jobs/123e4567-e89b-12d3-a456-426614174001",
"result_url": "/v1/chat/jobs/123e4567-e89b-12d3-a456-426614174001/result"
}The optional Idempotency-Key header accepts 1–200 characters. The same token, key and semantic request return the existing job, which may already be finished. Different generation parameters return 409 idempotency_key_conflict. Without this header, a retry can create another generation.
This header also applies to synchronous completions, SSE completions and resumes. Delivery mode does not distinguish semantic requests.
Status and result
| Metadata field | Type | Meaning |
|---|---|---|
id | string | job UUID |
status | string | one of the states below |
model | string | requested custom:<uuid> or group:<uuid> |
request_id | string | generation correlation identifier |
created_at | string | ISO 8601 creation time |
deadline_at | string | ISO 8601 processing deadline, including queue time |
result_expires_at | string or null | ISO 8601 retention deadline once available |
error | object or null | stable code and public message on failure |
| State | Client action |
|---|---|
queued, running, finalizing | continue polling; the result is not yet available |
awaiting_tool_approval | retrieve /result and inspect tool_calls |
completed | retrieve /result |
cancelling | wait for cancellation to finish |
cancelled, failed, timeout | stop polling and inspect the error |
Default processing time is 30 minutes, queue time included. Results and replayable events are retained for 24 hours after termination by default. Deployments can configure these limits. Tool approval has its own kitemesh.expires_at deadline.
/result returns 409 job_not_ready while processing, 409 job_failed for failed/cancelled/timed-out jobs, and 410 result_expired after retention while metadata still exists. Unknown or inaccessible IDs return 404.
Result reads preserve completion identifiers, creation time, usage and charged credits. A resume is a new execution with its own usage.
Streaming, approval and cancellation
Attach to GET /v1/chat/jobs/:id/stream to receive SSE without creating another generation. Reconnect with the optional Last-Event-ID header, using the last received text event's id:. It must be a nonnegative safe integer. Ignore heartbeat comments such as : heartbeat, normally sent every 15 seconds. Closing this stream does not cancel the job.
To approve tools, send the fields required by chat resume, including model, messages, approval_id and decisions for all pending tools, to POST /v1/chat/jobs/:id/resume. Omit stream or set it to false; omit keep_alive. The response identifies a new job. An approval can be consumed only once. The previous job becomes completed and retains its historical approval response.
A job created on /v1/chat/jobs continues without an attached client. Use /cancel explicitly. Work and external effects already performed are not undone. A completed, failed or finalizing job returns 409 on cancellation.
Default admission limits count queued work: 100 active jobs globally and 10 for one token. Reads and cancellation have a separate default limit of 600 requests per 60 seconds per token/IP. Open completion streams also have connection limits. Handle 429 with backoff.
Server-side TypeScript
Keep the key on your server. Handle tool approval before executing any returned action.
const base = 'https://api.kitemesh.com';
const headers = { Authorization: 'Bearer ' + process.env.KITEMESH_API_KEY };
const created = await fetch(base + '/v1/chat/jobs', {
method: 'POST',
headers: { ...headers, 'Content-Type': 'application/json', 'Idempotency-Key': 'summary-001' },
body: JSON.stringify({
model: 'custom:123e4567-e89b-12d3-a456-426614174000',
messages: [{ role: 'user', content: 'Summarize this document.' }],
}),
});
if (!created.ok) throw new Error(await created.text());
const job = await created.json() as { id: string };
const stopAt = Date.now() + 31 * 60_000;
while (Date.now() < stopAt) {
const response = await fetch(base + '/v1/chat/jobs/' + job.id, { headers });
if (!response.ok) throw new Error(await response.text());
const state = await response.json() as { status: string };
if (['completed', 'awaiting_tool_approval'].includes(state.status)) {
const result = await fetch(base + '/v1/chat/jobs/' + job.id + '/result', { headers });
if (!result.ok) throw new Error(await result.text());
console.log(await result.json());
break;
}
if (['failed', 'cancelled', 'timeout'].includes(state.status)) throw new Error(state.status);
await new Promise(resolve => setTimeout(resolve, 2000));
}