Skip to main content
The HTTP API creates Sessions, reads their events and sends follow-ups. Use it when your own product shows the conversation. This page builds a small TypeScript client that starts a Session on your computer, follows it, and sends a follow-up.

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

Create an API key in Settings > API with the scopes below, and export it. Do not paste the key into a file.
See Authentication and scopes.

Base URL and calls

Every call goes to https://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

The target field selects the computer:
  • {"kind": "headless", "device_id": "...", "cwd": "/path"} runs on a computer you connected with reason connect. Get device_id from the devices list. Use cwd to choose the folder.
  • {"kind": "cloud"} runs on a Reason cloud computer.
  • Omit target and it runs in the cloud.
Creating a Session returns at once. The Session runs in the background, so poll it.

Sample

Save as reason-chat.ts and run it with bun reason-chat.ts.

Verify

  • Run the script. It prints the Session URL, then the pwd reply, 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 exit and outcome success. Treat any other outcome as not done.
To stop a Session early, send 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. Send compaction in the body of Create a session, Send a message or Message a Reason:
  • threshold_tokens is a whole number from 64,000 to 10,000,000. null clears 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.
Workspace default. An owner or admin sets the default for Sessions without their own value with Update a settings namespace:
Send {"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.
A threshold above the recommendation on a model with a long-context price tier pays that price for every request past the tier. On GPT-6, input past 272k tokens costs double.
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 seq per id and orders by position. Events reference explains why.
  • The sample reads assistant events only. Tool calls and reasoning are in the same feed with other type values.
  • Pending: a public TypeScript SDK with event long polling is in progress. Until it ships, use the calls above.