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

# Ollama

> Trace Ollama SDK calls in Braintrust to debug local model inference for chat, generate, and embedding calls

[Ollama](https://ollama.com) is an open-source tool for running large language models locally. Braintrust traces chat, generate, and embedding calls from the `ollama` npm package.

**TypeScript**

## Setup

Install Braintrust alongside the Ollama client.

```bash pnpm theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
pnpm add braintrust ollama
```

```bash npm theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
npm install braintrust ollama
```

```bash title=".env" theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
BRAINTRUST_API_KEY=<your-braintrust-api-key>

# For organizations on the EU data plane, use https://api-eu.braintrust.dev
# For self-hosted deployments, use your data plane URL
# BRAINTRUST_API_URL=<your-braintrust-api-url-here>
```

## 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**

```javascript title="trace-ollama-auto.js" theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
import { initLogger } from "braintrust";
import { Ollama } from "ollama";

initLogger({
  projectName: "My Project",
  apiKey: process.env.BRAINTRUST_API_KEY,
});

const ollama = new Ollama();

const response = await ollama.chat({
  model: "llama3.2",
  messages: [{ role: "user", content: "Why is the sky blue?" }],
});

console.log(response.message.content);
```

**2. Run with the import hook**

```bash theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
node --import braintrust/hook.mjs trace-ollama-auto.js
```

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 and run the compiled file with the import hook, or [run TypeScript directly](https://docs.braintrust.dev/instrument/trace-llm-calls.md#auto-instrumentation) with a TypeScript loader.

> [!NOTE]
>
> If you're using a bundler, see [Trace LLM calls](https://docs.braintrust.dev/instrument/trace-llm-calls.md#auto-instrumentation) for plugin and loader setup.

## Manual instrumentation

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

```typescript TypeScript theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
import { initLogger, wrapOllama } from "braintrust";
import { Ollama } from "ollama";

initLogger({
  projectName: "My Project",
  apiKey: process.env.BRAINTRUST_API_KEY,
});

const ollama = wrapOllama(new Ollama());

const response = await ollama.chat({
  model: "llama3.2",
  messages: [{ role: "user", content: "Why is the sky blue?" }],
});

console.log(response.message.content);
```

## 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

* [`ollama` on npm](https://www.npmjs.com/package/ollama)
* [Ollama documentation](https://ollama.com/docs)
* [Braintrust JavaScript SDK](https://github.com/braintrustdata/braintrust-sdk-javascript)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.