Usage Tracking
The AI Context module tracks how context items are used by consumers, giving you visibility into which items are being selected and where.
Scope of usage tracking
Usage tracking is intentionally lightweight. It records which context items consumers select, not full AI request logs.
Usage tracking is disabled by default. Enable it at
/admin/config/ai/context/settings/usage when you want lightweight
observability into which context items consumers select.
Access and redaction
Usage pages require View AI Context Usage. That permission alone does not allow viewing linked context items, their content, or target entities the user cannot otherwise access.
Listing and detail views redact inaccessible context items and target
entities (for example Context item {id} (no access)). See
Access boundaries for the full model.
How it works
When a consumer runs and context is selected, the module records:
- Which context items were included in the prompt
- Which consumer used them (
consumer_idplus derivedconsumer_type) - The runner ID when the consumer has one (agent runs). Generic chat
injection records
NULLbecause those requests have no runner. - The paths where the consumer was triggered. Each record keeps the last 20 paths. A path that is longer than 255 characters is cut to 255 characters and ends with "…". A path hint that is longer than 2,048 characters is ignored. See Site Sections on API-proxy routes.
- Which tools were used during an agent run
- Which entities were created or modified
Usage records are stored as ai_context_usage entities.
A record that was saved before the path limits is cut the next time a path is added to it.
Usage tracking does not stop the consumer. When recording usage fails, for example because a save fails, the error is logged and the AI request or agent run continues. A fatal PHP error, for example the memory limit, is not caught.
A record names the items that were delivered. When the token budget
has no room for a selected item, the renderer leaves it out, and no
usage is recorded for it: it never reached the model. An item whose
content was only shortened is recorded, because part of it did. The
renderer decides what was delivered, so a TEXT_RENDERED subscriber that
changes item text does not change the records.
AiContextSelectionResult::getTruncatedItemIds() covers both dropped
and shortened items, and getOmittedItemIds() is the list of items that
were dropped.
The two push subscribers create usage records, and agent lifecycle subscribers can update them. Pull calls do not create or update usage records. See Background jobs and queue workers.
Database indexes
The context_item_id field is an entity reference, so Drupal creates its
target ID index automatically. In raw database output, this index may appear
with a generated hashed name such as ai_context_usage__0ca4136628.
The module also adds explicit indexes for runner_id, changed,
(entity_item_type, entity_item_id), and
(context_item_id, consumer_id).
Viewing usage data
The Usage pages are only reachable while tracking is on. With Enable usage
tracking off, /admin/config/ai/context/usage and the record pages return
403, even for a user holding View AI Context Usage.
Usage listing
Go to /admin/config/ai/context/usage to see a Views-based listing of all usage
records, with these columns:
| Column | Shows |
|---|---|
| Context Item | The context item that was selected |
| Consumer | The consumer that selected it |
| Entity | The entity being worked on, when there was one |
| Tools Used | Tools the consumer ran during execution |
| Last Used | When the combination was last recorded |
| View | Link to the usage record details |
There is one record for each combination of context item, consumer and entity being worked on (entity type and ID, or no entity). Using the same combination again updates the existing record. Working on a different entity, or without an entity, uses a separate record.
Last Used is written on every use, including a repeat use through the same consumer on the same route. Cleanup reads it, so a record that is still in use is kept. The record page also shows Created, when the record was first written. Both are request start times, so a long run stamps when it started.
Usage record detail
Click on any usage record to see its full details at
/admin/config/ai/context/usage/{id}, including:
- The consumer that ran
- The context items that were selected
- The last 20 paths where it happened, oldest first
- Tools used during execution
- Entities that were created or modified
Usage settings
Configure usage tracking at /admin/config/ai/context/settings/usage:
- Enable/disable usage tracking
- Maximum records -- cap the total number of usage records stored
- Maximum age -- automatically purge records older than a configured duration
Automatic cleanup
Old usage records are purged during cron based on the configured limits. Both rules read the record's Last Used time, not Created:
- Records whose last use is older than the maximum age are deleted first
- If the total count still exceeds the maximum records setting, the excess records with the oldest last use are removed
Clearing usage data
To manually clear all usage records, go to
/admin/config/ai/context/settings/usage/clear and confirm the deletion.
If a consumer fails because its usage records are very large, do these steps:
- Turn off usage tracking at
/admin/config/ai/context/settings/usage. - Clear the records at
/admin/config/ai/context/settings/usage/clear. - If the clear page also fails, empty the
ai_context_usagetable, for example withdrush sql:query "TRUNCATE TABLE ai_context_usage". Then rebuild the caches.