Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Deploy, evaluate, and manage AI agents end-to-end on Microsoft Azure AI Foundry
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
foundry-agent/invoke/references/session-management.md
1# Session Management23Manage hosted agent sessions — isolated compute environments that provide persistent state across invocations.45This document covers session creation and lifecycle for both HTTP-protocol agents (`responses`, `invocations`) and WebSocket agents (`invocations_ws`).67## Overview89Sessions bind a hosted agent to a dedicated compute instance. Files written to `$HOME` during a session persist across requests for the lifetime of that session. When a session is deleted, its compute resources and stored files are released.1011## Session Creation1213| Protocol | How a session is created | Session id |14|----------|--------------------------|------------|15| `responses`, `invocations` (HTTP) | Call the `session_create` MCP tool before invoking the agent | **Server-issued** `sessionId` (or a client-supplied one passed to `session_create`) |16| `invocations_ws` (WebSocket) | Implicitly, on the first WebSocket upgrade (no `session_create` call) | **Client-supplied** `agent_session_id` query parameter on the upgrade URL — **optional**; if omitted, the platform (or the container) generates a random id |1718Both ids follow the same format rule: `^[A-Za-z0-9_-]{8,128}$`.1920## Session Lifecycle2122**HTTP (`responses`, `invocations`):**2324```text25session_create → Running → (invoke, file ops) → session_delete26↓27Expired (platform auto-cleanup)28```2930**WebSocket (`invocations_ws`):**3132```text33client opens WS upgrade (optionally with ?agent_session_id=<id>)34└─► first upgrade for that id ──► sandbox created, handler bound35└─► frames flow ──► either side closes ──► WS connection ends36└─► sandbox + $HOME persist ──► next WS upgrade with same id re-hydrates37└─► after the idle timeout, compute is deprovisioned; state is persisted38```3940Key points for `invocations_ws`:4142- There is **no `session_create` / `session_delete`** call. The first upgrade creates the session; the session outlives any individual WebSocket connection.43- The `agent_session_id` query parameter is **optional**. If you omit it, the platform (or the container) generates a random id; supply it explicitly only when you need a specific id to resume an existing session.44- The `agent_session_id` is the **affinity key** — the platform routes upgrades with the same id back to the same sandbox.45- Closing the WebSocket does **not** delete the session. To resume, open a new upgrade with the same `agent_session_id` and the container sees its previous `$HOME` state.46- After the idle timeout, the platform deprovisions compute but persists session state, so the next reconnect re-hydrates the sandbox.4748## Session ID Format4950Session IDs must match the pattern `^[A-Za-z0-9_-]{8,128}$`.5152- If you provide a `sessionId` to `session_create`, it must conform to this pattern53- If you omit `sessionId`, the platform auto-generates one54- Store the returned `sessionId` — it is required for all subsequent operations5556## MCP Tool Details5758### Create Session5960Use `session_create` to provision a new session:6162| Parameter | Required | Description |63|-----------|----------|-------------|64| `projectEndpoint` | ✅ | AI Foundry project endpoint |65| `agentName` | ✅ | Name of the hosted agent |66| `sessionId` | ❌ | Optional custom session ID (8-128 chars, alphanumeric + hyphens/underscores) |6768Returns: Session resource with `sessionId`, status, and expiration.6970### Get Session7172Use `session_get` to check session status:7374| Parameter | Required | Description |75|-----------|----------|-------------|76| `projectEndpoint` | ✅ | AI Foundry project endpoint |77| `agentName` | ✅ | Name of the hosted agent |78| `sessionId` | ✅ | The session ID to inspect |7980Returns: Session details including status, version, creation time, and expiration.8182### Delete Session8384Use `session_delete` to release compute resources:8586| Parameter | Required | Description |87|-----------|----------|-------------|88| `projectEndpoint` | ✅ | AI Foundry project endpoint |89| `agentName` | ✅ | Name of the hosted agent |90| `sessionId` | ✅ | The session ID to delete |9192> ⚠️ **Warning:** Deleting a session permanently removes all files stored in `$HOME` for that session.9394### List Sessions9596Use `session_list` to enumerate sessions:9798| Parameter | Required | Description |99|-----------|----------|-------------|100| `projectEndpoint` | ✅ | AI Foundry project endpoint |101| `agentName` | ✅ | Name of the hosted agent |102| `limit` | ❌ | Max results to return (1-100, default 20) |103| `order` | ❌ | Sort order: `asc` or `desc` (default `asc`) |104| `after` | ❌ | Cursor for forward pagination |105| `before` | ❌ | Cursor for backward pagination |106107> ⚠️ **Warning:** `after` and `before` are mutually exclusive — do not pass both.108109## Session vs Conversation110111| Concept | Purpose | Scope |112|---------|---------|-------|113| `sessionId` | Binds requests to a compute instance with persistent filesystem state | Hosted agents only |114| `conversationId` | Tracks conversation history across turns | Responses protocol only |115116- A single session can host multiple conversations117- A conversation does not require a session (prompt agents use `conversationId` without sessions)118- For hosted agents using `responses` protocol, use **both**: `sessionId` for compute affinity and `conversationId` for history119120## Best Practices1211221. **Create sessions explicitly** — Always use `session_create` before invoking a hosted agent with `responses` or `invocations` protocol. Do not rely on implicit session creation.1232. **Reuse sessions** — Keep the same session for related multi-turn interactions to preserve agent state.1243. **Clean up when done** — Delete sessions after use to release compute resources and avoid quota consumption.1254. **Handle expiry** — Sessions expire based on platform policies. If `session_get` returns a non-running state, create a new session.1265. **Version awareness** — The platform auto-resolves the agent version at session creation time. If you need a specific version, ensure it is active before creating the session.1276. **Debug with logstream** — Use `session_logstream` to stream stdout/stderr from a running session for troubleshooting.128