LLM Provider Java Integrations with Future AGI Tracing

Trace Google GenAI, Vertex AI, Azure OpenAI, Ollama, and Watsonx in Java using Future AGI's Traced wrapper pattern for auto-instrumentation.

📝
TL;DR
  • Google GenAI, Azure OpenAI, Ollama and Watsonx use Traced* wrappers
  • The legacy Vertex AI generative SDK requires migration
  • Azure OpenAI traces chat completions, embeddings, and legacy completions
  • Ollama wraps ollama4j, Watsonx uses reflection like Anthropic

Prerequisites

Complete the Java SDK setup first. All providers below need traceai-java-core and TraceAI.init() called before use.

Control captured content

The examples use default content capture, which can send prompts, responses, messages, raw request/response data and exception details to Future AGI. To hide standard input, output and message fields, replace TraceAI.initFromEnvironment() with:

import ai.traceai.TraceAI;
import ai.traceai.TraceConfig;

TraceAI.init(TraceConfig.builder()
    .hideInputs(true)
    .hideOutputs(true)
    .hideInputMessages(true)
    .hideOutputMessages(true)
    .build());

These are separate Java configuration controls. They do not suppress exception messages or stack traces recorded by setError, and are not a filter for every provider-specific attribute. Use non-sensitive sample data if your data policy does not permit those fields.


Google GenAI

Wraps the com.google.genai.Client for Google’s Gemini API.

<dependency>
    <groupId>com.github.future-agi.traceAI</groupId>
    <artifactId>traceai-java-google-genai</artifactId>
    <version>main-SNAPSHOT</version>
</dependency>
implementation 'com.github.future-agi.traceAI:traceai-java-google-genai:main-SNAPSHOT'
import ai.traceai.TraceAI;
import ai.traceai.googlegenai.TracedGenerativeModel;
import com.google.genai.Client;

TraceAI.initFromEnvironment();

Client client = Client.builder()
    .apiKey(System.getenv("GOOGLE_API_KEY"))
    .build();

// Note: model name is a constructor parameter
TracedGenerativeModel model = new TracedGenerativeModel(client, "gemini-2.0-flash");

// Simple generation
var response = model.generateContent("What is the capital of France?");
System.out.println(response.text());

// Multi-turn chat
var chat = model.startChat();
var reply = chat.sendMessage("Hello!");
System.out.println(reply.text());

// Token counting
var tokenCount = model.countTokens("How many tokens is this?");

Spans created:

  • generateContent() - “Google GenAI Generate Content” (LLM)
  • chat.sendMessage() - “Google GenAI Chat Message” (LLM)
  • countTokens() - “Google GenAI Count Tokens” (LLM)

Open your project in Future AGI to inspect the spans.


Vertex AI

Google removed com.google.cloud.vertexai.generativeai from Vertex AI SDK releases after June 24, 2026. For new applications, follow Google’s migration guide to the Google Gen AI SDK. The legacy traceai-java-vertexai wrapper targets that removed package; do not apply it to a com.google.genai.Client.

The Google GenAI example above uses a Gemini API key. It is not a Vertex AI project/location authentication example.


Azure OpenAI

Trace synchronous chat completions, embeddings, and legacy text completions from existing applications using com.azure.ai.openai.OpenAIClient.

Microsoft no longer updates this Azure SDK client in its current form. For new OpenAI features, follow Microsoft’s guidance to the official OpenAI Java SDK. That SDK uses different client types; this wrapper is not a drop-in adapter for it.

Install the legacy client and wrapper

Use Java 17 or later and add both dependencies from Maven Central. The wrapper declares the Azure SDK as a provided dependency, so your application must include it explicitly.

<dependency>
    <groupId>com.futureagi</groupId>
    <artifactId>traceai-java-azure-openai</artifactId>
    <version>1.0.0</version>
</dependency>
<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-ai-openai</artifactId>
    <version>1.0.0-beta.16</version>
</dependency>
implementation 'com.futureagi:traceai-java-azure-openai:1.0.0'
implementation 'com.azure:azure-ai-openai:1.0.0-beta.16'

Trace a chat completion

Set AZURE_OPENAI_ENDPOINT to your Azure resource endpoint, AZURE_OPENAI_API_KEY to its API key, and AZURE_OPENAI_DEPLOYMENT to your chat deployment name. A deployment name can differ from the underlying model name. Configure your Future AGI credentials using the Java SDK setup.

import ai.traceai.TraceAI;
import ai.traceai.azure.openai.TracedAzureOpenAIClient;
import com.azure.ai.openai.OpenAIClient;
import com.azure.ai.openai.OpenAIClientBuilder;
import com.azure.core.credential.AzureKeyCredential;
import com.azure.ai.openai.models.ChatCompletionsOptions;
import com.azure.ai.openai.models.ChatRequestUserMessage;
import java.util.List;

TraceAI.initFromEnvironment();
try {
    OpenAIClient client = new OpenAIClientBuilder()
        .endpoint(System.getenv("AZURE_OPENAI_ENDPOINT"))
        .credential(new AzureKeyCredential(System.getenv("AZURE_OPENAI_API_KEY")))
        .buildClient();
    TracedAzureOpenAIClient traced = new TracedAzureOpenAIClient(client);

    var options = new ChatCompletionsOptions(List.of(
        new ChatRequestUserMessage("What is the capital of France?")
    ));
    var response = traced.getChatCompletions(
        System.getenv("AZURE_OPENAI_DEPLOYMENT"), options);
    System.out.println(response.getChoices().get(0).getMessage().getContent());
} finally {
    TraceAI.shutdown();
}

For a long-running application, initialize TraceAI once and call TraceAI.shutdown() when the application exits, not after each request.

Open your project in Future AGI to inspect the Azure OpenAI Chat Completion span, its deployment name, response model, messages, and token usage. See the Azure OpenAI attributes below for the exported keys. Content capture follows the controls described above.

Supported operations

Wrapper methodSpanKind
getChatCompletions()Azure OpenAI Chat CompletionLLM
getEmbeddings()Azure OpenAI EmbeddingEMBEDDING
getCompletions()Azure OpenAI CompletionLLM, legacy text API

These three methods are blocking calls. Use a deployment appropriate to the operation; an embeddings request needs an embeddings deployment.

Streaming, OpenAIAsyncClient, Responses, audio, images, and files are not traced by this wrapper. unwrap() returns the original client without this wrapper’s instrumentation. Calling a streaming method through unwrap() does not create a semantic LLM span, even if separate Azure HTTP tracing is enabled. Keep calls on the wrapper’s supported methods when you need these traces; do not switch to traceai-java-openai expecting equivalent Azure or streaming behavior.


Ollama

Trace local Ollama chat and embedding requests with TracedOllamaAPI, the wrapper for the legacy io.github.ollama4j.OllamaAPI client.

Use Ollama4j 1.0.79 with this wrapper. Ollama4j 1.1.7 uses io.github.ollama4j.Ollama instead and is not compatible. The SDK is a provided dependency, so add both packages explicitly from Maven Central; no JitPack repository is needed.

<dependency>
    <groupId>com.futureagi</groupId>
    <artifactId>traceai-java-ollama</artifactId>
    <version>1.0.0</version>
</dependency>
<dependency>
    <groupId>io.github.ollama4j</groupId>
    <artifactId>ollama4j</artifactId>
    <version>1.0.79</version>
</dependency>
repositories {
    mavenCentral()
}

dependencies {
    implementation 'com.futureagi:traceai-java-ollama:1.0.0'
    implementation 'io.github.ollama4j:ollama4j:1.0.79'
}

Start your Ollama server at http://localhost:11434 and make sure the llama3 model is installed, or replace the model name below with one you already have. Configure the FI_* variables from the Java SDK setup before running this example. Local model inference stays on your Ollama server; captured trace content is sent to Future AGI.

import ai.traceai.TraceAI;
import ai.traceai.ollama.TracedOllamaAPI;
import io.github.ollama4j.OllamaAPI;
import io.github.ollama4j.models.chat.OllamaChatMessage;
import io.github.ollama4j.models.chat.OllamaChatMessageRole;
import java.util.List;

TraceAI.initFromEnvironment();
try {
    OllamaAPI api = new OllamaAPI("http://localhost:11434");
    TracedOllamaAPI traced = new TracedOllamaAPI(api);

    var response = traced.chat("llama3", List.of(
        new OllamaChatMessage(OllamaChatMessageRole.USER,
            "What is the capital of France?")
    ));
    System.out.println(response.getResponse());
} finally {
    TraceAI.shutdown();
}

For a long-running application, initialize once at startup and call TraceAI.shutdown() when the application exits. Open your project in Future AGI to inspect the Ollama Chat span.

Supported operations

Wrapper methodSpan
chat(model, messages)Ollama Chat (LLM)
embed(model, text)Ollama Embed (EMBEDDING)
embedBatch(model, texts)Ollama Embed Batch (EMBEDDING)
listModels()Ollama List Models (LLM)

Use chat() for text generation: generate() in traceAI 1.0.0 throws before sending the request. Choose an embedding-capable model when using embed() or embedBatch().

Only calls through the wrapper are traced. Streaming callbacks, structured tool-call attributes and token usage are not supported; calls through unwrap() bypass tracing. ollama.response_time_ms records the SDK’s client-side elapsed time for chat, not the server’s model-evaluation duration.


IBM Watsonx

Wraps the Watsonx Java SDK using reflection (like Anthropic). Compatibility depends on the methods exposed by the SDK version you use.

<dependency>
    <groupId>com.github.future-agi.traceAI</groupId>
    <artifactId>traceai-java-watsonx</artifactId>
    <version>main-SNAPSHOT</version>
</dependency>
implementation 'com.github.future-agi.traceAI:traceai-java-watsonx:main-SNAPSHOT'

TracedWatsonxAI is a wrapper reference, not a standalone Watsonx application. Pass an initialized SDK client to its constructor, then pass that SDK’s request objects to generateText, chat or embedText. See the wrapper source for the reflected method signatures; request construction depends on your Watsonx SDK.

Spans created:

  • generateText() - “Watsonx Text Generation” (LLM)
  • chat() - “Watsonx Chat” (LLM)
  • embedText() - “Watsonx Embed” (EMBEDDING)

Watsonx spans include watsonx.project_id, watsonx.space_id, and watsonx.stop_reason.

Like Anthropic, the reflection approach means the client and request objects are typed as Object. Cast the return values to your SDK’s response types.


Common span attributes

Azure OpenAI attributes

The chat example using traceai-java-azure-openai:1.0.0 and azure-ai-openai:1.0.0-beta.16 exports these keys. Response model and token usage depend on the service response; message and output fields follow your content-capture settings.

AttributeDescription
gen_ai.provider.nameazure
gen_ai.request.modelDeployment name passed to the wrapper, which can differ from the model name
gen_ai.response.modelModel name returned by Azure
gen_ai.usage.input_tokensInput token count
gen_ai.usage.output_tokensOutput token count
gen_ai.usage.total_tokensTotal token count
gen_ai.input.messagesJSON-encoded input messages with role and content
gen_ai.output.messagesJSON-encoded output messages with role and content
output.valueText from the first response choice

Note

With these versions, fi.raw_input and fi.raw_output can contain Java object strings such as com.azure.ai.openai.models.ChatCompletionsOptions@..., not full request/response JSON. Use the structured message, model, and usage fields above for inspection and filtering. Do not rely on the raw fields for payload replay.

Legacy snapshot provider attributes

The following table applies to the legacy main-SNAPSHOT integrations for Google GenAI, Ollama, and Watsonx, not the released Azure OpenAI setup above. Attributes vary by provider and operation; check the keys exported by your installed snapshot before configuring filters.

AttributeDescription
llm.providerWrapper-specific provider label: google, ollama or ibm (Watsonx)
llm.request.modelModel name from the request
llm.response.modelModel name from the response (if different)
llm.token_count.promptInput token count
llm.token_count.completionOutput token count
llm.token_count.totalTotal token count
input.value / output.valuePlain text input/output
fi.raw_input / fi.raw_outputBest-effort serialized request/response; not guaranteed to contain complete JSON
Was this page helpful?

Questions & Discussion