How to beginner · 3 min read

How to log LLM prompts with wandb

Quick answer
Use the wandb Python SDK to log LLM prompts and completions by calling wandb.log() with your prompt and response data. Initialize wandb with wandb.init() and log each prompt-response pair as a dictionary for easy experiment tracking.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • wandb account and API key
  • pip install openai>=1.0 wandb

Setup

Install the required packages and set environment variables for your OpenAI and wandb API keys.

  • Install packages: pip install openai wandb
  • Set environment variables in your shell or IDE:
    export OPENAI_API_KEY=your_openai_key
    export WANDB_API_KEY=your_wandb_key
bash
pip install openai wandb

Step by step

This example shows how to initialize wandb, call the OpenAI API to generate a completion, and log the prompt and completion text to wandb for tracking.

python
import os
from openai import OpenAI
import wandb

# Initialize wandb project
wandb.init(project="llm-prompt-logging", entity="your_wandb_entity")

# Initialize OpenAI client
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

# Define prompt
prompt = "Explain the benefits of using wandb for ML experiments."

# Call OpenAI chat completion
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": prompt}]
)

# Extract completion text
completion = response.choices[0].message.content

# Log prompt and completion to wandb
wandb.log({
    "prompt": prompt,
    "completion": completion
})

print("Logged prompt and completion to wandb.")
output
Logged prompt and completion to wandb.

Common variations

You can log additional metadata such as model name, token usage, or timestamps. For async or streaming calls, log partial outputs incrementally. You can also log multiple prompts in a batch or use different LLM providers by adapting the client initialization.

python
import os
from openai import OpenAI
import wandb

wandb.init(project="llm-prompt-logging")
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

prompts = [
    "What is semantic search?",
    "Summarize the latest AI trends."
]

for prompt in prompts:
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": prompt}]
    )
    completion = response.choices[0].message.content
    wandb.log({
        "prompt": prompt,
        "completion": completion,
        "model": "gpt-4o-mini"
    })

print("Logged multiple prompts and completions.")
output
Logged multiple prompts and completions.

Troubleshooting

  • If wandb logs do not appear, ensure WANDB_API_KEY is set and wandb.init() is called before logging.
  • For API errors, verify your OpenAI API key and model name are correct.
  • Check your internet connection and firewall settings if logging fails.

Key Takeaways

  • Initialize wandb with wandb.init() before logging any data.
  • Log LLM prompts and completions as key-value pairs using wandb.log() for clear experiment tracking.
  • Include metadata like model name and timestamps to enhance your logs.
  • Batch logging multiple prompts improves efficiency for large-scale experiments.
  • Verify environment variables and API keys to avoid common connection or authentication errors.
Verified 2026-04 · gpt-4o-mini
Verify ↗