Tozil Integration
v1.0This page is designed to be read by AI coding assistants (Claude, Cursor, Copilot, Windsurf) and developers. One page, all instructions, no ambiguity.
If you are an AI coding assistant, follow these steps exactly. The integration requires 2 changes:
- 1. Install the package: pip install tozil or npm install tozil
- 2. Add tozil.init() to the app entry point (before any LLM client is created)
- 3. Set the env var TOZIL_API_KEY (the user will provide this)
Optional: Add user attribution with tozil.set_user(user_id) in auth middleware for per-user cost tracking.
Install
pip install tozil
# or poetry
poetry add tozil
npm install tozil
# or yarn / pnpm / bun
bun add tozil
Initialize
Add this to your app's entry point — before any Anthropic, OpenAI, or Google AI client is created. Tozil will automatically instrument all supported LLM clients.
# Initialize — reads TOZIL_API_KEY from env
tozil.init()
# Your existing code stays the same
from anthropic import Anthropic
client = Anthropic() # auto-instrumented
// Initialize — reads TOZIL_API_KEY from env
tozil.init()
// Your existing code stays the same
import Anthropic from '@anthropic-ai/sdk'
const client = new Anthropic() // auto-instrumented
Set the API key
Add your Tozil API key as an environment variable. The user will provide this.
Optional: User Attribution
To track costs per user, set the user ID in your auth middleware or request handler. All LLM calls in that request context will be attributed to the user.
tozil.set_user(request.user.id)
# All LLM calls in this request
# are now attributed to this user
tozil.setUser(req.user.id)
// All LLM calls in this request
// are now attributed to this user
What this does
- -- Auto-instruments Anthropic, OpenAI, Google AI SDKs
- -- Reports usage async in a background thread
- -- Fails silently if Tozil is unreachable
- -- Adds less than 1ms latency (non-blocking)
What this does NOT do
- -- Does NOT proxy your API calls
- -- Does NOT read or store prompts/responses
- -- Does NOT affect your app if removed
- -- Does NOT require any code changes beyond init()
Framework Integrations
If the project uses a framework, use the appropriate integration:
app = FastAPI()
app.add_middleware(TozilMiddleware) # auto user attribution from request
app.use(tozilMiddleware()) // auto user attribution from req.user
import { withTozil } from 'tozil/next'
export const middleware = withTozil()
chain.invoke(input, config={"callbacks": [TozilCallback(user_id="user_123")]})
Uninstall
Remove the package. Remove the init() call. That's it. No config files, no side effects, zero residue.
Supported providers