> ## Documentation Index
> Fetch the complete documentation index at: https://www.getmaxim.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Generations

> Use generations to log individual calls to Large Language Models (LLMs). Each trace or span can contain multiple generations, allowing you to track and monitor all LLM interactions within your AI application.

## How to Track Generations?

Each trace/span can contain multiple generations.

<div className="w-full flex justify-end -mb-11">
  <LanguageSwitcher />
</div>

<Steps>
  <Step title="Send and record LLM request">
    <CodeGroup>
      ```typescript JS/TS theme={null}
      // 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({ ... })
      ```

      ```python Python theme={null}
      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(...)
      ```

      ```go Go theme={null}
      // 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{...})
      ```

      ```java Java theme={null}
      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(...));
      ```
    </CodeGroup>
  </Step>

  <Step title="Record LLM response">
    <CodeGroup>
      ```typescript JS/TS theme={null}
      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
          }
      }); 
      ```

      ```python Python theme={null}
      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
          }
      })
      ```

      ```go Go theme={null}
      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,
      	},
      })
      ```

      ```java Java theme={null}
      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)
      ));
      ```
    </CodeGroup>
  </Step>
</Steps>

<img src="https://mintcdn.com/maximai/fHnWe0mnvuD5228y/images/docs/tracing/via-sdk/generation.png?fit=max&auto=format&n=fHnWe0mnvuD5228y&q=85&s=7dbeb9e116cd7973d83b1b1c970ff637" alt="Generations" width="2392" height="1704" data-path="images/docs/tracing/via-sdk/generation.png" />

<Note>[Schedule a demo](https://getmaxim.ai/demo) to see how Maxim AI helps teams ship reliable agents.</Note>
