The Managed Agents Sessions API: run stateful agent sessions against an environment. Covers session lifecycle, event submission, event history, and the SSE event stream.
A session is event-driven:
- Create the session with
create/1, giving it an agent id and an environment id. - Send a
user.messageviasend_events/3to start work. - Stream the agent's activity back with
stream/2, which yields parsed session/agent/span events. - Steer or interrupt in-flight with additional
send_events/3calls, typicallyuser.interruptoruser.tool_confirmation.
Example
{:ok, session} =
ReqAnthropic.Sessions.create(
agent: "agent_01ABC...",
environment_id: "env_01DEF..."
)
{:ok, _} =
ReqAnthropic.Sessions.send_events(session["id"], [
%{type: "user.message", content: [%{type: "text", text: "List files"}]}
])
{:ok, stream} = ReqAnthropic.Sessions.stream(session["id"])
stream |> Enum.take(10) |> IO.inspect()
Summary
Functions
Archive a session. Preserves history but blocks future events.
Delete a session permanently. Cannot be called while the session is running.
List historical events for a session (paginated).
Convenience: send an interrupt, optionally with a follow-up message.
Send one or more events to a session. events is a list of event maps
(user.message, user.interrupt, user.custom_tool_result,
user.tool_confirmation, user.define_outcome).
Convenience: send a single user text message.
Open an SSE stream for a session and return {:ok, stream} where
stream yields parsed event maps. Must be consumed in the calling
process (per Req's into: :self contract).
Functions
@spec archive( String.t(), keyword() ) :: {:ok, map()} | {:error, ReqAnthropic.Error.t() | Exception.t()}
Archive a session. Preserves history but blocks future events.
@spec create(keyword()) :: {:ok, map()} | {:error, ReqAnthropic.Error.t() | Exception.t()}
@spec delete( String.t(), keyword() ) :: {:ok, map()} | {:error, ReqAnthropic.Error.t() | Exception.t()}
Delete a session permanently. Cannot be called while the session is running.
@spec events( String.t(), keyword() ) :: {:ok, map()} | {:error, ReqAnthropic.Error.t() | Exception.t()}
List historical events for a session (paginated).
@spec get( String.t(), keyword() ) :: {:ok, map()} | {:error, ReqAnthropic.Error.t() | Exception.t()}
@spec interrupt( String.t(), keyword() ) :: {:ok, map()} | {:error, ReqAnthropic.Error.t() | Exception.t()}
Convenience: send an interrupt, optionally with a follow-up message.
@spec list(keyword()) :: {:ok, map()} | {:error, ReqAnthropic.Error.t() | Exception.t()}
@spec send_events(String.t(), [map()], keyword()) :: {:ok, map()} | {:error, ReqAnthropic.Error.t() | Exception.t()}
Send one or more events to a session. events is a list of event maps
(user.message, user.interrupt, user.custom_tool_result,
user.tool_confirmation, user.define_outcome).
@spec send_message(String.t(), String.t(), keyword()) :: {:ok, map()} | {:error, ReqAnthropic.Error.t() | Exception.t()}
Convenience: send a single user text message.
@spec stream( String.t(), keyword() ) :: {:ok, Enumerable.t()} | {:error, term()}
Open an SSE stream for a session and return {:ok, stream} where
stream yields parsed event maps. Must be consumed in the calling
process (per Req's into: :self contract).