> ## Documentation Index
> Fetch the complete documentation index at: https://braintrust.dev/docs/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.

<View title="TypeScript" icon="https://img.logo.dev/typescriptlang.org?token=pk_BdcHD9e5SCW3j1rnJkNyMQ">
  <h2 id="setup-typescript">
    Setup
  </h2>

  Install Braintrust alongside the Ollama client.

  <CodeGroup>
    ```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
    ```
  </CodeGroup>

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

  <h2 id="auto-instrumentation-typescript">
    Auto-instrumentation
  </h2>

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

  <Steps>
    <Step title="Initialize Braintrust and call Ollama">
      <CodeGroup>
        ```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);
        ```
      </CodeGroup>
    </Step>

    <Step title="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, then run the compiled file with the import hook.

      <Note>
        If you're using a bundler, see [Trace LLM calls](/docs/instrument/trace-llm-calls#auto-instrumentation) for plugin and loader setup.
      </Note>
    </Step>
  </Steps>

  <h2 id="manual-instrumentation-typescript">
    Manual instrumentation
  </h2>

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

  <CodeGroup>
    ```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);
    ```
  </CodeGroup>

  <h2 id="what-traced-typescript">
    What Braintrust traces
  </h2>

  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.

  <h2 id="resources-typescript">
    Resources
  </h2>

  * [`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)
</View>
