Run an agent, then read its runs back using get_runs(session_id):
from praisonaiagents import Agentfrom praisonaiagents.db import PraisonAIDB# Run an agentagent = Agent(name="Research Agent", instructions="Research the topic")result = agent.start("What is quantum computing?")# Query runs for this sessiondb = PraisonAIDB(state_url="sqlite:///agent_state.db")runs = db.get_runs(session_id=agent.session_id)for run in runs: print(f"Run {run['run_id']}: {run['status']} at {run['started_at']}")
3
Query Traces with Filters
Use get_traces() with optional filters for session_id, user_id, and limit:
# Get traces for a specific sessiontraces = db.get_traces(session_id=agent.session_id, limit=10)# Get traces for a specific usertraces = db.get_traces(user_id="user123", limit=5)# Get all traces (up to limit)all_traces = db.get_traces(limit=100)
def get_runs(session_id: str, limit: Optional[int] = None) -> List[dict]: """ Get runs for a session. Args: session_id: Session ID to filter by (required) limit: Maximum number of runs to return (optional) Returns: List of run dictionaries sorted by started_at desc """def get_traces( session_id: Optional[str] = None, user_id: Optional[str] = None, limit: Optional[int] = None) -> List[dict]: """ Get traces with optional filters. Args: session_id: Filter by session ID (optional) user_id: Filter by user ID (optional) limit: Maximum number of traces to return (optional) Returns: List of trace dictionaries sorted by started_at desc """
Behavior:
If state_store is not configured, returns [] and logs a warning
Otherwise you get [] and a warning: "get_runs() called but no state_url configured; returning []".Configure via environment variable or constructor:
# Environment variable (recommended)os.environ["PRAISONAI_STATE_URL"] = "postgresql://user:pass@host/db"# Or via constructordb = PraisonAIDB(state_url="sqlite:///data.db")
Pick a small limit for UIs
These methods scan the store, so use small limits for responsive UIs:
# Good for UI paginationrecent_runs = db.get_runs(session_id, limit=20)# Avoid large scans in UI codeall_runs = db.get_runs(session_id) # Could be thousands
Use these methods outside hot paths
Both methods perform store scans which can be expensive for large datasets. Cache results when possible:
# Cache for dashboard views@lru_cache(maxsize=100)def get_cached_runs(session_id: str, limit: int = 10): return tuple(db.get_runs(session_id, limit)) # Tuple for hashability