## Retrieve an agent session

`beta.agents.sessions.retrieve(session_id) -> AgentSession`

**get** `/agents/sessions/{session_id}`

Retrieves the current state of a managed agent session. See [managing sessions](/api/docs/guides/agents-api/sessions/manage).

### Parameters

- `session_id: String`

  `minLength: 0`, `maxLength: 1048576`

### Returns

- `class AgentSession`

  A Managed Agents session.

  - `id: String`

    The ID of the session.

    `minLength: 0`

  - `agent: Agent{ id, instructions, model, 8 more}`

    The agent running in the session.

    - `id: String`

      The ID of the agent.

      `minLength: 0`

    - `instructions: String`

      Custom instructions appended to the agent's default base instructions.

      `minLength: 0`

    - `model: String`

      The model used by the agent.

      `minLength: 0`

    - `multi_agent: MultiAgentConfig`

      Configuration for creating and coordinating subagents.

      - `enabled: bool`

        Whether subagent tools are enabled. Defaults to false.

      - `max_concurrent_subagents: Integer`

        Maximum number of subagents that may run concurrently, or null when disabled. Defaults to 6 when enabled.

        `format: int64`, `minimum: 1`, `maximum: 4294967295`

    - `name: String`

      The reusable agent's name when the session was created, or null if no name was saved. Later changes to the agent's name do not affect this value.

      `minLength: 0`

    - `reasoning: AgentReasoning`

      The agent's reasoning configuration.

      - `effort: :none | :minimal | :low | 4 more`

        The requested reasoning effort, or `null` when the model selects its own default.

        - `:none`

        - `:minimal`

        - `:low`

        - `:medium`

        - `:high`

        - `:xhigh`

        - `:max`

      - `summary: :concise | :detailed | :auto`

        The requested reasoning summary format, or `null` when summaries are disabled.

        - `:concise`

          Returns a concise reasoning summary when supported.

        - `:detailed`

          Returns a detailed reasoning summary when supported.

        - `:auto`

          Automatically selects the most detailed summary supported by the model.

    - `service_tier: :auto | :default | :flex | 3 more`

      The effective service-tier policy for model requests. Defaults to `auto`.

      - `:auto`

      - `:default`

      - `:flex`

      - `:priority`

      - `:fast`

      - `:ultrafast`

    - `text: AgentText`

      Configuration for text generated by the agent.

      - `format_: TextFormat`

        The effective output format. Defaults to ordinary text.

        - `class Text`

          Generates ordinary text without a structured-output constraint.

          - `type: :text`

            The type of the object. Always `text`.

            - `:text`

        - `class JSONSchema`

          Constrains generated text to a JSON Schema.

          - `schema: Hash[Symbol, untyped]`

            The JSON Schema that generated text must match.

          - `type: :json_schema`

            The type of the object. Always `json_schema`.

            - `:json_schema`

      - `verbosity: :low | :medium | :high`

        The amount of text produced by the agent. Defaults to `medium`.

        - `:low`

        - `:medium`

        - `:high`

    - `tools: Array[AgentTool]`

      Tools available to the agent.

      `minItems: 0`, `maxItems: 2000`

      - `class Function`

        A function defined by the application.

        - `defer_loading: bool`

          Whether the function is deferred and discovered through tool search.

        - `description: String`

          A description of what the function does.

          `minLength: 0`

        - `name: String`

          The name of the function.

          `minLength: 0`

        - `parameters: Hash[Symbol, untyped]`

          A JSON Schema object describing the function's arguments.

        - `type: :function`

          The type of the object. Always `function`.

          - `:function`

        - `async: bool`

          Whether the agent continues without waiting for the function result.

      - `class ProgrammaticToolCalling`

        Enables calling tools from model-generated code.

        - `enabled: bool`

          Whether tools can be called from model-generated code.

        - `type: :programmatic_tool_calling`

          The type of the object. Always `programmatic_tool_calling`.

          - `:programmatic_tool_calling`

      - `class Mcp`

        Tools provided by a remote MCP server.

        - `allowed_tools: Array[String]`

          The MCP tools the agent may call.

          `minItems: 0`, `maxItems: 2000`

        - `connection_origin: :service | :environment`

          Where outbound MCP HTTP connections originate.

          - `:service`

          - `:environment`

        - `credential_id: String`

          The attached vault credential selected for this MCP server, if any. Optional when exactly one attached credential matches the server URL.

          `minLength: 0`

        - `request_metadata: Hash[Symbol, untyped]`

          Metadata included with requests to this MCP server.

        - `required: bool`

          Whether this MCP server must initialize before the first turn.

        - `server_label: String`

          A label used to identify the MCP server in tool calls.

          `minLength: 0`

        - `transport: McpTransport`

          The transport used to connect to the MCP server.

          - `class HTTP`

            Connects to an MCP server over HTTP.

            - `server_url: String`

              The URL of the MCP server.

              `minLength: 0`

            - `type: :http`

              The type of the object. Always `http`.

              - `:http`

          - `class Stdio`

            Starts an MCP server as a local process.

            - `args: Array[String]`

              Arguments passed to the MCP server command.

              `minItems: 0`, `maxItems: 2000`

            - `command: String`

              The command used to start the MCP server.

              `minLength: 0`

            - `cwd: String`

              The working directory used to start the MCP server.

              `minLength: 0`

            - `env_vars: Array[String]`

              Environment variable names inherited from the execution environment.

              `minItems: 0`, `maxItems: 2000`

            - `type: :stdio`

              The type of the object. Always `stdio`.

              - `:stdio`

        - `type: :mcp`

          The type of the object. Always `mcp`.

          - `:mcp`

      - `class WebSearch`

        Web search.

        - `allowed_domains: Array[String]`

          Allowed search domains, or `null` when the search is unrestricted.

          `minItems: 0`, `maxItems: 2000`

        - `context_size: :low | :medium | :high`

          The amount of search context made available to the model. Defaults to `medium`.

          - `:low`

          - `:medium`

          - `:high`

        - `location: Location{ city, country, region, timezone}`

          Approximate location used to localize search results, if provided.

          - `city: String`

            The city name.

            `minLength: 0`

          - `country: String`

            The two-letter ISO country code, such as `US`.

            `minLength: 0`

          - `region: String`

            The region or state name.

            `minLength: 0`

          - `timezone: String`

            The IANA timezone, such as `America/Los_Angeles`.

            `minLength: 0`

        - `mode: :disabled | :cached | :live`

          The source used for web search results.

          - `:disabled`

          - `:cached`

          - `:live`

        - `type: :web_search`

          The type of the object. Always `web_search`.

          - `:web_search`

      - `class ComputerUse`

        Browser use in an OpenAI-hosted session.

        - `include_screenshots: bool`

          Whether computer tool outputs include screenshots.

        - `type: :computer_use`

          The type of the object. Always `computer_use`.

          - `:computer_use`

      - `class Automations`

        Lets the agent manage its own scheduled wakeups.

        - `type: :automations`

          The type of the object. Always `automations`.

          - `:automations`

    - `compaction: Compaction{ async}`

      The effective context compaction settings.

      - `async: bool`

        Whether context is compacted in the background. Defaults to true.

    - `experimental: Experimental{ time_awareness}`

      The effective experimental settings for the agent.

      - `time_awareness: bool`

        Whether the model receives periodic current-time reminders.

  - `created_at: Integer`

    The Unix timestamp, in seconds, when the session was created.

    `format: int64`

  - `environment: Environment`

    The execution environment for the session.

    - `class None`

      The session talks to CCA without selecting or provisioning an execution environment.

      - `type: :none`

        The type of the object. Always `none`.

        - `:none`

    - `class OpenAIHosted`

      An environment hosted by OpenAI.

      - `id: String`

        The public ID of the environment.

        `minLength: 0`

      - `capability_directories: Array[String]`

        Directories that contain capabilities exposed to the agent.

        `minItems: 0`, `maxItems: 2000`

      - `files: Array[HostedEnvironmentFile]`

        Files available in the environment, excluding their contents.

        `minItems: 0`, `maxItems: 50`

        - `class HostedEnvironmentFileID`

          A file copied from the OpenAI Files API.

          - `id: String`

            The session-scoped ID of the file in the execution environment.

            `minLength: 0`

          - `file_id: String`

            The ID of the uploaded file.

            `minLength: 0`

          - `path: String`

            The file's absolute path inside the environment.

            `minLength: 0`

          - `size_bytes: Integer`

            The decoded file size in bytes.

            `format: int64`, `minimum: 0`

          - `type: :file_id`

            The type of the object. Always `file_id`.

            - `:file_id`

        - `class Inline`

          A file supplied inline when the session was created.

          - `id: String`

            The session-scoped ID of the file in the execution environment.

            `minLength: 0`

          - `path: String`

            The file's absolute path inside the environment.

            `minLength: 0`

          - `size_bytes: Integer`

            The decoded file size in bytes.

            `format: int64`, `minimum: 0`

          - `type: :inline`

            The type of the object. Always `inline`.

            - `:inline`

      - `network: Network{ access, allowed_domains}`

        The effective network access policy for the environment.

        - `access: :enabled | :disabled | :restricted`

          The environment's network access mode.

          - `:enabled`

            Allows unrestricted network access.

          - `:disabled`

            Disables network access.

          - `:restricted`

            Applies the configured domain restrictions.

        - `allowed_domains: Array[String]`

          Domains the environment may access when network access is restricted.

          `minItems: 0`, `maxItems: 2000`

      - `packages: Packages{ npm, python, system_}`

        Packages installed in the environment.

        - `npm: Array[String]`

          npm packages installed globally in the environment.

          `minItems: 0`, `maxItems: 16384`

        - `python: Array[String]`

          Python packages installed in the environment.

          `minItems: 0`, `maxItems: 16384`

        - `system_: Array[String]`

          System packages installed in the environment.

          `minItems: 0`, `maxItems: 16384`

      - `plugins: Array[HostedPlugin]`

        Plugins installed in the environment, excluding their archive contents.

        `minItems: 0`, `maxItems: 2000`

        - `description: String`

          The installed plugin description.

          `minLength: 0`

        - `name: String`

          The installed plugin name.

          `minLength: 0`

        - `type: :inline`

          The type of the object. Always `inline`.

          - `:inline`

      - `skills: Array[HostedSkill]`

        Skills installed in the environment, excluding their archive contents.

        `minItems: 0`, `maxItems: 2000`

        - `class HostedSkillReference`

          A skill installed from the Skills API.

          - `description: String`

            The installed skill description.

            `minLength: 0`

          - `name: String`

            The installed skill name.

            `minLength: 0`

          - `skill_id: String`

            The referenced skill ID.

            `minLength: 0`

          - `type: :skill_reference`

            The type of the object. Always `skill_reference`.

            - `:skill_reference`

          - `version: String`

            The concrete skill version installed for this session.

            `minLength: 0`

        - `class Inline`

          A skill installed from an inline ZIP archive.

          - `description: String`

            The installed skill description.

            `minLength: 0`

          - `name: String`

            The installed skill name.

            `minLength: 0`

          - `type: :inline`

            The type of the object. Always `inline`.

            - `:inline`

      - `type: :openai_hosted`

        The type of the object. Always `openai_hosted`.

        - `:openai_hosted`

      - `container_size: :small | :medium | :large`

        The effective CPU and memory tier, or null when unknown or outside the public tiers.

        - `:small`

        - `:medium`

        - `:large`

      - `desktop: Desktop{ enabled}`

        The effective desktop configuration.

        - `enabled: bool`

          Whether the environment provisions a desktop and browser proxy.

    - `class SelfHosted`

      An environment hosted by the application.

      - `id: String`

        The public ID of the environment.

        `minLength: 0`

      - `capability_directories: Array[String]`

        Directories that contain capabilities exposed to the agent.

        `minItems: 0`, `maxItems: 2000`

      - `remote_url: String`

        Pass this URL unchanged to `codex exec-server --remote` when connecting this environment.

        `minLength: 0`

      - `type: :self_hosted`

        The type of the object. Always `self_hosted`.

        - `:self_hosted`

      - `workspace_directory: String`

        The absolute project directory inside the environment. Defaults to `/workspace`.

        `minLength: 0`

  - `error: String`

    The error that caused the session to fail, if any.

    `minLength: 0`

  - `last_active_at: Integer`

    The Unix timestamp, in seconds, when the session was last active.

    `format: int64`

  - `metadata: Hash[Symbol, String]`

    Custom string key-value pairs attached to the session.

  - `object: :"agent.session"`

    The object type. Always `agent.session`.

    - `:"agent.session"`

  - `required_actions: Array[ComputerUseApprovalRequest{ request, request_id, turn_id, type} | FunctionCall{ arguments, call_id, name, 2 more} | EnvironmentConnection{ environment_id, type}]`

    Actions that must be completed before the session can continue.

    `minItems: 0`, `maxItems: 2000`

    - `class ComputerUseApprovalRequest`

      Respond to a computer-use request.

      - `request: BrowserAuthentication{ credential_origin, fields, options, 2 more} | BrowserOriginAccess{ origin, reason, type}`

        The information needed to render the request.

        - `class BrowserAuthentication`

          A registered form awaiting the application's response.

          - `credential_origin: String`

            The registered form or frame origin where values will be entered.

            `minLength: 0`

          - `fields: Array[Field{ id, label, required, type}]`

            Controls to render. All submitted values are sensitive.

            `minItems: 0`, `maxItems: 2000`

            - `id: String`

              The field ID to submit as field_id in a fields entry.

              `minLength: 0`

            - `label: String`

              The label to display beside the control.

              `minLength: 0`

            - `required: bool`

              Whether this control requires a nonempty value.

            - `type: String`

              The rendering type, such as email, password, or text.

              `minLength: 0`

          - `options: Array[Option{ id, field_ids, label}]`

            Sign-in methods. Empty for a plain form.

            `minItems: 0`, `maxItems: 2000`

            - `id: String`

              The option ID to submit as selected_option.

              `minLength: 0`

            - `field_ids: Array[String]`

              IDs from the registered fields that this method accepts.

              `minItems: 0`, `maxItems: 2000`

            - `label: String`

              The method label to display.

              `minLength: 0`

          - `reason: String`

            Why the agent needs the user to sign in.

            `minLength: 0`

          - `type: :browser_authentication`

            The type of the object. Always `browser_authentication`.

            - `:browser_authentication`

        - `class BrowserOriginAccess`

          A browser origin awaiting the application's approval decision.

          - `origin: String`

            The origin the browser needs permission to access.

            `minLength: 0`

          - `reason: String`

            The browser's explanation for this request, or null when unavailable.

            `minLength: 0`

          - `type: :browser_origin_access`

            The type of the object. Always `browser_origin_access`.

            - `:browser_origin_access`

      - `request_id: String`

        The registered request ID to echo when responding.

        `minLength: 0`

      - `turn_id: String`

        The turn that requested approval.

        `minLength: 0`

      - `type: :computer_use_approval_request`

        The type of the object. Always `computer_use_approval_request`.

        - `:computer_use_approval_request`

    - `class FunctionCall`

      Run a function tool and submit its result.

      - `arguments: untyped`

        The arguments supplied by the model.

      - `call_id: String`

        The ID to include when submitting the function result.

        `minLength: 0`

      - `name: String`

        The function name.

        `minLength: 0`

      - `turn_id: String`

        The ID of the turn that requested the function call.

        `minLength: 0`

      - `type: :function_call`

        The type of the object. Always `function_call`.

        - `:function_call`

    - `class EnvironmentConnection`

      Reconnect a session environment.

      - `environment_id: String`

        The ID of the environment to reconnect.

        `minLength: 0`

      - `type: :environment_connection`

        The type of the object. Always `environment_connection`.

        - `:environment_connection`

  - `status: :idle | :in_progress | :requires_action | :failed`

    The current status of the session.

    - `:idle`

      The session has no turn in progress and is ready for input. A hosted environment may still be provisioning.

    - `:in_progress`

      The session is processing a turn.

    - `:requires_action`

      The session is waiting for one or more required actions.

    - `:failed`

      The session failed.

  - `usage: TokenUsage`

    Best-effort token usage for the session, or null if unknown. Recorded usage may change.

    - `input_tokens: Integer`

      The number of input tokens used by the agent.

      `format: int64`

    - `input_tokens_details: InputTokensDetails{ cached_tokens}`

      A breakdown of the agent's input token usage.

      - `cached_tokens: Integer`

        The number of input tokens retrieved from the prompt cache.

        `format: int64`

    - `output_tokens: Integer`

      The number of output tokens generated by the agent.

      `format: int64`

    - `output_tokens_details: OutputTokensDetails{ reasoning_tokens}`

      A breakdown of the agent's output token usage.

      - `reasoning_tokens: Integer`

        The number of output tokens used for reasoning.

        `format: int64`

    - `total_tokens: Integer`

      The total number of input and output tokens used by the agent.

      `format: int64`

  - `vault_ids: Array[String]`

    The IDs of vaults made available to the session.

    `minItems: 0`, `maxItems: 2000`

  - `prompt_cache_options: PromptCacheOptions{ ttl}`

    Default prompt cache lifetime for turns in this session, when configured.

    - `ttl: :"30m" | :"12h"`

      - `:"30m"`

      - `:"12h"`

  - `review: :none | :auto_review`

    Review applied to eligible actions in this session. Omitted when review is none and automatic review is not enabled. Omission means none.

    - `:none`

    - `:auto_review`

  - `spend_control: SpendControl{ consumed, limit}`

    Configured spending limit and best-effort consumption, in USD cents. Unlimited sessions omit this object.

    - `consumed: Integer`

      Best-effort recorded spend floored to whole USD cents, or null when unavailable.

      `minimum: 0`

    - `limit: Integer`

      The configured positive limit in USD cents.

      `format: int64`, `minimum: 1`, `maximum: 4503599627370495`

### Example

```ruby
require "openai"

openai = OpenAI::Client.new(api_key: "My API Key")

agent_session = openai.beta.agents.sessions.retrieve("session_id")

puts(agent_session)
```

#### 200 — Example

```json
{
  "id": "id",
  "agent": {
    "id": "id",
    "instructions": "instructions",
    "model": "model",
    "multi_agent": {
      "enabled": true,
      "max_concurrent_subagents": 1
    },
    "name": "name",
    "reasoning": {
      "effort": "none",
      "summary": "concise"
    },
    "service_tier": "auto",
    "text": {
      "format": {
        "type": "text"
      },
      "verbosity": "low"
    },
    "tools": [
      {
        "defer_loading": true,
        "description": "description",
        "name": "name",
        "parameters": {
          "foo": "bar"
        },
        "type": "function",
        "async": true
      }
    ],
    "compaction": {
      "async": true
    },
    "experimental": {
      "time_awareness": true
    }
  },
  "created_at": 0,
  "environment": {
    "type": "none"
  },
  "error": "error",
  "last_active_at": 0,
  "metadata": {
    "foo": "string"
  },
  "object": "agent.session",
  "required_actions": [
    {
      "request": {
        "credential_origin": "credential_origin",
        "fields": [
          {
            "id": "id",
            "label": "label",
            "required": true,
            "type": "type"
          }
        ],
        "options": [
          {
            "id": "id",
            "field_ids": [
              "string"
            ],
            "label": "label"
          }
        ],
        "reason": "reason",
        "type": "browser_authentication"
      },
      "request_id": "request_id",
      "turn_id": "turn_id",
      "type": "computer_use_approval_request"
    }
  ],
  "status": "idle",
  "usage": {
    "input_tokens": 0,
    "input_tokens_details": {
      "cached_tokens": 0
    },
    "output_tokens": 0,
    "output_tokens_details": {
      "reasoning_tokens": 0
    },
    "total_tokens": 0
  },
  "vault_ids": [
    "string"
  ],
  "prompt_cache_options": {
    "ttl": "30m"
  },
  "review": "none",
  "spend_control": {
    "consumed": 0,
    "limit": 1
  }
}
```
