Sessions
A session is a task you start over the API.
A session is a task you start over the API, and it shows in the Context app like any other task. The owner of the key that created it owns the task. A service account key can name another member as the owner with the x-context-owner-user-id or x-context-owner-user-email header. The task has the same transcript and tool permissions as a task started in the app. A session ends when you archive it, and its history stays readable. Nothing deletes a session for you: your deployment keeps it until you delete it. To remove a session and its data permanently, delete it with a key that has sessions:delete, which keys don't have by default (see Choosing a key). To send messages to a session and stream the agent's replies, see Events.
Not every task is a session. A task is a session only when it is a top-level task (not a subagent's), runs on Context's own agent engine and was started over an API: by this API, by the Task API (POST /api/v1/tasks) or by a schedule. A task someone started in the app is not a session, even though it is the same kind of task: GET /v1/sessions/{session_id} returns 404 for it, and List sessions leaves it out. So a session you create here can also be read with the Task API, under the same ID, but a task started in the app can't be picked up with this API.
Status
status | Meaning |
|---|---|
idle | The session is waiting for a message or a tool approval, or hasn't started. |
running | A turn is in progress. |
terminated | The session is archived. Nothing else terminates a session. |
rescheduling | This status is never returned. List sessions accepts it as a statuses filter, which then matches nothing. |
The session object
| Field | Type | Description |
|---|---|---|
id | string | The session's ID, a UUID. It is also the task's ID in the app. |
type | string | Always session. |
status | string | The session's status. One of idle, running or terminated. See Status. |
title | string | null | The session's title. The maximum length is 280 characters. |
agent | object | The agent the session runs, as in Agents without its timestamps and metadata. |
environment_id | string | Where the session runs: env_default. See Environments. |
max_step_minutes | integer | null | A Context extension. The longest one step of this session may run, in minutes, set when the session was created. See Create a session. null means no step limit runs: a session on a paired computer, or one created before this field existed. |
resources | array<object> | The files attached when the session was created, each {id, type: "file", file_id, mount_path, created_at, updated_at}. If you sent a mount_path, this is the file's absolute path on the agent's disk. Otherwise it is inputs/<filename>, in the session's inputs/ folder. id equals file_id. |
usage | object | The totals for the whole session, subagents included: input_tokens, output_tokens, cache_read_input_tokens, cache_creation, active_seconds and list_cost. |
usage.cache_creation | object | Cache-write tokens, as {ephemeral_5m_input_tokens, ephemeral_1h_input_tokens}. ephemeral_1h_input_tokens is always 0, because every cache write counts as a 5-minute write. |
usage.active_seconds | number | The seconds the session spent working, to the millisecond. Time spent waiting for a tool approval isn't counted. While a turn runs, the value grows on every read. |
usage.list_cost | object | null | The session's cost at list price, as {amount, currency: "USD"}. amount is a whole number of cents, as a string. It covers model calls plus web search, image search and media generation, priced for the session's owner. See the list below the table for when it is null. |
created_at | string | When the session was created, as an RFC 3339 timestamp. |
updated_at | string | When the session last changed, as an RFC 3339 timestamp. |
archived_at | string | null | For an archived session, the time it last changed, as an RFC 3339 timestamp. That is when it was archived, unless it was renamed after, which sets it to the time of the rename. null for a session that isn't archived. |
usage.list_cost is null in these cases:
- A turn is running. A cost shown earlier returns to
nullwhile a later turn runs. - Billing hasn't settled, which takes about 12 minutes after the session goes idle.
- No model call was made.
- Your organization counts usage in tokens.
Once it appears, usage.list_cost can still rise. A model call made outside a turn, such as one by a background
job the agent started, is added when billing records it.
Some fields always hold the same value: budget is null, metadata and stats are {}, and outcome_evaluations and vault_ids are empty.
Example
Code
Differences from Anthropic's API
This API follows Anthropic's Managed Agents API, with these differences:
- Deleting a session needs the
sessions:deletecapability, which keys don't have by default. Deleting a session while a turn runs returns409: interrupt the turn, wait foridle, then delete it. The delete response lists what was removed and what was kept. See Delete a session. POST /v1/sessionstakesx-context-owner-user-idorx-context-owner-user-email, so a service account key can create a session for another member. Anthropic's API has no such headers.POST /v1/sessionsreturns400if the request overrides an agent field (any field in theagentobject besidestype,idandversion), or sends a non-emptymetadata, a non-emptyvault_idsor a non-nullbudget.initial_eventstakes at most one event, auser.message. Anthropic's API takes up to 50 events of different types.- Only
fileresources are supported. Any other type returns400. - A file sent without
mount_pathisn't placed on the agent's disk. Anthropic's API copies it to/mnt/session/uploads/<file_id>. Here it stays in the session'sinputs/folder in Drive, where the agent reads it, and itsmount_pathreadsinputs/<filename>. mount_pathtakes a path under/workspace/or/mnt/session/uploads/. Any other absolute path is placed under/mnt/session/uploads:/data/a.csvbecomes/mnt/session/uploads/data/a.csv, andmount_pathreports the new path.usage.server_tool_useisn't sent.titletakes up to 280 characters, and a session takes up to 50 files. Anthropic's API allows 500 of each.- A list response holds at most 100 sessions per page. Anthropic's API allows up to 1,000.
- On list,
order=ascand theagent_version,deployment_idandmemory_store_idfilters return400.
For every difference in one place, see Coming from Anthropic's API.
List sessions
Lists the sessions this key can see, newest first.
A personal key reads its owner's sessions, including sessions a service account key created for its owner. A service account key reads only the sessions it created, whoever owns them. If the key is limited to specific agents (its Agent scope), it reads only those agents' sessions.
The statuses filter applies after the server reads each page.
The key must have the tasks:read capability. For what each capability allows, see Keys.
Response
Returns 200 with the matching sessions in data, newest first. next_page is the value to pass as page for the next page. A page can hold fewer sessions than limit, or none, and still have a next_page value. Keep requesting pages until next_page is null. prev_page is always null.
Errors
This endpoint can return these errors as well as the common errors.
| Status | error.type | Cause and fix |
|---|---|---|
| 400 | invalid_request_error | order is any value other than desc. Omit order. |
| 400 | invalid_request_error | statuses contains an unknown value. Send only idle, running, terminated or rescheduling. |
| 400 | invalid_request_error | The request includes agent_version, deployment_id or memory_store_id, which this API does not take. Remove them. |
query Parameters
limitThe maximum number of sessions to return. Must be between 1 and 100.
The maximum number of sessions to return. Must be between 1 and 100.
pageThe next_page value from the previous response. Leave it out to get the first page.
The next_page value from the previous response. Leave it out to get the first page.
created_at[gt]Only sessions created after this RFC 3339 timestamp.
Only sessions created after this RFC 3339 timestamp.
created_at[gte]Only sessions created at or after this RFC 3339 timestamp.
Only sessions created at or after this RFC 3339 timestamp.
created_at[lt]Only sessions created before this RFC 3339 timestamp.
Only sessions created before this RFC 3339 timestamp.
created_at[lte]Only sessions created at or before this RFC 3339 timestamp.
Only sessions created at or before this RFC 3339 timestamp.
agent_idThe ID of the agent whose sessions to return.
The ID of the agent whose sessions to return.
include_archivedWhether to include archived sessions.
Whether to include archived sessions.
orderNot supported. Omit it or send desc. Any other value returns 400.
Not supported. Omit it or send desc. Any other value returns 400.
statusesThe statuses to return: idle, running or terminated. rescheduling is accepted for compatibility but never matches a session. To pass more than one, repeat the parameter: statuses=idle&statuses=running.
The statuses to return: idle, running or terminated. rescheduling is accepted for compatibility but never matches a session. To pass more than one, repeat the parameter: statuses=idle&statuses=running.
statuses[]The same filter as statuses, in the bracketed form the SDKs send: statuses[]=idle&statuses[]=running.
The same filter as statuses, in the bracketed form the SDKs send: statuses[]=idle&statuses[]=running.
Headers
anthropic-workspace-idThe ID of a workspace in the key's organization that the key's owner works in. Agent and session requests run in that workspace; other requests only check it. The response echoes it. If the owner doesn't work there, the request returns 404. Without it, a new session runs in the agent's workspace or the owner's private workspace: see Where a session runs.
The ID of a workspace in the key's organization that the key's owner works in. Agent and session requests run in that workspace; other requests only check it. The response echoes it. If the owner doesn't work there, the request returns 404. Without it, a new session runs in the agent's workspace or the owner's private workspace: see Where a session runs.
List sessions › Responses
Returns 200 with the matching sessions in data, newest first. next_page is the value to pass as page for the next page. A page can hold fewer sessions than limit, or none, and still have a next_page value. Keep requesting pages until next_page is null. prev_page is always null.
next_pageprev_pageCreate a session
Creates a session for an agent, optionally with files and a first message.
Without initial_events the session is idle until you send its first message. With one message it starts at
once and is running. If the request fails, it returns an error and no session. If the server already created
the session's task in the Context app, it archives it.
Files sent with a mount_path are copied to the agent's disk, read-only, before the first turn. If a file can't
be copied, the session still runs and the file stays in the session's inputs/ folder.
The key must have the tasks:create capability. For what each capability allows, see Keys.
Response
Returns 200 with the session object. For every field, see Sessions.
Errors
This endpoint can return these errors as well as the common errors.
| Status | error.type | Cause and fix |
|---|---|---|
| 400 | invalid_request_error | A resource isn't a file. Send each resource as {type: "file", file_id}, with an optional mount_path. |
| 400 | invalid_request_error | A mount_path is empty, relative, ends in /, holds ., .., a repeated / or a control character, or is too long. Send a normalized absolute path. |
| 400 | invalid_request_error | More than 100 files have a mount_path, two files name the same path, a path sits below another mounted file, or a mounted file is listed twice. Give each mounted file its own path, and list it once. |
| 400 | invalid_request_error | An owner header is empty, or the request sends both x-context-owner-user-id and x-context-owner-user-email. Send one header with a member's ID or email, or omit both so the key's owner owns the session. |
| 400 | invalid_request_error | The named owner isn't an active member of this key's organization. Check the ID or email. |
| 400 | invalid_request_error | initial_events holds more than one event, or one that isn't a user.message. Send at most one user.message, then send the rest with Send events. |
| 400 | invalid_request_error | The title is over 280 characters. Shorten it. |
| 400 | invalid_request_error | max_step_minutes: <n> is above this deployment's maximum of <max> minutes (NATIVE_MAX_TURN_BUDGET_MINUTES). Send <max> or less, or ask your administrator to raise the maximum. A value that isn't a whole number above 0 also returns 400. |
| 400 | invalid_request_error | The body sends non-empty metadata or vault_ids, a non-null budget, or agent overrides (any field in the agent object besides type, id and version). Remove them. |
| 400 | invalid_request_error | A file_id names a file a session produced, which can't be attached. Download it and upload it, then attach the upload. |
| 400 | invalid_request_error | Agent "<name>" (<id>) is not in a workspace this session can run in. The agent isn't in a workspace the session's owner works in. Create the agent with metadata.workspace_id set to a workspace the owner works in, or pick an agent that is in one. |
| 403 | permission_error | The key lacks tasks:create, or isn't allowed to run this agent. An unknown agent ID returns the same 403. Check the agent ID and the key's capabilities. |
| 403 | permission_error | A personal key names a member other than its owner. Use a service account key to create sessions for other members, or omit the header. |
| 403 | permission_error | The named owner can't run this agent. Pick an agent the member can run, or name another member. |
| 404 | not_found_error | environment_id isn't env_default. Check the ID with List environments. |
| 404 | not_found_error | A file_id isn't a file this key can read. Check the ID with List files. |
Headers
x-context-owner-user-idThe ID of the member who owns the session. A service account key can name any active member of its organization. The member owns the session, sees it in the Context app, and must be able to run the agent. A service account key reads and writes only the sessions it created, including those it created for other members. A personal key can name only its own owner. Send this header or x-context-owner-user-email, not both. Without either, the key's owner owns the session. Only this endpoint takes the owner headers.
The ID of the member who owns the session. A service account key can name any active member of its organization. The member owns the session, sees it in the Context app, and must be able to run the agent. A service account key reads and writes only the sessions it created, including those it created for other members. A personal key can name only its own owner. Send this header or x-context-owner-user-email, not both. Without either, the key's owner owns the session. Only this endpoint takes the owner headers.
x-context-owner-user-emailThe email of the member who owns the session. The same rules apply as for x-context-owner-user-id. Send this header or x-context-owner-user-id, not both.
The email of the member who owns the session. The same rules apply as for x-context-owner-user-id. Send this header or x-context-owner-user-id, not both.
anthropic-workspace-idThe ID of a workspace in the key's organization that the key's owner works in. Agent and session requests run in that workspace; other requests only check it. The response echoes it. If the owner doesn't work there, the request returns 404. Without it, a new session runs in the agent's workspace or the owner's private workspace: see Where a session runs.
The ID of a workspace in the key's organization that the key's owner works in. Agent and session requests run in that workspace; other requests only check it. The response echoes it. If the owner doesn't work there, the request returns 404. Without it, a new session runs in the agent's workspace or the owner's private workspace: see Where a session runs.
Create a session › Request Body
An agent ID from List agents. The object form is also accepted. Each agent has one version, so version must be 1 or left out. Any other field in the object returns 400. An unknown agent ID returns 403, not 404.
environment_idThe environment to run the session in: env_default. Any other ID returns 404. See Environments.
working_directoryLeave it out. With env_default, any value returns 400.
The events to start the session with. The array can hold at most one event, and it must be a user.message. See Send events. The response can return before the first turn starts. Before you send another message, wait for session.status_running on the event stream.
Not supported. Omit it or send {}. Any other value returns 400.
The files to give the agent. Each is a {type: "file", file_id, mount_path} object, with a file_id from Upload a file. Up to 50 files. A request with more than 50 files returns 500 instead of a named 400, which is a known defect.
titleThe session's title. The maximum length is 280 characters. Default: The first 80 characters of the first message, or API session.
vault_idsNot supported. Omit it or send []. Any other value returns 400.
budgetNot supported. Omit it or send null. Any other value returns 400.
max_step_minutesA Context extension. The longest one step may run, in minutes. A step is everything the agent does to answer one message. When a step runs past it, the step is stopped and the session goes back to idle, so you can send another message. The maximum is your deployment's limit, which is 720 (12 hours) unless your administrator changed it. A larger value returns 400 with the maximum in the message. It is never lowered for you. The value is set when the session is created and applies to every step. The session object returns it. Default: 240, or your deployment's maximum if that is lower.
Create a session › Responses
Returns 200 with the session object. For every field, see Sessions.
idarchived_atbudgetcreated_atenvironment_idmax_step_minutesmetadatastatsstatustitletypeupdated_atGet a session
Returns a session with its status and token totals.
The key must have the tasks:read capability. For what each capability allows, see Keys.
Response
Returns 200 with the session object. For every field, see Sessions.
Errors
This endpoint can return these errors as well as the common errors.
| Status | error.type | Cause and fix |
|---|---|---|
| 404 | not_found_error | No session with this ID exists, or this key can't read it. Check the session ID and the key you are using. |
| 404 | not_found_error | The ID is a task that isn't a session, such as one started in the app (see Sessions). Read it with the Task API, GET /api/v1/tasks/roots/{taskId}. |
| 503 | api_error | The session's history is still being indexed. Retry the request shortly. |
path Parameters
session_idThe ID of the session to retrieve.
The ID of the session to retrieve.
Headers
anthropic-workspace-idThe ID of a workspace in the key's organization that the key's owner works in. Agent and session requests run in that workspace; other requests only check it. The response echoes it. If the owner doesn't work there, the request returns 404. Without it, a new session runs in the agent's workspace or the owner's private workspace: see Where a session runs.
The ID of a workspace in the key's organization that the key's owner works in. Agent and session requests run in that workspace; other requests only check it. The response echoes it. If the owner doesn't work there, the request returns 404. Without it, a new session runs in the agent's workspace or the owner's private workspace: see Where a session runs.
Get a session › Responses
Returns 200 with the session object. For every field, see Sessions.
idarchived_atbudgetcreated_atenvironment_idmax_step_minutesmetadatastatsstatustitletypeupdated_atRename a session
Changes a session's title and returns the updated session.
Only the title can change. Sending agent, vault_ids, budget or a non-empty metadata returns 400. An empty body, or metadata: {}, returns 200 and changes nothing.
The key must have the tasks:create capability. For what each capability allows, see Keys.
Response
Returns 200 with the session object. For every field, see Sessions.
Errors
This endpoint can return these errors as well as the common errors.
| Status | error.type | Cause and fix |
|---|---|---|
| 400 | invalid_request_error | The title is empty, contains only whitespace or is longer than 280 characters. Send a title of 1 to 280 characters, or null to clear it. |
| 400 | invalid_request_error | The body includes agent, vault_ids, budget or a non-empty metadata. Send only title. |
| 403 | permission_error | The key does not act for the session's owner, or the owner is no longer an active member. Use the owner's personal key or the service account key that created the session. |
| 404 | not_found_error | No session with this ID exists, or this key can't read it. Check the session ID. |
path Parameters
session_idThe ID of the session to rename.
The ID of the session to rename.
Headers
anthropic-workspace-idThe ID of a workspace in the key's organization that the key's owner works in. Agent and session requests run in that workspace; other requests only check it. The response echoes it. If the owner doesn't work there, the request returns 404. Without it, a new session runs in the agent's workspace or the owner's private workspace: see Where a session runs.
The ID of a workspace in the key's organization that the key's owner works in. Agent and session requests run in that workspace; other requests only check it. The response echoes it. If the owner doesn't work there, the request returns 404. Without it, a new session runs in the agent's workspace or the owner's private workspace: see Where a session runs.
Rename a session › Request Body
titleThe new title. Must be between 1 and 280 characters after trimming (Anthropic: 500). Send null to clear the title.
Not supported. Omit it or send {}. Any other value returns 400.
Not supported. Any value returns 400.
vault_idsNot supported. Any value returns 400.
Rename a session › Responses
Returns 200 with the session object. For every field, see Sessions.
idarchived_atbudgetcreated_atenvironment_idmax_step_minutesmetadatastatsstatustitletypeupdated_atDelete a session
Permanently deletes a session and its data.
Deleting removes the session's task in the Context app and the tasks of its subagents, with their events and
transcripts. It also removes the session's inputs/ copies and outputs, its sandbox and saved workspace, its
LLM traces, and the memories its runs saved. The session no longer appears in the task history in the Context
app, and any open stream on the session ends. Response lists what stays.
Delete a session with the owner's personal key or with the service account key that created it. Keys don't have
sessions:delete by default. The key's owner must grant it when creating the key, and an existing key can't gain
it.
Nothing deletes a session for you. Your deployment keeps a session and its history until you delete it with this request, and archiving a session keeps its history too.
If a turn is running, the request returns 409 and the turn keeps running. Send a user.interrupt, wait for
the session to go idle, then delete it. A session waiting for a tool approval counts as idle. If another change
to the session is in progress, for example an event you sent, the delete can return 409. The session still
exists, but its traces, saved workspace and recordings may already be deleted. Retry the delete.
Files you uploaded with Upload a file are kept. Delete them with
Delete a file.
The key must have the sessions:delete capability. For what each capability allows, see Keys.
Response
Returns 200 with a receipt whose type is session_deleted.
removed counts what was deleted:
| Field | What it counts |
|---|---|
tasks | The session's task and its subagents' tasks. |
runs | The runs of those tasks, with their events, transcripts and state. |
sandboxes | The sandboxes the runs still held, now shut down. |
dispatch_rows_scrubbed | Dispatch history rows whose prompt, title and output text were cleared. |
approval_previews_purged | Approval cards whose preview of the action's content was cleared. |
billing_events_unlinked | Billing usage rows whose link to the session's runs was cleared. |
agent_memories_deleted | Memories the session's runs saved. |
traces | deleted, or no_trace_store when your deployment keeps no traces. |
kept lists each store that stays, with a store and a reason:
store | What stays |
|---|---|
billing_meter_events | Billing usage and each run's billed cost, kept for invoicing. They hold counts and prices, not conversation content. |
audit_log | Audit records of who did what, when, and with what outcome. |
dispatch_ledger_rows | Dispatch history, with its prompt, title and output text cleared and its task link removed. |
uploaded_files | Files uploaded with Upload a file. The session's own copy in its inputs/ folder is deleted. |
agent_memory_notes | Notes in the agent's own .memory folder in Drive. They belong to the agent and aren't linked to any run. Memories the session's runs saved are deleted. |
storage_residue | Deleted data that storage keeps for a while: trace rows until the trace store's next merge, and earlier versions of removed files on storage that keeps versions, until they expire. No API returns them. |
Errors
This endpoint can return these errors as well as the common errors.
| Status | error.type | Cause and fix |
|---|---|---|
| 400 | invalid_request_error | The request sends a query parameter. Remove it. |
| 403 | permission_error | The key lacks sessions:delete. Create a key that has sessions:delete. Only the key's owner can grant it. |
| 403 | permission_error | The key doesn't act for the session's owner. Use the owner's personal key or the service account key that created the session. |
| 403 | permission_error | The session's owner doesn't have permission to delete it in the Context app, or is no longer an active member. No key can delete the session in that case. |
| 404 | not_found_error | No session with this ID exists, it is already deleted, or this key can't read it. Check the session ID. |
| 409 | invalid_request_error | A turn is running. Send a user.interrupt, wait until the session is idle, then delete it. |
| 409 | invalid_request_error | Another change to the session was in progress. The session still exists, but its traces, saved workspace and recordings may already be deleted. Retry the delete. |
| 500 | api_error | The session's traces couldn't be removed, so the session wasn't deleted. Retry the delete. |
path Parameters
session_idThe ID of the session to delete.
The ID of the session to delete.
Headers
anthropic-workspace-idThe ID of a workspace in the key's organization that the key's owner works in. Agent and session requests run in that workspace; other requests only check it. The response echoes it. If the owner doesn't work there, the request returns 404. Without it, a new session runs in the agent's workspace or the owner's private workspace: see Where a session runs.
The ID of a workspace in the key's organization that the key's owner works in. Agent and session requests run in that workspace; other requests only check it. The response echoes it. If the owner doesn't work there, the request returns 404. Without it, a new session runs in the agent's workspace or the owner's private workspace: see Where a session runs.
Delete a session › Responses
Returns 200 with a receipt whose type is session_deleted.
idtypeArchive a session
Ends a session permanently. Its history stays readable.
The status becomes terminated. After that, sending an event to the session returns 400, and an open stream
ends with an invalid_request_error frame. Archiving an archived session returns it unchanged. To remove a
session's data permanently, delete it instead. To list archived sessions, call
List sessions with include_archived=true.
You can archive a running session. Its current turn is not cut short, and the run stops once the turn is over.
To stop the turn at once, send a user.interrupt before you archive.
The key must have the tasks:create capability. For what each capability allows, see Keys.
Response
Returns 200 with the session object. For every field, see Sessions.
Errors
This endpoint can return these errors as well as the common errors.
| Status | error.type | Cause and fix |
|---|---|---|
| 403 | permission_error | The key does not act for the session's owner, or the owner is no longer an active member. Use the owner's personal key or the service account key that created the session. |
| 404 | not_found_error | No session with this ID exists, or this key can't read it. Check the session ID. |
path Parameters
session_idThe ID of the session to archive.
The ID of the session to archive.
Headers
anthropic-workspace-idThe ID of a workspace in the key's organization that the key's owner works in. Agent and session requests run in that workspace; other requests only check it. The response echoes it. If the owner doesn't work there, the request returns 404. Without it, a new session runs in the agent's workspace or the owner's private workspace: see Where a session runs.
The ID of a workspace in the key's organization that the key's owner works in. Agent and session requests run in that workspace; other requests only check it. The response echoes it. If the owner doesn't work there, the request returns 404. Without it, a new session runs in the agent's workspace or the owner's private workspace: see Where a session runs.
Archive a session › Responses
Returns 200 with the session object. For every field, see Sessions.
idarchived_atbudgetcreated_atenvironment_idmax_step_minutesmetadatastatsstatustitletypeupdated_at