Python SDK
Full reference for the Thoth Python SDK — ThothConfig, instrument(), instrument_anthropic(), instrument_openai(), LangGraph and CrewAI integrations.
Installation
Python 3.12+ required. The package ships type hints and is fully typed.
thoth.instrument()
Instrument a generic AI agent with Thoth governance. Wraps all tools with enforce/emit hooks. Returns the same agent object (mutated in-place).
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
agent | Any | Yes | Agent object with a .tools attribute |
agent_id | str | Yes | Unique agent identifier |
approved_scope | list[str] | Yes | Authorized tool names |
tenant_id | str | Yes | Your Thoth tenant identifier |
user_id | str | No | User initiating the session (default: "system") |
enforcement | str | No | Enforcement mode (default: "progressive") |
api_key | str | None | No | API key; falls back to THOTH_API_KEY env var |
api_url | str | None | Yes* | Tenant API base URL used for both event ingestion and policy checks; provide directly or via THOTH_API_URL |
session_id | str | None | No | Custom session ID; auto-generated if omitted |
session_intent | str | None | No | Session purpose for HIPAA minimum-necessary scope checks |
environment | str | No | Environment tag for env-scoped policy lookup (default: "prod") |
enforcement_trace_id | str | None | No | Optional cross-service correlation ID; defaults to session ID when omitted |
* api_url may be omitted only when THOTH_API_URL is set.
Supported agent shapes
instrument() detects the agent type automatically:
- LangChain
AgentExecutor— callswrap_langchain_agent()internally - CrewAI
Agent— callswrap_crewai_agent()internally - Generic — any object with a
.toolslist where each tool has a.nameand.run()method
Enforcement Modes
instrument_anthropic()
Wrap tool functions for use in an Anthropic Claude agentic loop.
Returns a new dict[str, Callable] with governance-wrapped callables. The wrapped callables receive the block.input dict from Anthropic tool-use responses.
instrument_openai()
Wrap tool functions for use in an OpenAI tool-calling loop. Signature is identical to instrument_anthropic() (including session_intent, environment, and enforcement_trace_id).
LangGraph Integration
CrewAI Integration
Error Handling
ThothPolicyViolation
Raised when the enforcer blocks a tool call.
Current SDK behavior is fail-closed for enforcement: if the enforcer is unreachable, tool calls
fall back to BLOCK (reason="enforcer unavailable").
Environment Variables
| Variable | Description |
|---|---|
THOTH_API_KEY | API key for hosted Thoth authentication |
THOTH_API_URL | Required tenant API base URL used for both enforcement and event ingestion |
If THOTH_API_URL is set, it overrides the api_url value passed in config.