Skip to main content

Installation

Requires Java 11 or higher.

Configuration

Configure the SDK once at application startup using the builder pattern:

Configuration options

* Required if the corresponding environment variable is not set.
The SDK is thread-safe. Call Cognisafe.configure() once at startup (e.g., in your Spring @Configuration class or main() method) — it initialises a shared singleton. Do not call configure() per-request.

Patching the OpenAI Java client

Cognisafe.openAIClient() returns a standard OpenAIClient with the baseUrl pre-configured to point at the Cognisafe proxy. You can also configure your own client by setting the base URL manually:

The @CognisafeTrace annotation (Spring Boot)

For Spring Boot applications, use the @CognisafeTrace annotation on any method that calls an LLM. The SDK uses Spring AOP to intercept the call and log the inputs and outputs:
The annotation captures:
  • Method arguments (serialised to JSON)
  • Return value (serialised to JSON)
  • Execution time (latency)
Results are shipped to /internal/log in a background thread — the annotated method is not blocked.
@CognisafeTrace requires the cognisafe-sdk-spring module. Add it alongside the core SDK:

Non-Spring usage (manual trace)

Without Spring AOP, wrap calls manually using Cognisafe.trace():