Zero-code Java tracing

Attach the traceAI Java agent to a Java 17+ application, check its collector connection, and find a model call in Future AGI without changing your code.

About

Add -javaagent to your startup command to trace model calls without changing your Java application. The agent watches the JDK HttpClient and OkHttp for supported AI requests, including requests to OpenAI-compatible gateways. It does not trace every HTTP request or Java method.

Attach the agent to your application

Use Java 17 or later. You need a Future AGI key pair for tracing; your application still needs its usual model provider credentials. If you don’t have an executable application JAR, complete steps 1–3, then use the support example in place of step 4.

Download the agent JAR

Download com.futureagi:traceai-javaagent:1.4.1 from Maven Central. Keep the agent separate from your application JAR. You don’t need to add a Maven dependency or change the application classpath.

export AGENT="$PWD/traceai-javaagent-1.4.1.jar"
curl -fL -o "$AGENT" https://repo.maven.apache.org/maven2/com/futureagi/traceai-javaagent/1.4.1/traceai-javaagent-1.4.1.jar

Set both keys and choose a project

In the deployment that will receive your traces, open Build → Keys (cloud dashboard). Use the API Key and Secret Key from the same row. If you don’t see Keys, ask an owner or admin for the pair. Supply the values through your secret manager or set them in the shell that starts the JVM:

Keys selected under Build, with the API Key and Secret Key masked in the same row.

export FI_API_KEY="YOUR_API_KEY"
export FI_SECRET_KEY="YOUR_SECRET_KEY"
export FI_PROJECT_NAME="ridgeline-support"
export COLLECTOR_ORIGIN="https://api.futureagi.com"

The examples pass COLLECTOR_ORIGIN to the Java agent as FI_BASE_URL for that JVM only. This leaves the API URL used by other tools unchanged. For a self-hosted deployment, use the origin that serves trace ingestion, for example export COLLECTOR_ORIGIN=http://localhost:4318 if the collector runs on the same host as the JVM. The agent appends /tracer/v1/traces and sends OTLP/HTTP JSON, so don’t include the trace path in the base URL.

Warning

Prompt and response content is captured by default. Before handling customer conversations, set FI_HIDE_INPUTS=true and FI_HIDE_OUTPUTS=true in the process environment to omit messages and values from agent-exported spans. Timing, model and reported token usage remain available. If your app or a Java client wrapper also exports spans, configure its content controls separately; the agent does not filter those exports.

Check the connection

Run these commands in a shell where JAVA_TOOL_OPTIONS and JDK_JAVA_OPTIONS do not already attach an agent. The first prints the masked configuration and integration inventory without sending data. The second attaches the agent and sets full sampling for this diagnostic process only, so the sampler keeps its single root span. Neither command makes a model request.

FI_BASE_URL="$COLLECTOR_ORIGIN" java -jar "$AGENT" doctor
FI_BASE_URL="$COLLECTOR_ORIGIN" FI_SAMPLE_RATE=1 java -javaagent:"$AGENT" -jar "$AGENT" doctor --check

In the first report, "active":false is expected. In the second, "collector":"accepted" means the export endpoint returned HTTP 2xx. To confirm ingestion, open Observe → Tracing → ridgeline-support and find traceai.auto.doctor in the Trace tab. This verifies the export path, not whether the agent can trace your application’s HTTP client.

The ridgeline-support Trace tab lists traceai.auto.doctor with a 9 ms latency and no model input or output.

Restart your app and inspect a model call

If you don’t have an app, continue with the support example instead. Otherwise, replace ridgeline-support.jar with your application’s JAR. Put -javaagent before -jar: after the application JAR name, the JVM passes it to the application as an argument instead.

FI_BASE_URL="$COLLECTOR_ORIGIN" java -javaagent:"$AGENT" -jar ridgeline-support.jar

Make one supported model request and consume the response. Under Observe → Tracing, open ridgeline-support. In the Trace tab, open the request row and select the model-call span. Look for the model, duration and token counts if the provider returned usage. With content capture enabled, you can also inspect the messages.

The openai/gpt-4.1-mini span shows the unused-tent return question and answer, with 42 prompt tokens and 29 completion tokens.

Example: trace a support request

Ridgeline Outfitters wants to see how its support app answers a return question. This example uses only JDK classes and makes one real OpenRouter chat request per run. Set OPENROUTER_API_KEY in your environment; it is separate from the two Future AGI keys. Save the following as RidgelineSupport.java in the directory where you downloaded the agent.

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.Objects;

public class RidgelineSupport {
    public static void main(String[] args) throws Exception {
        String key = Objects.requireNonNull(System.getenv("OPENROUTER_API_KEY"),
                "Set OPENROUTER_API_KEY before starting the app");
        String body = """
                {"model":"openai/gpt-4.1-mini","messages":[
                  {"role":"system","content":"Ridgeline Outfitters accepts unused gear within 30 days. Answer in one sentence."},
                  {"role":"user","content":"Can I return an unused tent I bought 12 days ago?"}
                ]}
                """;
        var request = HttpRequest.newBuilder(URI.create("https://openrouter.ai/api/v1/chat/completions"))
                .timeout(Duration.ofSeconds(60))
                .header("Authorization", "Bearer " + key)
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(body)).build();
        var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
        if (response.statusCode() != 200) {
            throw new IllegalStateException("Model request returned HTTP " + response.statusCode());
        }
        System.out.println(response.body());
    }
}

Build once with JDK 17 or later. In the same shell you used for the connection check, start the same JAR twice. The first launch is the uninstrumented baseline; the second explicitly attaches the agent and samples this standalone request:

javac --release 17 RidgelineSupport.java
jar --create --file ridgeline-support.jar --main-class RidgelineSupport RidgelineSupport.class
java -jar ridgeline-support.jar
FI_BASE_URL="$COLLECTOR_ORIGIN" FI_SAMPLE_RATE=1 java -javaagent:"$AGENT" -jar ridgeline-support.jar

Both runs print a model response. Only the second adds chat openai/gpt-4.1-mini to ridgeline-support. Open that span to see the tent question and any usage the provider reported. With both hide switches enabled, conversation text is omitted; reported usage is not. The separate traceai.auto.doctor span came from doctor --check.

Choose what to trace

By default the agent enables all supported integrations. Choose them at JVM startup:

SettingWhen to use it
FI_INSTRUMENTATIONSInclude wire in a comma-separated allowlist to enable HTTP tracing. This matches supported protocols across providers; openai,wire does not restrict capture to OpenAI.
FI_WIRE_CAPTUREDefaults to true. Setting it to false disables provider HTTP capture even when wire is allowed.
FI_DISABLED_INSTRUMENTATIONSExclude integrations even if allowed. wire disables all provider HTTP tracing; java.net.http or okhttp disables just that transport. servlet disables joining incoming servlet trace context.
FI_AUTO_DEBUG=truePrint masked configuration and integration statuses at application startup. Use this to inspect the actual application instead of the standalone doctor JVM.

For example, keep tracing the JDK client used by ridgeline-support.jar, but skip OkHttp:

FI_BASE_URL="$COLLECTOR_ORIGIN" FI_INSTRUMENTATIONS=wire FI_DISABLED_INSTRUMENTATIONS=okhttp \
  java -javaagent:"$AGENT" -jar ridgeline-support.jar

Limits and troubleshooting

SymptomWhat to check
The app starts but the agent is inactive.Use Java 17+ and supply FI_API_KEY and FI_SECRET_KEY as a complete pair from one source. On Java 8 through 16 the app runs untraced. OTEL_SDK_DISABLED=true also turns the agent off.
The diagnostic appears, but a model call is missing.Check sampling: the default parent-based sampler drops children of an unsampled parent, even with FI_SAMPLE_RATE=1. Also check the transport, not just the SDK name. The agent covers the JDK java.net.http.HttpClient and OkHttp (tested versions: 3.14.9, 4.12.0 and 5.0.0). Apache HttpClient, Reactor Netty, gRPC and WebSockets are not covered. OkHttp in a nested class loader, such as a Spring Boot executable JAR, may not be visible to the agent.
You already start the OpenTelemetry Java agent.Choose one startup agent. With both in the same JVM, the traceAI agent warns and stays off regardless of flag order. An application-owned OpenTelemetry SDK is different: it can keep its own provider, exporter and spans. The traceAI agent exports only its own spans.
A wrapper reports manual, or a call appears twice.The agent does not export traceai-java-* wrapper spans. Those go through the exporter configured by your application. The agent skips a call when an LLM, EMBEDDING, RERANKER or GUARDRAIL span is active on the sending thread. If the SDK sends on another thread or a plain child span is active, both can record the call. Choose one tracing path for that traffic.
Spans are missing when a short-lived app exits.Let the JVM exit normally so the agent’s shutdown hook can drain its queue. For explicit lifecycle calls, put the agent JAR on the compile classpath and import ai.traceai.auto.TraceAIAuto. Its flush() and shutdown() methods wait up to five seconds by default; the startup-only setup needs neither call. Your application’s forceFlush() does not flush the agent’s separate queue.

Next steps

Was this page helpful?

Questions & Discussion