* 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.
import uk.cognisafe.Cognisafe;import uk.cognisafe.CognisafeConfig;import com.openai.client.OpenAIClient;import com.openai.client.okhttp.OpenAIOkHttpClient;Cognisafe.configure( CognisafeConfig.builder() .apiKey("csk_your_key_here") .projectId("my-app") .build());// The SDK provides a factory method that returns a pre-configured clientOpenAIClient client = Cognisafe.openAIClient();var response = client.chat().completions().create( ChatCompletionCreateParams.builder() .model(ChatModel.GPT_4O) .addUserMessage("Hello from Java!") .build());System.out.println(response.choices().get(0).message().content());
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:
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:
import uk.cognisafe.annotation.CognisafeTrace;import org.springframework.stereotype.Service;@Servicepublic class LlmService { @CognisafeTrace(model = "gpt-4o") public String summarise(String text) { // your LLM call — HTTP, custom client, etc. return callMyInternalModel(text); }}
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:
Without Spring AOP, wrap calls manually using Cognisafe.trace():
import uk.cognisafe.Cognisafe;import java.util.concurrent.Callable;String result = Cognisafe.trace( () -> callMyInternalModel(prompt), "my-model", // model name tag prompt // input to record);