For the complete documentation index, see llms.txt. This page is also available as Markdown.
Session Management Best Practices
This guide covers best practices for managing PizzaStack sessions effectively.
Session Lifecycle
Creating a Session
Sessions are created automatically on your first request with a given X-Session-ID. Use a UUID for each new workflow.
import requestsimport uuidAPI_BASE="https://api.tomatopy.pizza/v1"defcreate_session_headers(api_key):"""Create headers for a new session."""return{"Content-Type":"application/json","X-API-Key": api_key,"X-Session-ID":str(uuid.uuid4())}headers =create_session_headers("your-api-key")
One Session Per Workflow
Each independent pizza-making workflow should use its own session. Do not reuse a session ID for unrelated workflows.
Session Expiration
Sessions expire after a period of inactivity. Plan your workflows to complete within a reasonable timeframe.
Checking Session State
Using the Debug Endpoint
The GET /v1/session/{session_id}/log endpoint shows all objects and operations in a session. Use it to debug issues.
Debugging Quality Issues
If your final pizza has a low quality score, use the session log to trace the problem:
Managing Multiple Concurrent Workflows
When running multiple workflows in parallel, each should have its own session:
Best Practices Summary
One session per workflow -- never mix unrelated operations in a single session
Use UUIDs for session IDs -- avoids collisions
Check session state when debugging -- the log endpoint shows all objects and their quality
Handle session expiration -- create a new session if the old one has expired
Keep sessions short-lived -- complete your workflow promptly rather than leaving sessions open
# GOOD: Separate sessions for separate workflows
session_a = create_session_headers("your-api-key")
session_b = create_session_headers("your-api-key")
# Workflow A: Make marinara sauce
tomato_a = requests.post(f"{API_BASE}/tomato/acquire", headers=session_a, json={
"variety": "San Marzano", "ripeness": 0.9, "weight": 400
}).json()
# Workflow B: Make a different sauce (independent)
tomato_b = requests.post(f"{API_BASE}/tomato/acquire", headers=session_b, json={
"variety": "Roma", "ripeness": 0.85, "weight": 300
}).json()
# BAD: Mixing unrelated workflows in one session
# This creates confusion about which objects belong to which workflow
shared_headers = create_session_headers("your-api-key")
tomato_a = requests.post(f"{API_BASE}/tomato/acquire", headers=shared_headers, json={...}).json()
tomato_b = requests.post(f"{API_BASE}/tomato/acquire", headers=shared_headers, json={...}).json()
# Now both tomatoes are in the same session -- harder to track
# Check if a session is still active
session_id = headers["X-Session-ID"]
response = requests.get(
f"{API_BASE}/session/{session_id}/log",
headers={"X-API-Key": "your-api-key"}
)
if response.status_code == 404:
print("Session expired -- create a new one")
headers = create_session_headers("your-api-key")
def check_session(api_key, session_id):
"""Print the current state of a session."""
response = requests.get(
f"{API_BASE}/session/{session_id}/log",
headers={"X-API-Key": api_key}
)
if response.status_code != 200:
print(f"Session not found: {response.status_code}")
return
log = response.json()
print(f"Session: {session_id}")
print(f"Objects: {len(log['objects'])}")
for obj in log["objects"]:
sauce_quality = obj.get("sauce_quality", "N/A")
print(f" {obj['id']} ({obj['type']}): sauce_quality={sauce_quality}")
print(f"\nOperations: {len(log['operations'])}")
for op in log["operations"]:
print(f" {op['timestamp']}: {op['endpoint']} -> {op['result_id']}")
# Usage
check_session("your-api-key", headers["X-Session-ID"])
def diagnose_quality(api_key, session_id):
"""Find where quality dropped in the pipeline."""
response = requests.get(
f"{API_BASE}/session/{session_id}/log",
headers={"X-API-Key": api_key}
)
log = response.json()
for obj in log["objects"]:
sauce_quality = obj.get("sauce_quality")
if sauce_quality == "degraded":
print(f"LOW QUALITY: {obj['id']} ({obj['type']}): {sauce_quality}")
print(f" Check if this object was created correctly")