What you will have
- A script that creates a Session on a computer you own, or in the cloud if none is connected.
- A printed transcript of the agent’s replies, read from
/events. - A follow-up message in the same Session.
Prerequisites
See Authentication and scopes.
Base URL and calls
Every call goes tohttps://api.reason.bot/v3, with Authorization: Bearer $REASON_API_KEY. Get your organization ID once from GET /v3/self (the field is org_id).
Where the Session runs
Thetarget field selects the computer:
{"kind": "headless", "device_id": "...", "cwd": "/path"}runs on a computer you connected withreason connect. Getdevice_idfrom the devices list. Usecwdto choose the folder.{"kind": "cloud"}runs on a Reason cloud computer.- Omit
targetand it runs in the cloud.
Sample
Save asreason-chat.ts and run it with bun reason-chat.ts.
Verify
- Run the script. It prints the Session URL, then the
pwdreply, then the follow-up reply. - Open the URL. The same Session shows the same two turns in the Reason app.
- Check the exit status: a finished Session has status
exitand outcomesuccess. Treat any other outcome as not done.
POST .../cancel with {"mode": "graceful"}.
Context compaction
When a Session’s context grows past a threshold, Reason folds its earlier turns into a summary and continues. You can set that threshold instead of compacting by hand. Per Session. Sendcompaction in the body of Create a session, Send a message or Message a Reason:
threshold_tokensis a whole number from 64,000 to 10,000,000.nullclears the Session’s own value.- Follow-ups, continuations, and the Sessions and subagents the Session starts keep the value until a message sets it again.
- A message’s value applies from the next turn. A turn that is already running keeps the threshold it started with.
{"threshold_tokens": null} to clear it.
Unset. With neither value, Reason uses its recommendation: near the model’s context window less its output room. For a Reason-paid model with a long-context price tier, the recommendation compacts about 32k tokens below that tier. For GPT-6 Luna that is about 240k.
Limits. A value above what the model can hold (its window less its output room) compacts there instead. It is not an error.
Read the effective value. Retrieve a session reports context.compaction_threshold_tokens and context.compaction_threshold_source, which is recommended, workspace or session. Reason records both when a turn ends, so they are absent until the first turn finishes.
Compact a session’s context (POST .../compact) still compacts at once, whatever the threshold.
Notes
- Events can repeat across pages. The sample keeps the highest
seqperidand orders byposition. Events reference explains why. - The sample reads
assistantevents only. Tool calls and reasoning are in the same feed with othertypevalues. - Pending: a public TypeScript SDK with event long polling is in progress. Until it ships, use the calls above.

