Practical patterns for using sessions in real applications.
Browser Automation
Control a browser across multiple steps:
python
1from inferencesh import Inference23client = Inference(api_key="...")45# Start browser session with 10-minute timeout (browser interactions can be slow)6result = client.run({7 "app": "browser-automation@abc123",8 "function": "navigate",9 "input": {"url": "https://example.com"},10 "session": "new",11 "session_timeout": 600 # 10 minutes12})13session_id = result["session_id"]1415try:16 # Click login button17 client.run({18 "app": "browser-automation@abc123",19 "function": "click",20 "input": {"selector": "#login-btn"},21 "session": session_id22 })2324 # Fill form25 client.run({26 "app": "browser-automation@abc123",27 "function": "type",28 "input": {"selector": "#email", "text": "[email protected]"},29 "session": session_id30 })3132 # Take screenshot33 result = client.run({34 "app": "browser-automation@abc123",35 "function": "screenshot",36 "input": {},37 "session": session_id38 })39 print(f"Screenshot: {result['output']['uri']}")4041finally:42 client.sessions.end(session_id)Code Interpreter
Run code with accumulated state:
python
1from inferencesh import Inference23client = Inference(api_key="...")45# Start interpreter session6result = client.run({7 "app": "python-interpreter@abc123",8 "function": "run",9 "input": {"code": "import pandas as pd\ndf = pd.DataFrame({'a': [1,2,3]})"},10 "session": "new"11})12session_id = result["session_id"]1314try:15 # Variables persist between calls16 result = client.run({17 "app": "python-interpreter@abc123",18 "function": "run",19 "input": {"code": "df['b'] = df['a'] * 2"},20 "session": session_id21 })2223 # Access previously defined data24 result = client.run({25 "app": "python-interpreter@abc123",26 "function": "run",27 "input": {"code": "print(df.to_string())"},28 "session": session_id29 })30 print(result["output"]["stdout"])3132finally:33 client.sessions.end(session_id)Interactive ML Pipeline
Load model once, run multiple inferences:
python
1from inferencesh import Inference23client = Inference(api_key="...")45# Initialize model with 30-minute timeout (model loading is expensive)6result = client.run({7 "app": "ml-pipeline@abc123",8 "function": "load_model",9 "input": {"model_name": "gpt2-large"},10 "session": "new",11 "session_timeout": 1800 # 30 minutes - keep model loaded longer12})13session_id = result["session_id"]1415try:16 # Fast inference calls17 prompts = ["Hello,", "The weather is", "I think that"]1819 for prompt in prompts:20 result = client.run({21 "app": "ml-pipeline@abc123",22 "function": "generate",23 "input": {"prompt": prompt, "max_tokens": 50},24 "session": session_id25 })26 print(f"{prompt} → {result['output']['text']}")2728finally:29 client.sessions.end(session_id)Distributed Usage
Pass session IDs between services:
Service A: Start Session
python
1# service_a.py2import redis3from inferencesh import Inference45client = Inference(api_key="...")6r = redis.Redis()78# Start session and store ID9result = client.run({10 "app": "data-processor@abc123",11 "function": "init",12 "input": {"config": "..."},13 "session": "new"14})1516# Store session ID for other services17r.set("current_session", result["session_id"])18print(f"Session started: {result['session_id']}")Service B: Use Session
python
1# service_b.py2import redis3from inferencesh import Inference45client = Inference(api_key="...")6r = redis.Redis()78# Retrieve session ID9session_id = r.get("current_session").decode()1011# Continue the session12result = client.run({13 "app": "data-processor@abc123",14 "function": "process",15 "input": {"data": "..."},16 "session": session_id17})Service C: End Session
python
1# service_c.py2import redis3from inferencesh import Inference45client = Inference(api_key="...")6r = redis.Redis()78session_id = r.get("current_session").decode()910# End session and cleanup11client.sessions.end(session_id)12r.delete("current_session")Context Manager Pattern (Python)
Automatic cleanup with context managers:
python
1from inferencesh import Inference23client = Inference(api_key="...")45# Session automatically ends when exiting the block6with client.session("browser-automation@abc123") as session:7 session.call("navigate", {"url": "https://example.com"})8 session.call("click", {"selector": "#button"})9 result = session.call("screenshot", {})10 print(f"Screenshot: {result['output']['uri']}")11# Session ended automatically1213# Also works with exceptions14with client.session("browser-automation@abc123") as session:15 session.call("navigate", {"url": "https://example.com"})16 raise ValueError("Something went wrong")17# Session still ended automaticallyError Recovery
Handle session expiration gracefully:
python
1from inferencesh import (2 Inference,3 SessionExpiredError,4 SessionNotFoundError,5)67client = Inference(api_key="...")89class ResilientSession:10 def __init__(self, client, app):11 self.client = client12 self.app = app13 self.session_id = None1415 def call(self, function, input):16 if not self.session_id:17 return self._call_new(function, input)1819 try:20 return self._call_existing(function, input)21 except (SessionExpiredError, SessionNotFoundError):22 # Session lost — recreate23 return self._call_new(function, input)2425 def _call_new(self, function, input):26 result = self.client.run({27 "app": self.app,28 "function": function,29 "input": input,30 "session": "new"31 })32 self.session_id = result["session_id"]33 return result3435 def _call_existing(self, function, input):36 return self.client.run({37 "app": self.app,38 "function": function,39 "input": input,40 "session": self.session_id41 })4243 def end(self):44 if self.session_id:45 try:46 self.client.sessions.end(self.session_id)47 except Exception:48 pass # Best effort49 self.session_id = None505152# Usage53session = ResilientSession(client, "browser-automation@abc123")54try:55 session.call("navigate", {"url": "https://example.com"})56 # ... long running work ...57 session.call("screenshot", {}) # Auto-recreates if expired58finally:59 session.end()Custom Timeout Examples
Choose the right timeout for your use case:
Quick Interactions (Short Timeout)
python
1# Short-lived session for quick form filling (2 minutes)2result = client.run({3 "app": "form-filler@abc123",4 "input": {...},5 "session": "new",6 "session_timeout": 1207})Interactive Workflows (Medium Timeout)
python
1# Interactive session with human-in-the-loop (5 minutes)2result = client.run({3 "app": "assistant@abc123",4 "input": {...},5 "session": "new",6 "session_timeout": 3007})Long-Running Pipelines (Long Timeout)
python
1# Data processing pipeline (1 hour max)2result = client.run({3 "app": "data-processor@abc123",4 "input": {...},5 "session": "new",6 "session_timeout": 3600 # Maximum allowed7})Timeout Guidelines
| Use Case | Recommended Timeout |
|---|---|
| Quick API calls | 30-60 seconds |
| Form filling, simple automation | 2-5 minutes |
| Interactive assistants | 5-15 minutes |
| Browser automation | 10-30 minutes |
| ML model inference | 15-60 minutes |
| Batch processing | 30-60 minutes |
Cleanup Best Practices
Always Use Try/Finally
python
1result = client.run({..., "session": "new"})2session_id = result["session_id"]34try:5 # Your session work6 pass7finally:8 client.sessions.end(session_id)Handle Cleanup Errors
python
1def safe_end_session(client, session_id):2 """End session, ignoring errors."""3 try:4 client.sessions.end(session_id)5 except Exception as e:6 print(f"Warning: Failed to end session {session_id}: {e}")Periodic Cleanup
List and clean up old sessions:
python
1from datetime import datetime, timedelta23def cleanup_old_sessions(client, max_age_hours=1):4 """End sessions older than max_age."""5 cutoff = datetime.utcnow() - timedelta(hours=max_age_hours)67 sessions = client.sessions.list()8 for session in sessions:9 if session.status == "active" and session.created_at < cutoff:10 try:11 client.sessions.end(session.id)12 print(f"Ended old session: {session.id}")13 except Exception as e:14 print(f"Failed to end {session.id}: {e}")Next
- Sessions Concept — Overview
- Sessions Developer Guide — Full API reference