Skip to main content

Overview

Sessions are isolated execution environments in buntime.sh. Each session provides a complete Bun runtime with its own filesystem, processes, and resources. Sessions persist state across multiple executions, making them ideal for iterative development and multi-step workflows.

What is a Session?

A session is:
  • Isolated: Completely separate from other sessions
  • Persistent: Files and processes survive between executions
  • Temporary: Automatically expires after a configurable TTL
  • Identified: Has a unique session ID (format: ses_ + alphanumeric)

Session Lifecycle

1. Creation

When you create a session:
  • A unique session ID is generated
  • An isolated container is allocated
  • An empty /workspace directory is created
  • Metadata is stored in KV
  • A TTL (time-to-live) is set

2. Active State

While active, a session:
  • Executes code immediately
  • Maintains running processes
  • Keeps files in memory
  • Responds to API requests
  • Tracks resource usage

3. Idle State

After 5 minutes of inactivity:
  • Container sleeps to save resources
  • Filesystem remains in memory
  • Processes stay alive but suspended
  • Reduced billing rate
  • Wakes instantly on next request

4. Deep Sleep

After 30 minutes of inactivity:
  • /workspace filesystem is saved to R2 storage
  • Container is deallocated
  • Processes are terminated
  • No billing while sleeping
  • ~3 second wake time on next request

5. Expiration

When TTL is reached or session is deleted:
  • All files are permanently deleted
  • Container is deallocated
  • R2 snapshot is removed
  • KV metadata is deleted
  • Session ID becomes invalid
Session expiration is permanent. All data is deleted and cannot be recovered.

Session Properties

Session ID

Format: ses_ followed by 18-20 alphanumeric characters
Used in all API calls to identify which session to execute code in.

Time-to-Live (TTL)

  • Default: 86400 seconds (24 hours)
  • Minimum: 300 seconds (5 minutes)
  • Maximum Free: 86400 seconds (24 hours)
  • Maximum Paid: 604800 seconds (7 days)
Sessions automatically expire after TTL:

Metadata

Attach custom metadata to sessions (max 16KB):
Metadata is useful for:
  • Tracking session ownership
  • Associating sessions with users or conversations
  • Debugging and analytics
  • Custom business logic

Preview URL

Every session gets a unique web preview URL:
This URL routes to any web server running on port 8080 in the session.

Resource Limits

Each session has resource limits: *Subject to memory/CPU limits

Filesystem Persistence

Workspace Directory

All code executes in /workspace:

File Persistence Rules

  • ✅ Files in /workspace persist between executions
  • ✅ Files survive idle and deep sleep states
  • ✅ Files are saved to R2 during deep sleep
  • ✅ Files are restored when session wakes
  • ❌ Files outside /workspace may not persist
  • ❌ Files are deleted when session expires

Directory Structure

Process Management

Background Processes

Processes started in a session stay alive:

Process Lifecycle

  • ✅ Processes survive between executions
  • ✅ Processes stay alive during idle state
  • ❌ Processes are terminated during deep sleep
  • ❌ Processes are terminated on session expiry

Stopping Processes

Kill specific processes:
Or restart the session by deleting and recreating.

Session Strategies

One Session Per Conversation

Best for AI assistants:

One Session Per Task

Best for independent operations:

Session Pool

Best for high-volume workloads:

Best Practices

Use shorter TTLs (1 hour) for ephemeral tasks, longer (24 hours) for conversations.
Always add metadata to help debug and track sessions.
Delete sessions explicitly to free resources and reduce costs.
Never reuse sessions across different users or security contexts.

Monitoring Sessions

Get Session Info

Check session status and resource usage:

List Your Sessions

View all active sessions (coming soon):

Limits

Common Issues

Session Not Found

If you get a 404 error:
  • Session may have expired
  • Session ID might be incorrect
  • Session was deleted

Session Limit Reached

If you hit concurrent session limits:
  • Delete unused sessions
  • Upgrade to paid plan
  • Implement session pooling

Files Missing After Wake

If files disappear after deep sleep:
  • Ensure files are in /workspace
  • Check file paths are absolute from /workspace
  • Verify TTL hasn’t expired

Create Session API

API endpoint documentation

Persistence

How data persists across executions

Resource Limits

Understanding resource constraints

Security

Session isolation and security