How to Track Generations?
Each trace/span can contain multiple generations.1
Send and record LLM request
// Initialize a trace with a unique ID
const trace = logger.trace({id: "trace-id"});
// Adding a generation
const generation = trace.generation({
id: "generation-id",
name: "customer-support--gather-information",
provider: "openai",
model: "gpt-4o",
modelParameters: { temperature: 0.7 },
messages: [
{ "role": "system", "content": "you are a helpful assistant who helps gather customer information" },
{ "role": "user", "content": "My internet is not working" },
],
});
// Note: Replace 'trace.generation' with 'span.generation' when creating generations within an existing span
// Execute the LLM call
// const aiCompletion = await openai.chat.completions.create({ ... })
generation = trace.generation({
"id":"generation-id",
"name":"customer-support--gather-information",
"provider":"openai",
"model":"gpt-4o",
"messages":[
{ "role": "system", "content": "you are a helpful assistant who helps gather customer information" },
{ "role": "user", "content": "My internet is not working" },
],
"model_parameters":{"temperature": 0.7},
})
# Note: Replace 'trace.generation' with 'span.generation' when creating generations within an existing span
# Execute the LLM call
# aiCompletion = client.chat.completions.create(...)
// Valid provider names are: openai, azure, bedrock, anthropic.
// Please reach out to the support if you are using any other provider here
generation := trace.AddGeneration(&logging.GenerationConfig{
Id: "generation-id",
Name: "customer-support--gather-information",
Provider: "openai",
Model: "gpt-4o",
Messages: []logging.CompletionRequest{
{
Role: "system",
Content: "you are a helpful assistant who helps gather customer information",
},
{
Role: "user",
Content: "My internet is not working",
},
},
ModelParameters: map[string]interface{}{"temperature": 0.7},
})
// Note: Replace 'trace.AddGeneration' with 'span.AddGeneration' when creating generations within an existing span
// Execute the LLM call
// aiCompletion, err := client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{...})
Generation generation = trace.addGeneration(new GenerationConfig(
"generation-id",
"customer-support--gather-information",
"openai",
"gpt-4o",
Arrays.asList(
new Message("system", "you are a helpful assistant who helps gather customer information"),
new Message("user", "My internet is not working"),
),
Map.of("temperature", 0.7),
));
// Note: Replace 'trace.addGeneration' with 'span.addGeneration' when creating generations within an existing span
// Execute the LLM call
// ChatCompletionResult aiCompletion = openAiService.createChatCompletion(new ChatCompletionRequest(...));
2
Record LLM response
generation.result({
id: "chatcmpl-123",
object: "chat.completion",
created: Date.now(),
model: "gpt-4o",
choices: [{
index: 0,
message: {
role: "assistant",
content: "Apologies for the inconvenience. Can you please share your customer id?"
},
finish_reason: "stop",
logprobs: null,
}],
usage: {
prompt_tokens: 100,
completion_tokens: 50,
total_tokens: 150
}
});
generation.result({
"id": "chatcmpl-123",
"object": "chat.completion",
"created": int(time.time()),
"model": "gpt-4o",
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": "Apologies for the inconvenience. Can you please share your customer id?"
},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 100,
"completion_tokens": 50,
"total_tokens": 150
}
})
generation.SetResult(map[string]interface{}{
"id": uuid.New().String(),
"model": "gpt-4o",
"created": time.Now().Unix(),
"choices": []map[string]interface{}{
{
"message": map[string]interface{}{
"role": "assistant",
"content": "Hello, world!",
},
},
},
"usage": map[string]interface{}{
"prompt_tokens": 10,
"completion_tokens": 10,
"total_tokens": 20,
},
})
generation.setResult(new ChatCompletionResult(
"chatcmpl-123",
"chat.completion",
System.currentTimeMillis() / 1000L,
"gpt-4o",
Arrays.asList(new ChatCompletionChoice(
0,
new Message("assistant", "Apologies for the inconvenience. Can you please share your customer id?"),
"stop"
)),
new Usage(100, 50, 150)
));

Schedule a demo to see how Maxim AI helps teams ship reliable agents.