Python SDK
Full reference for the Thoth Python SDK — ThothConfig, instrument(), instrument_toolchain(), toolchain_function_map(), instrument_anthropic(), instrument_openai(), instrument_claude_agent_sdk(), LangGraph and CrewAI integrations.
Installation
Python 3.12+ required. The package ships type hints and is fully typed.
API Style
Preferred APIs for new integrations:
thoth.instrument()thoth.instrument_toolchain()thoth.toolchain_function_map()thoth.instrument_anthropic()thoth.instrument_openai()thoth.instrument_claude_agent_sdk()
Legacy compatibility:
from thoth import ThothClientremains supported for backward compatibility.ThothClient.wrap(...)is a legacy alias toThothClient.instrument(...).
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 | Authorization allow-list of tool names the agent is permitted to execute |
tenant_id | str | Yes | Your Thoth tenant identifier |
user_id | str | No | User initiating the session (default: "system") |
enforcement | str | No | Enforcement mode (default: "block") |
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.
approved_scope clarifier:
- It is an authorization allow-list, not a free-text description field.
- Values must match emitted tool names exactly (for example
search_docsordata_sources.cloudtrail). - Use tool schemas/prompts and policy context fields (
session_intent,purpose,data_classification,task_context) to convey intent/context to the enforcer.
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_toolchain()
Recursively wraps nested dict/list/object toolchains from a single call.
Public methods are named by dotted path (for example data_sources.cloudtrail)
and governed through the same enforcement path as other SDK integrations.
max_depth is optional; when omitted (None), traversal automatically covers
the full reachable toolchain graph (cycles are skipped).
toolchain_function_map()
Builds a framework-compatible function map from a governed toolchain so users do not need to hand-write mapping dictionaries.
This is useful for frameworks like AutoGen that require a function_map
boundary even when your tools are already instrumented.
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).
instrument_claude_agent_sdk()
Instrument claude-agent-sdk by attaching Thoth governance to
ClaudeAgentOptions.can_use_tool plus post-success/post-failure tool hooks.
Notes:
claude-agent-sdkrequires streaming mode forcan_use_tool, so pass an async iterable prompt toquery(...)instead of a plain string prompt.- This integration is for
query()/ClaudeSDKClientflows that do not expose a.toolslist.
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").
EnforcementDecision
Normalized enforcer response model (snake_case and camelCase payload aliases are both accepted):
Important fields include:
decision,authorization_decision,reason,violation_id,hold_tokendecision_reason_code,action_classificationrisk_score,latency_mspack_id,pack_version,rule_versionregulatory_regimes,matched_rule_ids,matched_control_idspolicy_references,model_signals,receipt
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 |
THOTH_LOG_LEVEL | Optional SDK logger level override (DEBUG, INFO, WARNING, ERROR). Falls back to LOG_LEVEL when unset. |
If THOTH_API_URL is set, it overrides the api_url value passed in config.