LangChain + Tork Governance
Add AI safety guardrails to your LangChain chains and agents. Evaluate inputs and outputs, enforce policies, and generate compliance receipts automatically.
Policy Enforcement
Block unsafe content automatically
Chain Governance
Wrap any LangChain chain
Tool Safety
Govern tool inputs and outputs
LCEL Support
Works with modern LangChain
Installation
Install Tork with LangChain dependencies.
The Tork SDK includes LangChain adapters in tork.adapters.langchain.
TorkCallbackHandler
Monitor and govern all LangChain operations via callbacks.
The callback handler integrates with LangChain's callback system to evaluate LLM calls, chain executions, and tool invocations against your governance policies.
Callback Events
| on_llm_start | Evaluate prompts before sending to LLM |
| on_llm_end | Evaluate LLM responses |
| on_chain_start | Evaluate chain inputs |
| on_chain_end | Evaluate chain outputs |
| on_tool_start | Evaluate tool inputs |
| on_tool_end | Evaluate tool outputs |
GovernedChain
Wrap any LangChain chain with governance controls.
GovernedChain wraps any LangChain chain (including LCEL) to evaluate inputs before execution and outputs after. Supports automatic redaction of sensitive data.
TorkGovernedTool
Apply governance to individual LangChain tools.
Wrap any LangChain tool to evaluate inputs before execution and outputs after. Useful for sensitive operations like database access, API calls, or file operations.
LCEL Integration
Use with LangChain Expression Language (LCEL) chains.
Tork integrates seamlessly with modern LangChain using LCEL. Use callbacks for monitoring or GovernedChain for strict input/output governance.
AgentExecutor with Governance
Full governance for multi-step agent workflows.
Combine callback handlers with governed tools for comprehensive agent governance. All agent reasoning steps, tool calls, and outputs are evaluated against policies.
Policy Enforcement Patterns
Handle governance violations gracefully.
When content violates policies, GovernanceViolation is raised. Catch this exception to provide user-friendly responses instead of errors.
Advanced Patterns
Streaming, async, and compliance receipts
Best Practices
Use callbacks for monitoring, GovernedChain for strict enforcement
Callbacks log and can block, but GovernedChain is designed to evaluate every input/output.
Wrap sensitive tools individually
Use TorkGovernedTool for tools that access databases, send emails, or modify state.
Handle GovernanceViolation gracefully
Catch exceptions and return user-friendly messages instead of exposing errors.
Enable receipt generation for compliance
Pass receipt_generator to create an audit trail of all governance decisions.
Use consistent agent IDs
Set meaningful agent_id values to track which component triggered evaluations.
Imports Reference
Next Steps
Configure policies in the dashboard and explore other integration options.