Lifecycle
Get session changes
Long-poll for new events and the answer.
GET
/
api
/
v2
/
sessions
/
{id}
/
changes
from hai_agents import Client
client = Client()
changes = client.sessions.get_session_changes(
session_id,
from_index=0,
wait_for_seconds=25,
)
import { HaiAgentsClient } from "hai-agents";
const client = new HaiAgentsClient();
const changes = await client.sessions.getSessionChanges({
id: sessionId,
fromIndex: 0,
waitForSeconds: 25,
});
curl "https://agp.eu.hcompany.ai/api/v2/sessions/$SESSION_ID/changes?from_index=0&wait_for_seconds=25" \
-H "Authorization: Bearer $HAI_API_KEY"
{
"status": "running",
"started_at": "2026-05-07T14:30:02Z",
"finished_at": null,
"error": null,
"error_code": null,
"answer": null,
"outcome": null,
"metrics": {
"steps": 2,
"total_cost": 0.0093874,
"input_cost": 0.0084664,
"output_cost": 0.000921,
"reasoning_cost": 0.0,
"cost_per_model": [
{
"name": "holo4-27b",
"input_tokens": 21166,
"cached_tokens": 0,
"output_tokens": 307,
"reasoning_tokens": 0,
"input_cost": 0.0084664,
"output_cost": 0.000921,
"reasoning_cost": 0.0,
"total_cost": 0.0093874
}
]
},
"new_events": [
{ "type": "AgentEvent", "data": { "...": "..." }, "timestamp": "2026-05-07T14:30:05Z" }
],
"first_index": 0,
"pending_tool_calls": []
}
Behavior
Works without a key on shared sessions.Polling rules
Poll/changes until status is terminal and a poll returns no events.
- The server holds the request open until something new arrives or
wait_for_secondsexpires. - Each call returns only what is new since
from_index.204means no new events yet. statuscan readcompletedbefore you reach the page with the final events and theanswer, so a terminalstatusalone doesn’t end the loop.- For a finished session, read
latest_answerinstead, or let the SDK helper run the loop.
Path parameters
string
required
The session ID.
Query parameters
integer
default:"0"
Event index to start from. Pass the next unread index to resume where you left off.
integer
Maximum number of events to return. Minimum
0.boolean
default:"true"
Whether to include event details in the response.
integer
default:"0"
How long the server holds the request waiting for changes,
0 to 25 seconds. 0 returns immediately, and 20 to 25 gives efficient long polling.Response
200 with the changes object, or 204 No Content if no new events arrive within the wait.
string | null
When the agent started executing.
string | null
When the session reached a terminal state.
string | null
Short, stable error message if the session failed or timed out. Branch on
error_code, not on this text.string | null
Failure category when the session failed or timed out:
environment_error, no_answer, answer_validation, timeout, insufficient_credit, or internal.string | null
The agent’s self-assessed outcome, reported with its final answer:
success, partial, infeasible, or blocked.string | object | null
The agent’s final result once produced: a string, or an object matching the agent’s
answer_format. It can arrive after status turns terminal (Polling rules). Mirrored on the session’s latest_answer.object
Usage and cost so far. Costs are in USD and
null when a model’s price is unavailable, so null-check before summing.integer
number | null
number | null
number | null
number | null
object[]
Events since
from_index, each an Event object (type, data, timestamp).integer | null
Index of
new_events[0]. Your next from_index is first_index plus the number of new_events. It equals from_index in the cloud. A local runtime session keeps a window of recent events, so it can be higher there when older events were dropped.object[]
The custom tool calls the agent is waiting on, as on status. Empty unless the status is
awaiting_tool_results, or paused while calls are pending.string
object
string | null
from hai_agents import Client
client = Client()
changes = client.sessions.get_session_changes(
session_id,
from_index=0,
wait_for_seconds=25,
)
import { HaiAgentsClient } from "hai-agents";
const client = new HaiAgentsClient();
const changes = await client.sessions.getSessionChanges({
id: sessionId,
fromIndex: 0,
waitForSeconds: 25,
});
curl "https://agp.eu.hcompany.ai/api/v2/sessions/$SESSION_ID/changes?from_index=0&wait_for_seconds=25" \
-H "Authorization: Bearer $HAI_API_KEY"
{
"status": "running",
"started_at": "2026-05-07T14:30:02Z",
"finished_at": null,
"error": null,
"error_code": null,
"answer": null,
"outcome": null,
"metrics": {
"steps": 2,
"total_cost": 0.0093874,
"input_cost": 0.0084664,
"output_cost": 0.000921,
"reasoning_cost": 0.0,
"cost_per_model": [
{
"name": "holo4-27b",
"input_tokens": 21166,
"cached_tokens": 0,
"output_tokens": 307,
"reasoning_tokens": 0,
"input_cost": 0.0084664,
"output_cost": 0.000921,
"reasoning_cost": 0.0,
"total_cost": 0.0093874
}
]
},
"new_events": [
{ "type": "AgentEvent", "data": { "...": "..." }, "timestamp": "2026-05-07T14:30:05Z" }
],
"first_index": 0,
"pending_tool_calls": []
}
Long-polling pattern
The SDK helper runs the polling loop for you: it advancesfrom_index, handles 204 responses, and returns once the session is terminal or idle and every event is read.
from hai_agents import wait_for_session
result = wait_for_session(client, session_id, wait_for_seconds=25)
for event in result.events:
print(event.type)
print(result.status, result.answer)
# client.run_session(...) creates the session and runs this loop in one call.
import { waitForSession } from "hai-agents";
const result = await waitForSession(client, { id: sessionId, waitForSeconds: 25 });
for (const event of result.events) {
console.log(event.type);
}
console.log(result.status, result.answer);
// client.runSession(...) creates the session and runs this loop in one call.
Errors
| Status | Cause |
|---|---|
404 | Not found, or not in your organization. |
Was this page helpful?
⌘I
from hai_agents import Client
client = Client()
changes = client.sessions.get_session_changes(
session_id,
from_index=0,
wait_for_seconds=25,
)
import { HaiAgentsClient } from "hai-agents";
const client = new HaiAgentsClient();
const changes = await client.sessions.getSessionChanges({
id: sessionId,
fromIndex: 0,
waitForSeconds: 25,
});
curl "https://agp.eu.hcompany.ai/api/v2/sessions/$SESSION_ID/changes?from_index=0&wait_for_seconds=25" \
-H "Authorization: Bearer $HAI_API_KEY"
{
"status": "running",
"started_at": "2026-05-07T14:30:02Z",
"finished_at": null,
"error": null,
"error_code": null,
"answer": null,
"outcome": null,
"metrics": {
"steps": 2,
"total_cost": 0.0093874,
"input_cost": 0.0084664,
"output_cost": 0.000921,
"reasoning_cost": 0.0,
"cost_per_model": [
{
"name": "holo4-27b",
"input_tokens": 21166,
"cached_tokens": 0,
"output_tokens": 307,
"reasoning_tokens": 0,
"input_cost": 0.0084664,
"output_cost": 0.000921,
"reasoning_cost": 0.0,
"total_cost": 0.0093874
}
]
},
"new_events": [
{ "type": "AgentEvent", "data": { "...": "..." }, "timestamp": "2026-05-07T14:30:05Z" }
],
"first_index": 0,
"pending_tool_calls": []
}