Overview
The Lucidic Python SDK provides a comprehensive set of functions to track, monitor, and analyze AI agent workflows. These functions handle session management, step tracking, event logging, and integration with various LLM providers.Installation
Quick Start
Session Management
init
Initialize a new Lucidic session
end_session
End the current session
update_session
Update session metadata
Session Context
Async-safe context managers and helpers
Step Management
create_step
Create a new step in the workflow
update_step
Update an existing step
end_step
End the current step
Event Management
create_event
Create a new event within a step
update_event
Update an existing event
end_event
End an event
Advanced Features
Decorators
Context Managers
session
Full-lifecycle session within a context
session_async
Async full-lifecycle session within a context
bind_session
Bind existing session for a block (no end)
bind_session_async
Async bind-only session context
run_session
Run a function in a new session context
run_in_session
Run a function in an existing session context
set_active_session
Manually set the active session id
clear_active_session
Clear the active session id
Function Categories
Session Lifecycle
- Initialization: Use
init()
to start a new session or resume withinit(session_id=...)
- Updates: Modify session data with
update_session()
- Completion: End with
end_session()
including final evaluation
Step Management
- Creation: Use
create_step()
to begin tracking a unit of work - Progress: Update step data with
update_step()
- Completion: Finish with
end_step()
including evaluation - Decorators: Use
@step
decorator for automatic management
Event Tracking
- Automatic: LLM calls through instrumented providers create events automatically
- Manual: Use
create_event()
for custom events - Updates: Modify events with
update_event()
andend_event()
- Decorators: Use
@event
decorator for function-level tracking
Utilities
- Prompts: Centralized prompt management with
get_prompt()
- Mass Simulations: Batch operations with
create_mass_sim()
Context Managers
Use these for async/thread-safe session handling, similar to the TypeScript SDK:session(...)
— full lifecycle within a contextsession_async(...)
— async variantbind_session(session_id)
— bind only (no end)bind_session_async(session_id)
— async variantrun_session(fn, init_params, ...)
— run a function within a new sessionrun_in_session(session_id, fn, ...)
— run a function in an existing sessionset_active_session(session_id)
/clear_active_session()
— manual control
Decorators
- @step: Wrap functions with automatic step tracking
- @event: Create events from function calls automatically
Provider Support
The SDK automatically instruments these providers when specified ininit()
:
- OpenAI -
providers=["openai"]
- Anthropic -
providers=["anthropic"]
- LangChain -
providers=["langchain"]
- PydanticAI -
providers=["pydantic_ai"]
- OpenAI Agents -
providers=["openai_agents"]
- LiteLLM -
providers=["litellm"]
(tracks many providers via LiteLLM)
Integration Guides
- LiteLLM — bridge LiteLLM to Lucidic events
Best Practices
- Prefer Context Managers: Use
with lai.session(...)
for self-sufficient workflows that start and end within one block - Bind Existing Sessions: Use
with lai.bind_session(session_id)
to attribute work to an existing session without ending it - Concurrent Safety: Rely on context-bound sessions instead of globals in multi-threaded/async code
- Ensure Cleanup: Context managers handle cleanup; otherwise call
lai.end_session()
explicitly when not using contexts - Evaluation Tracking: Use
eval_score
andeval_description
on steps where helpful; avoid manual overrides at scale
Error Handling
Environment Variables
LUCIDIC_API_KEY
- Your API key for authenticationLUCIDIC_AGENT_ID
- Your agent identifierLUCIDIC_PRODUCTION_MONITORING
- Enable production modeLUCIDIC_AUTO_END
- Control automatic session ending