# Context meter

> How full a model's context window is: a ring and `48k / 200k`. A native Pyzed component for Python desktop GUIs, with a live preview, code examples and its API.

Source: https://albakore.github.io/pyzed/library/context-meter/

:::tip[In plain words]
A little ring that fills up as a chat with an AI gets longer, like a fuel gauge.
:::

`ui.context_meter(used, limit)` shows how full an AI model's context window is: a small ring and `48k / 200k`. It is gray below 80%, yellow from 80% and red from 95%, and its tooltip gives the exact counts. `ui.format_tokens(count)` writes a count the short way: `950`, `4.5k`, `48k`, `1.25M`.

`ui.context_meter` · Category: [AI agents](https://albakore.github.io/pyzed/library/#ai-agents) · [Full reference →](https://albakore.github.io/pyzed/reference/ui/#context_meter)

**Code:**

```python
pz.row([
    ui.context_meter(48_000, 200_000),
    ui.context_meter(170_000, 200_000),
    ui.context_meter(196_000, 200_000),
], spacing=24, align="center")
```

:::note[New]
This component was added recently. Tell us how it works for you in the [issue tracker](https://github.com/albakore/pyzed/issues).
:::

## When to use

- In the toolbar or the input box of an agent's chat, so the user sees when a conversation gets long.

## Usage

```python
import pyzed as pz
from pyzed import ui

pz.row([
    ui.context_meter(48_000, 200_000),
    ui.context_meter(170_000, 200_000),
    ui.context_meter(196_000, 200_000),
], spacing=24, align="center")
```

## Variants

:::note[About these captures]
Each capture is recorded by pyzed itself, off-screen at 2x, in One Dark or One Light to match this site's theme, and cropped to the component so it shows at its real size. The pointer hovers, presses or types into it; a component that does not react to the pointer cycles through a few themes. With reduced motion you see the first frame. Open **Code** under a capture to see the snippet that draws it; free names such as `on_save` stand for your own callbacks.
:::

### Level

Drawn as the pointer shows it: tooltips only open on hover.

```python
ui.context_meter(48_000, 200_000)
```

```python
ui.context_meter(170_000, 200_000)
```

```python
ui.context_meter(196_000, 200_000)
```

```python
ui.context_meter(48_000, 200_000)
```

### Short counts

`ui.format_tokens` returns the text the meter shows, for your own labels.

```python
ui.label(ui.format_tokens(950), mono=True)
```

```python
ui.label(ui.format_tokens(4_500), mono=True)
```

```python
ui.label(ui.format_tokens(48_000), mono=True)
```

```python
ui.label(ui.format_tokens(1_250_000), mono=True)
```

## Events

Context meter has no events of its own: it shows what you give it. Like every widget it also has the **18 common events** (`click`, `double_click`, `right_click`, `mouse_enter`, `drag`, `key_down`, `resize`…), attached with `.on(...)`: see [Events](https://albakore.github.io/pyzed/guides/events/) and [Events of every component](https://albakore.github.io/pyzed/library/events/).

## Properties and API

### `ui.context_meter`

```python
context_meter(used, limit, *, theme=None)
```

How full a model's context window is, like Zed's token counter: a small ring and `48k / 200k`, yellow past 80% and red past 95%.

| Property | Default | Description |
| --- | --- | --- |
| `used` | *required* | Tokens in use. |
| `limit` | *required* | The model's window. |
| `theme` | `None` | Theme. |

[Full reference →](https://albakore.github.io/pyzed/reference/ui/#context_meter)

### `ui.format_tokens`

```python
format_tokens(count)
```

A short token count: `950`, `4.5k`, `48k`, `1.25M`.

| Property | Default | Description |
| --- | --- | --- |
| `count` | *required* | Tokens. |

[Full reference →](https://albakore.github.io/pyzed/reference/ui/#format_tokens)

## Related

- [Donut](https://albakore.github.io/pyzed/library/donut/): A donut chart with a label in the middle.
- [Progress](https://albakore.github.io/pyzed/library/progress/): A thin progress bar in a semantic color.
- [Tool call](https://albakore.github.io/pyzed/library/tool-call/): One step of an AI agent: what it did, its result, and Allow or Reject.
