Skip to main content

Installation

The base package includes OpenAI and Mistral support. Install extras for Anthropic (anthropic) and Cohere (cohere), or use [all] to get everything.

Configuration

Call cognisafe.configure() once at application startup, before any LLM calls are made.
configure() must be called before any patch function or LLM call. Patching before configuration raises a CognisafeNotConfiguredError.

Parameters

* Required if the corresponding environment variable is not set.

Environment variables

As an alternative to passing values to configure(), set these environment variables:
configure() reads these automatically when the keyword arguments are omitted.

Provider patches

OpenAI (proxy mode)

patch_openai() rewrites openai.base_url so all calls route through the proxy. The proxy forwards them to https://api.openai.com and logs each request asynchronously.

Anthropic (direct mode)

Requires pip install "cognisafe[anthropic]". The SDK wraps messages.create and ships payloads to the backend after the response is returned.

Mistral (proxy mode)

Cohere (direct mode)

Requires pip install "cognisafe[all]".

The @cognisafe.trace decorator

Use trace for any LLM call that doesn’t go through a patched provider — custom endpoints, fine-tuned models, internal APIs:
The decorator captures the function’s input arguments and return value, then ships them to /internal/log in a background thread. The decorated function’s execution is not blocked.