Skip to main content
Ollama is an open-source tool for running large language models locally. Braintrust traces chat, generate, and embedding calls from the ollama npm package.

Setup

Install Braintrust alongside the Ollama client.
.env

Auto-instrumentation

To trace Ollama without modifying your application code, run your app with Braintrust’s import hook. The hook patches the ollama client at runtime.
1

Initialize Braintrust and call Ollama

2

Run with the import hook

The auto-instrumentation example uses plain JavaScript so node --import can run the file directly. The Braintrust APIs work the same in TypeScript projects — compile your TypeScript to JavaScript, then run the compiled file with the import hook.
If you’re using a bundler, see Trace LLM calls for plugin and loader setup.

Manual instrumentation

To trace Ollama manually, wrap the client yourself with wrapOllama().

What Braintrust traces

Braintrust traces chat, generate, and embedding calls from the ollama client:
  • Chat spans (ollama.chat), with messages and request parameters as input and the response message as output. Tool calls are normalized to the OpenAI function-call format.
  • Generate spans (ollama.generate), with the prompt (plus optional system prompt and images) as input and the generated text as output. Thinking content is captured as reasoning.
  • Embedding spans (ollama.embed), with the input text as input and the first embedding’s vector length as output.
  • Token usage metrics (prompt_tokens and completion_tokens) from Ollama’s prompt_eval_count and eval_count response fields.
  • Model name and request options (temperature, top_p, max_tokens, and others) as span metadata.
  • Image inputs are captured as attachments.
  • Errors captured on every span.

Resources