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

# Ollama

> Wrap the Ollama client, or the OpenAI client pointed at Ollama, to meter and gate calls to models you host yourself.

Ollama runs open models on your own hardware. No vendor bills you per token, so with Ollama you meter and limit the use of your own capacity, for example to give each customer or agent a share of it.

## Set up

Wrap an instance of `Ollama` from the `ollama` package. The SDK recognizes it automatically.

```typescript theme={null}
import { Ollama } from 'ollama';

const ollama = stigg.wrap(new Ollama({ host: 'http://127.0.0.1:11434' }));

// A regular call. Usage is reported when the response arrives.
await ollama.chat({
  model: 'llama3.3',
  messages: [{ role: 'user', content: 'Summarize this ticket' }],
  stiggContext: { customerId: 'customer-acme', entitlement: { currencyId: 'credits' } },
});

// A streamed call. Usage is reported once, when the stream finishes.
const stream = await ollama.chat({
  model: 'llama3.3',
  messages: [{ role: 'user', content: 'Summarize this ticket' }],
  stream: true,
  stiggContext: { customerId: 'customer-acme', entitlement: { currencyId: 'credits' } },
});
for await (const part of stream) {
  // show the text to the user as it arrives
}
```

Both `chat` and `generate` are covered.

### With the OpenAI client

Ollama also serves an API in OpenAI's format. To call it with an `OpenAI` client, point the `baseURL` at Ollama and wrap it:

```typescript theme={null}
import OpenAI from 'openai';

const ollama = stigg.wrap(new OpenAI({ baseURL: 'http://localhost:11434/v1', apiKey: 'ollama' }));
```

Ollama does not check the API key, but the `OpenAI` client requires one, so pass any string. Calls through the `OpenAI` client are metered with the OpenAI recipe: they are reported under the `openai.chat.completions.create` and `openai.responses.create` event names, with the same metrics as [OpenAI](/documentation/ai-agents/llm-wrappers/providers/openai#what-is-reported). See Ollama's [OpenAI compatibility docs](https://docs.ollama.com/api/openai-compatibility).

## What is reported

The responses of Ollama's own API include the number of tokens each call processed. The wrapper reads them and reports them to Stigg as the metrics below. For details, see Ollama's [API docs](https://docs.ollama.com/api).

| Method | Event name |
| - | - |
| `chat` | `ollama.chat` |
| `generate` | `ollama.generate` |

| Metric | What it counts |
| - | - |
| `prompt_eval_count` | Input tokens |
| `eval_count` | Output tokens |
| `total_tokens` | The sum of the two, added up by the SDK because Ollama does not report a total |

`total_tokens` includes the other two, so price the total or the parts, never both.

Other Ollama methods are not metered: `embed` measures a different kind of usage, and `pull`, `push` and `create` manage models rather than run them. Ollama's `webSearch` and `webFetch` are billed by ollama.com but return no token counts, so they are not metered either.


## Related topics

- [Supported providers and gateways](/documentation/ai-agents/llm-wrappers/supported-providers.md)
- [OpenAI-compatible endpoints](/documentation/ai-agents/llm-wrappers/providers/openai-compatible.md)
- [Agent SDK](/api-and-sdks/references/sdk/backend/graphql/agent-sdk.md)
- [JavaScript](/api-and-sdks/changelog/agent-sdk/javascript.md)


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