Skip to main content
An episode is a complete run of experience from start to termination. In ORS, a session with a server is equivalent to an episode if the agent terminates the session after receiving a finished signal.

Sessions as Episodes

In ORS an episode
  • Starts with a specific task
  • Continues through multiple tool calls
  • Ends when finished: true is received from a ToolOutput.

RL Episode Terminology

Episode Lifecycle

Complete Flow

Episode Lifecycle - 5 steps from session creation to cleanup

States in Detail

1. Session ID Generation

Purpose: Generate a unique identifier for this episode. Response:
Note: This just creates an ID. No environment is instantiated yet.

2. Episode Initialization

All body fields are optional. env_name defaults to the first registered environment. Either task_spec or both split+index must be provided (see CreateSession). What happens:
  1. Server resolves env_name (or defaults to first environment) and task_spec (or loads from split/index)
  2. Instantiates the environment class with task_spec and secrets
  3. Calls environment.setup() (async)
  4. Marks session as “ready” when setup completes
Blocking: Subsequent requests wait for setup to complete before proceeding.

3. Initial Observation

Purpose: Get the initial observation (o₀) for the episode. Response:
RL Interpretation: This is the initial observation that the agent uses to select its first action.

4. Action-Observation Loop

Response (SSE):
What happens:
  1. Agent takes action (calls tool)
  2. Environment executes action
  3. Environment returns next state (blocks), reward, and termination flag
  4. If finished: false, repeat from step 1
  5. If finished: true, episode is complete
RL Interpretation: This is the core RL loop:
  • Action: Tool call
  • Observation: Blocks
  • Reward: Reward signal
  • Terminal: Finished flag

5. Episode Termination

Purpose: Clean up episode resources. What happens:
  1. Calls environment.teardown()
  2. Removes session from active sessions
  3. Frees memory and resources
Important: Always call /delete when done, even if episode finished naturally.

Episode Termination

The finished Signal

The finished field in ToolOutput is critical:
When finished: true:
  • Episode is complete
  • Agent should stop calling tools
  • Agent should call /delete to cleanup
  • Task succeeded or failed (check reward or blocks for details)
When finished: false:
  • Episode continues
  • Agent should take another action
  • State may have changed (reflected in blocks)

Termination Patterns

Pattern 1: Immediate Termination

Task completes in one step:

Pattern 2: Multi-Step Termination

Task requires multiple actions:

Pattern 3: Failure Termination

Task fails (but episode still terminates):

State Management

What’s Preserved in a Session?

Environment state:
  • Instance variables in environment class
  • Files created during episode (if environment has filesystem or persistent sandbox)
  • Any side effects from tool executions
Example:

What’s NOT Preserved?

Across episodes:
  • Each session is independent at the protocol level
  • Session 1 and Session 2 have separate environment instances
  • No shared instance state between sessions (though implementations may share class-level or cached state)
After timeout:
  • 15 minutes of inactivity → session deleted
  • State is lost
  • Must create new session
After finished: true:
  • Episode data is final
  • Further tool calls should not be made
  • Call /delete for cleanup

Session Timeout

Sessions automatically expire after 15 minutes of inactivity.

Inactivity Definition

“Inactivity” means no requests with that session’s X-Session-ID:
  • /ping resets timer
  • /{env_name}/call resets timer
  • /{env_name}/prompt resets timer
  • Any request with X-Session-ID resets the timer (except /delete, which removes the session)

Keeping Sessions Alive

For long-running episodes, periodically call /ping:

Timeout Cleanup

When a session times out:
  1. Server calls environment.teardown()
  2. Session removed from active sessions
  3. Subsequent requests with that session ID → 404 Not Found

Session Best Practices

1. Always Delete Sessions

2. Check finished Flag

3. Handle Errors Gracefully

4. Use Context Managers

Debugging Sessions

Common Issues

Issue: “404 Session not found”
  • Cause: Session timed out or was deleted
  • Fix: Check that episode completes within 15 minutes or use /ping
Issue: “Session already exists”
  • Cause: Trying to create episode with already-used session ID
  • Fix: Generate new session ID with /create_session
Issue: “Session deleted” (410)
  • Cause: Calling tool after /delete was called
  • Fix: Don’t reuse session IDs after deletion

Next Steps

Rewards Concept

Understand reward signals in episodes

Implementing a Client

Build a client that manages sessions

Key Takeaway: Sessions are RL episodes. They start with a task, continue until finished: true, and should always be cleaned up with /delete.