Tracer
Track agent behavior and evaluate performance in real-time with the Tracer class.
The Tracer class provides comprehensive observability for AI agents and LLM applications. It automatically captures execution traces, spans, and performance metrics while enabling real-time evaluation and monitoring through the Judgment platform.
import { Judgeval } from "judgeval";
const client = Judgeval.create();
const tracer = await client.nodeTracer.create({
projectName: "customer_support_agent",
});
const processQuery = tracer.observe(async function (query: string) {
const response = await generateLLMResponse(query);
return response;
}, "llm");
const result = await processQuery("What is machine learning?");
await tracer.shutdown();client.nodeTracer.create()
Creates a NodeTracer with the specified configuration. Use this for Node.js applications.
async create(config: NodeTracerConfig): Promise<NodeTracer>Parameters
configrequired
:NodeTracerConfigprojectNamerequired
:stringenableEvaluation
:booleanToggle evaluations for asyncEvaluate()
enableMonitoring
:booleanEnable monitoring and metrics collection
resourceAttributes
:Record<string, unknown>OpenTelemetry resource attributes to attach to all spans
instrumentations
:Instrumentation[]OpenTelemetry instrumentations to enable (e.g., OpenAIInstrumentation, HttpInstrumentation). Refer to the auto-instrumentation example below.
serializer
:FunctionCustom serialization function for span data
initialize
:booleanWhether to automatically initialize after creation
Returns
Promise<NodeTracer> - Initialized NodeTracer instance
Example
import { Judgeval } from "judgeval";
const client = Judgeval.create();
const tracer = await client.nodeTracer.create({
projectName: "customer_support_agent",
enableEvaluation: true,
enableMonitoring: true,
resourceAttributes: {
"service.name": "support-api",
"service.version": "2.0.0",
"deployment.environment": "production",
},
});
const myFunction = tracer.observe(async function (input: string) {
const result = await processData(input);
return result;
});
await myFunction("test data");
await tracer.shutdown();Auto-instrumentation
import { OpenAIInstrumentation } from "@opentelemetry/instrumentation-openai";
import { Judgeval, type NodeTracer } from "judgeval";
export const client = Judgeval.create();
const initPromise = client.nodeTracer
.create({
projectName: "auto_instrumentation_example",
enableEvaluation: true,
enableMonitoring: true,
instrumentations: [new OpenAIInstrumentation()],
})
.then((t: NodeTracer) => {
return t;
});
export async function getTracer(): Promise<NodeTracer> {
return await initPromise;
}import { Example } from "judgeval";
import OpenAI from "openai";
import { client, getTracer } from "./instrumentation";
function requireEnv(name: string): string {
const value = process.env[name];
if (!value) {
throw new Error(`Environment variable ${name} is not set`);
}
return value;
}
const OPENAI_API_KEY = requireEnv("OPENAI_API_KEY");
const openai = new OpenAI({
apiKey: OPENAI_API_KEY,
});
async function _chatWithUser(userMessage: string): Promise<string> {
const messages: OpenAI.Chat.ChatCompletionMessageParam[] = [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: userMessage },
];
const completion = await openai.chat.completions.create({
model: "gpt-4o-mini",
messages,
});
const result = completion.choices[0].message.content || "";
console.log(`User: ${userMessage}`);
console.log(`Assistant: ${result}`);
const tracer = await getTracer();
tracer.asyncEvaluate(
client.scorers.builtIn.answerRelevancy(),
Example.create({
input: "What is the capital of France?",
actual_output: result,
})
);
return result;
}
(async () => {
const tracer = await getTracer();
const chatWithUser = tracer.observe(_chatWithUser);
const result = await chatWithUser("What is the capital of France?");
console.log(result);
await new Promise((resolve) => setTimeout(resolve, 10000));
await tracer.shutdown();
})();client.browserTracer.create()
Creates a BrowserTracer with the specified configuration. Use this for browser applications.
async create(config: BrowserTracerConfig): Promise<BrowserTracer>Parameters
configrequired
:BrowserTracerConfigprojectNamerequired
:stringenableEvaluation
:booleanToggle evaluations for asyncEvaluate()
enableMonitoring
:booleanEnable monitoring and metrics collection
resourceAttributes
:Record<string, unknown>OpenTelemetry resource attributes to attach to all spans
serializer
:FunctionCustom serialization function for span data
initialize
:booleanWhether to automatically initialize after creation
Returns
Promise<BrowserTracer> - Initialized BrowserTracer instance
Example
import { Judgeval } from "judgeval";
const client = Judgeval.create();
const tracer = await client.browserTracer.create({
projectName: "ecommerce_frontend",
enableEvaluation: false,
enableMonitoring: true,
resourceAttributes: {
"browser.user_agent": navigator.userAgent,
"browser.language": navigator.language,
"app.version": "1.0.0",
},
});
const handleClick = tracer.observe(async function (event: MouseEvent) {
const response = await fetch("/api/data");
const data = await response.json();
return data;
});
document.getElementById("btn")?.addEventListener("click", handleClick);observe()
Wraps a function to automatically create spans and record inputs/outputs.
Can also be used as a decorator for class methods. Read more about decorators in the TypeScript handbook.
observe<TArgs extends unknown[], TResult>(
func: (...args: TArgs) => TResult,
spanKind?: string
): (...args: TArgs) => TResult
observe(
spanKind?: string
): (
target: unknown,
propertyKey: string | symbol,
descriptor?: PropertyDescriptor
) => voidParameters
func
:FunctionThe function to wrap (automatically provided when used as decorator)
spanKind
:stringType of span to create. Available options:
-
"span": General span (default) -
"tool": For functions that should be tracked as agent tools -
"llm": For language model calls
Returns
Wrapped function or decorator that creates spans during execution
Examples
import { Judgeval } from "judgeval";
const client = Judgeval.create();
const tracer = await client.nodeTracer.create({
projectName: "document_search",
enableEvaluation: true,
});
const searchFunction = tracer.observe(async function(query: string) {
return await searchDatabase(query);
}, "tool");
const results = await searchFunction("search term");import { Judgeval } from "judgeval";
const client = Judgeval.create();
const tracer = await client.nodeTracer.create({
projectName: "content_generator",
enableEvaluation: true,
});
class ContentService {
@tracer.observe("llm")
async generateResponse(prompt: string): Promise<string> {
return await callLLM(prompt);
}
@tracer.observe("tool")
async processData(data: unknown): Promise<unknown> {
return transform(data);
}
}const outerFunction = tracer.observe(async function(input: string) {
const innerFunction = tracer.observe(async function(data: string) {
return await processData(data);
}, "tool");
const result = await innerFunction(input);
return result;
});asyncEvaluate()
Asynchronously evaluates a span using a scorer. Read more about scorers in the Evaluation section.
asyncEvaluate(
scorer: Scorer,
example: ExampleModel,
): voidParameters
scorerrequired
:ScorerThe scorer to use for evaluation. Use built-in scorers from
client.scorers.builtIn or retrieve prompt scorers
examplerequired
:ExampleModelExample object containing evaluation data with input, actual_output,
and optionally expected_output fields
Returns
void - evaluation is triggered asynchronously
Example
import { Judgeval, Example } from "judgeval";
const client = Judgeval.create();
const tracer = await client.nodeTracer.create({
projectName: "qa_assistant",
enableEvaluation: true,
});
const answerQuestion = tracer.observe(async function (input: string) {
const output = await processInput(input);
tracer.asyncEvaluate(
client.scorers.builtIn.answerRelevancy(),
Example.create({
input,
actual_output: output,
expected_output: "expected",
}),
);
return output;
});asyncTraceEvaluate()
Asynchronously evaluates an entire trace using a scorer.
asyncTraceEvaluate(scorer: Scorer, model?: string): voidParameters
scorerrequired
:Scorermodel
:stringReturns
void - evaluation is triggered asynchronously
Example
import { Judgeval } from "judgeval";
const client = Judgeval.create();
const tracer = await client.nodeTracer.create({
projectName: "workflow_orchestrator",
enableEvaluation: true,
});
const executeWorkflow = tracer.observe(async function () {
const result = await performComplexOperation();
tracer.asyncTraceEvaluate(client.scorers.builtIn.faithfulness(), "gpt-4");
return result;
});setAttribute()
Sets a single attribute on the current active span.
setAttribute(key: string, value: unknown): voidParameters
keyrequired
:stringvaluerequired
:unknownExample
tracer.setAttribute("user.id", "12345");
tracer.setAttribute("request.params", { query: "hello" });setAttributes()
Sets multiple attributes on the current active span.
setAttributes(attributes: Record<string, unknown>): voidParameters
attributesrequired
:Record<string, unknown>Example
tracer.setAttributes({
"user.id": "12345",
"request.method": "POST",
"request.path": "/api/chat",
});setSpanKind()
Sets the kind of the current active span.
setSpanKind(kind: string): voidParameters
kindrequired
:stringExample
tracer.setSpanKind("llm");setLLMSpan()
Sets the current span kind to 'llm'.
setLLMSpan(): voidExample
const processLLMCall = tracer.observe(async function (prompt: string) {
tracer.setLLMSpan();
const response = await callOpenAI(prompt);
return response;
});setToolSpan()
Sets the current span kind to 'tool'.
setToolSpan(): voidExample
const searchTool = tracer.observe(async function (query: string) {
tracer.setToolSpan();
return await searchDatabase(query);
});setGeneralSpan()
Sets the current span kind to 'span'.
setGeneralSpan(): voidExample
const processData = tracer.observe(async function (data: unknown) {
tracer.setGeneralSpan();
return transform(data);
});setInput()
Sets the input attribute on the current span.
setInput(input: unknown): voidParameters
inputrequired
:unknownExample
tracer.setInput({ prompt: "What is the capital of France?" });setOutput()
Sets the output attribute on the current span.
setOutput(output: unknown): voidParameters
outputrequired
:unknownExample
tracer.setOutput({ response: "Paris" });getConfiguration()
Gets the current tracer configuration.
getConfiguration(): TracerConfigurationReturns
TracerConfiguration - Current configuration object
getProjectId()
Gets the resolved project ID.
getProjectId(): string | nullReturns
string | null - The project ID or null if not resolved
getSerializer()
Gets the current serializer function.
getSerializer(): SerializerReturns
Serializer - The serializer function
getSpanExporter()
Gets or creates the span exporter for sending traces to Judgment.
async getSpanExporter(): Promise<JudgmentSpanExporter | NoOpSpanExporter>Returns
Promise<JudgmentSpanExporter | NoOpSpanExporter> - The span exporter instance
forceFlush()
Flushes all pending spans to the ingest endpoint.
async forceFlush(): Promise<void>Example
await tracer.forceFlush();shutdown()
Shuts down the tracer and flushes any pending data.
async shutdown(): Promise<void>Example
await tracer.shutdown();