Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Build and deploy AI applications on Azure AI Foundry using Microsoft's model catalog and AI services
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.45## Overview67Sessions 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.89## Session Lifecycle1011```text12session_create → Running → (invoke, file ops) → session_delete13↓14Expired (platform auto-cleanup)15```1617## Session ID Format1819Session IDs must match the pattern `^[A-Za-z0-9_-]{8,128}$`.2021- If you provide a `sessionId` to `session_create`, it must conform to this pattern22- If you omit `sessionId`, the platform auto-generates one23- Store the returned `sessionId` — it is required for all subsequent operations2425## MCP Tool Details2627### Create Session2829Use `session_create` to provision a new session:3031| Parameter | Required | Description |32|-----------|----------|-------------|33| `projectEndpoint` | ✅ | AI Foundry project endpoint |34| `agentName` | ✅ | Name of the hosted agent |35| `sessionId` | ❌ | Optional custom session ID (8-128 chars, alphanumeric + hyphens/underscores) |3637Returns: Session resource with `sessionId`, status, and expiration.3839### Get Session4041Use `session_get` to check session status:4243| Parameter | Required | Description |44|-----------|----------|-------------|45| `projectEndpoint` | ✅ | AI Foundry project endpoint |46| `agentName` | ✅ | Name of the hosted agent |47| `sessionId` | ✅ | The session ID to inspect |4849Returns: Session details including status, version, creation time, and expiration.5051### Delete Session5253Use `session_delete` to release compute resources:5455| Parameter | Required | Description |56|-----------|----------|-------------|57| `projectEndpoint` | ✅ | AI Foundry project endpoint |58| `agentName` | ✅ | Name of the hosted agent |59| `sessionId` | ✅ | The session ID to delete |6061> ⚠️ **Warning:** Deleting a session permanently removes all files stored in `$HOME` for that session.6263### List Sessions6465Use `session_list` to enumerate sessions:6667| Parameter | Required | Description |68|-----------|----------|-------------|69| `projectEndpoint` | ✅ | AI Foundry project endpoint |70| `agentName` | ✅ | Name of the hosted agent |71| `limit` | ❌ | Max results to return (1-100, default 20) |72| `order` | ❌ | Sort order: `asc` or `desc` (default `asc`) |73| `after` | ❌ | Cursor for forward pagination |74| `before` | ❌ | Cursor for backward pagination |7576> ⚠️ **Warning:** `after` and `before` are mutually exclusive — do not pass both.7778## Session vs Conversation7980| Concept | Purpose | Scope |81|---------|---------|-------|82| `sessionId` | Binds requests to a compute instance with persistent filesystem state | Hosted agents only |83| `conversationId` | Tracks conversation history across turns | Responses protocol only |8485- A single session can host multiple conversations86- A conversation does not require a session (prompt agents use `conversationId` without sessions)87- For hosted agents using `responses` protocol, use **both**: `sessionId` for compute affinity and `conversationId` for history8889## Best Practices90911. **Create sessions explicitly** — Always use `session_create` before invoking a hosted agent. Do not rely on implicit session creation.922. **Reuse sessions** — Keep the same session for related multi-turn interactions to preserve agent state.933. **Clean up when done** — Delete sessions after use to release compute resources and avoid quota consumption.944. **Handle expiry** — Sessions expire based on platform policies. If `session_get` returns a non-running state, create a new session.955. **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.966. **Debug with logstream** — Use `session_logstream` to stream stdout/stderr from a running session for troubleshooting.97