Skip to content

AI

AiContextMeter

Bit.BlazorUI.AITokenMeter

BitAiContextMeter shows how much of the context window of the model the conversation is using - a label, a meter and a percentage. Running out of context is the failure users understand least: the model simply starts forgetting. So the meter turns to the warning color at the Warn mark and to the error color at the Critical one, and says so once through a polite live region as it crosses each. It reads the tokens and the window off a conversation, takes them directly, or takes the window from a model, and counts what is about to be added (the prompt being typed) on top.

Notes

To use this component, you need to install the Bit.BlazorUI.AI(opens in a new tab) nuget package, as described in the Optional steps of the Getting started page.

Nothing is rendered while no window is known. The tokens a conversation uses are those of its latest assistant message - its prompt plus its output, which the next request sends again - and its window the ContextWindow of the BitAiGenerationMetadata a transport reports (see AiTokenUsageView). Accessibility: the bar is a meter (role="meter", a reading that moves either way rather than a task that finishes), named "Context" and read out as the count of tokens ("1,500 / 10,000 tokens"), which is also its tooltip unless a title attribute is given, and the warnings (BitAiStrings.ContextWarning, ContextCritical) are announced once each. These texts, the label and the count (Context, ContextUsage) included, come from the strings in force, which a BitAiOptionsProvider localizes. See it beside the model picker in the Models and usage scenario.

Usage

Every example is live. Open its code to see exactly what produced the component running underneath.

Basic

Used tokens out of a Window: a meter with the share of the window used. The exact count is its tooltip.

Context
32%

Warn & Critical

Drag the slider across the marks. Past Warn (0.8 of the window by default) the meter and its words turn to the warning color, past Critical (0.95) to the error color, and a polite live region says so once as it crosses each; past the window the bar stays full while the percentage goes on. The second meter has marks of its own.


Context
75%

Context
75% The context window is almost full

Pending tokens

PendingTokens counts an estimate of what is about to be added - the prompt being typed, a file about to be attached - on top of what is used, so the warning comes before the request that would overflow the window. Type a few more words, or attach the report.



Context
79%

Model

Without a Window (or a conversation that reports one), the meter takes the ContextWindow of the Model: the same 30,000 tokens fill a little of a large window and nearly all of a small one.

Demo Large
Context
15%
Demo Fast
Context
23%
Local Small
Context
93% The context window is filling up

Conversation

Given a Conversation, the meter reads the tokens and the window off it (Measure): the prompt plus the output of the latest assistant message, which the next request sends again whole - not the sum of every turn, which would count the early turns once per later one. Here a scripted transport of this site answers on a model with a window of 340 tokens - the Model supplies it until the conversation reports one: send a few messages and watch the meter cross its marks.


Context
0%

Messages: 0

Disabled

A disabled meter steps back - for a composer that cannot send right now: its label and percentage take the disabled color, past the marks too, and its bar is dimmed. It still reads the tokens, names them in its tooltip and announces the marks it crosses.

Context
32%

Context
96% The context window is almost full

Cascading parameters

BitParams with a BitAiContextMeterParams sets the marks (Warn, Critical) and the look of every meter inside it; the figures - the conversation, the model and the counts - stay each meter's own. A meter's own value still wins: under the cascaded marks of 0.5 and 0.6, 50% is a warning and 70% critical, while the last meter sets its own Critical of 0.95 and is only warned at the same 70%.

Context
50% The context window is filling up

Context
70% The context window is almost full

Context
70% The context window is filling up

Style & Class

Style and Class reach the root, Styles and Classes the label, the meter (a BitProgressClassStyles), the percentage and the hidden live region. The public --bit-AiContextMeter-* variables re-skin the words and the layout, the warning and critical colors of the words included; the bar is a BitProgress, which the --bit-Progress-* variables restyle.

Context
32%

Context
32%

Context
85% The context window is filling up

RTL

In right-to-left text the label, the meter and the percentage are laid out from right to left, and the bar fills from the right - whether the direction comes from Dir or from a dir attribute around the meter.

Context
32%

Context
85% The context window is filling up

CSS variables

The public custom properties this component reads off its root, for what no parameter covers.

Every variable is read with a fallback and never declared by the component, so it inherits like any other custom property: set one on :root (or in a [bit-theme] block) to restyle every instance, on any ancestor to restyle the ones inside it, or on the Style of one instance to restyle it - and any instance nested inside it, which inherits the value too; a parameter changes one alone, as does the real CSS property on the instance's Style / Styles when that property does not inherit (opacity: 1 keeps one disabled instance undimmed without undimming a disabled one inside it), while an inherited one such as color or font-family also reaches the nested instances that do not set it themselves. Leave a variable unset and the component falls back to the theme token beside it.

BitAiContextMeter CSS variables

Name Default value Description
--bit-AiContextMeter-color var(--bit-clr-fg-sec) The label and the percentage.
--bit-AiContextMeter-warning-color var(--bit-clr-wrn-fg) The label and the percentage past the Warn mark. The bar is drawn in the warning role there, which the state of the reading says rather than a choice of the app's.
--bit-AiContextMeter-critical-color var(--bit-clr-err-fg) The label and the percentage past the Critical mark, where the bar is drawn in the error role.
--bit-AiContextMeter-font-size var(--bit-tpg-fs-xs) Size of the label and the percentage.
--bit-AiContextMeter-gap spacing(1.5) Room between the label, the bar and the percentage.
--bit-AiContextMeter-meter-max-width spacing(24) Width the bar grows to where there is room, and asks for where the meter is sized to its content (in a flex row, say).
--bit-AiContextMeter-track-color var(--bit-Progress-track-color, var(--bit-clr-brd-sec)) The empty part of the bar. The secondary border color by default rather than the background a BitProgress draws its track in, so it stands off the surface the meter sits on.

API

Every parameter, public member, sub-class and enum this component exposes.

BitAiContextMeter parameters

Name Type Default value Description
Classes BitAiContextMeterClassStyles? null Custom CSS classes for different parts of the context meter.
Conversation BitAiConversation? null Reads the used tokens and the window from a conversation (see Measure) instead of taking them directly: the used tokens it reports win over Used, while an explicit Window wins over the window it reports.
Critical double 0.95 The fraction of the window at which the meter turns to its critical (error) styling and announces BitAiStrings.ContextCritical once.
Model BitAiModel? null Supplies the window (its ContextWindow) when neither Window nor the Conversation carries one.
PendingTokens long 0 An estimate of the tokens about to be added - the prompt being typed, a file about to be attached - counted on top of what the conversation has used, so the meter warns before the request that would overflow the window.
Styles BitAiContextMeterClassStyles? null Custom CSS styles for different parts of the context meter.
Used long 0 The tokens used so far. Overridden by the Conversation when it carries usage.
Warn double 0.8 The fraction of the window at which the meter turns to its warning styling and announces BitAiStrings.ContextWarning once.
Window long? null The size of the context window in tokens. When null, the window the Conversation reports is used, else the ContextWindow of the Model; nothing is rendered while none is known.

BitAiContextMeter public members

Name Type Default value Description
Measure (long? Used, long? Window) BitAiContextMeter.Measure(BitAiConversation conversation), static: works out how full the context is from a conversation - the tokens the next request will carry (the prompt plus the output of its latest assistant message that reports usage) and the window (the newest ContextWindow its messages report), either of which is null when the conversation does not report it. Summing every turn's usage instead, as a total-cost readout does, counts the early turns once per later turn and runs the meter to the top long before the model forgets anything.

BitComponentBase parameters

Name Type Default value Description
AriaLabel string? null Gets or sets the accessible label for the component, used by assistive technologies.
Class string? null Gets or sets the CSS class name(s) to apply to the rendered element.
Dir BitDir? null Gets or sets the text directionality for the component's content.
Disabled bool false Gets or sets a value indicating whether the component is disabled and cannot respond to user interaction.
ForceAnimation bool false Gets or sets a value indicating whether the component's animations play at their full duration even when reduced motion is requested.
HtmlAttributes Dictionary<string, object> new Dictionary<string, object>() Captures additional HTML attributes to be applied to the rendered element, in addition to the component's parameters. An attribute written on the component wins over an entry of the same name.
Id string? null Gets or sets the unique identifier for the component's root element.
Style string? null Gets or sets the CSS style string to apply to the rendered element.
TabIndex string? null Gets or sets the tab order index for the component when navigating with the keyboard.
Visibility BitVisibility BitVisibility.Visible Gets or sets the visibility state (visible, hidden, or collapsed) of the component.

BitComponentBase public members

Name Type Default value Description
UniqueId Guid Guid.NewGuid() Gets the readonly unique identifier for the component's root element, assigned when the component instance is constructed.
RootElement ElementReference Gets the reference to the root HTML element associated with this component.

BitAiContextMeterClassStyles properties

Name Type Default value Description
Root string? null Custom CSS classes/styles for the root element of the BitAiContextMeter.
Label string? null Custom CSS classes/styles for the label ("Context") of the BitAiContextMeter.
Meter BitProgressClassStyles? null Custom CSS classes/styles for the BitProgress meter of the BitAiContextMeter.
Percent string? null Custom CSS classes/styles for the percentage readout of the BitAiContextMeter.
Status string? null Custom CSS classes/styles for the visually hidden live region of the BitAiContextMeter.

BitVisibility enum

Name Value Description
Visible 0 The content of the component is visible.
Hidden 1 The content of the component is hidden, but the space it takes on the page remains (visibility:hidden).
Collapsed 2 The component is hidden (display:none).

BitDir enum

Name Value Description
Ltr 0 Ltr (left to right) is to be used for languages that are written from the left to the right (like English).
Rtl 1 Rtl (right to left) is to be used for languages that are written from the right to the left (like Arabic).
Auto 2 Auto lets the user agent decide. It uses a basic algorithm as it parses the characters inside the element until it finds a character with a strong directionality, then applies that directionality to the whole element.

Feedback

Found a mistake, a gap, or something that could be clearer? Every page and every component is one click from its source.