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
/workspacedirectory 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:/workspacefilesystem 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 Properties
Session ID
Format:ses_ followed by 18-20 alphanumeric characters
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)
Metadata
Attach custom metadata to sessions (max 16KB):- 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:Resource Limits
Each session has resource limits:| Resource | Free Tier | Paid Tier | Enterprise |
|---|---|---|---|
| Memory | 1 GB | 4 GB | Custom |
| CPU | 0.25 cores | 1 core | Custom |
| Disk | 1 GB | 5 GB | Custom |
| Processes | Unlimited* | Unlimited* | Unlimited* |
Filesystem Persistence
Workspace Directory
All code executes in/workspace:
File Persistence Rules
- ✅ Files in
/workspacepersist 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
/workspacemay 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: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
Set appropriate TTLs
Set appropriate TTLs
Use shorter TTLs (1 hour) for ephemeral tasks, longer (24 hours) for conversations.
Use metadata for tracking
Use metadata for tracking
Always add metadata to help debug and track sessions.
Clean up when done
Clean up when done
Delete sessions explicitly to free resources and reduce costs.
Don't share sessions
Don't share sessions
Monitoring Sessions
Get Session Info
Check session status and resource usage:List Your Sessions
View all active sessions (coming soon):Limits
| Plan | Concurrent Sessions | Max TTL | Total Sessions/Month |
|---|---|---|---|
| Free | 10 | 24 hours | Unlimited |
| Paid | 100 | 7 days | Unlimited |
| Enterprise | Custom | Custom | Unlimited |
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