# Add telemetry for context compaction \(\#58928\) · gitcafe/zed

[View on GitCafe](https://git.cafe/gitcafe/zed/commit/fa4ae33e20791134909b4d5bb035f8d34cae66e9)

Repository: [gitcafe/zed](https://git.cafe/gitcafe/zed)

Visibility: public

Requested revision: fa4ae33e20791134909b4d5bb035f8d34cae66e9

Requested commit: fa4ae33e20791134909b4d5bb035f8d34cae66e9

Commit: fa4ae33e20791134909b4d5bb035f8d34cae66e9

Tree: a78bd63ef486dd77f49434b311af3b869f603423

Author: Richard Feldman

Committer: GitHub

## Message

```
Add telemetry for context compaction (#58928)

Adds an `"Agent Compaction Completed"` telemetry event that fires for
both threshold-triggered auto-compaction and the manual `/compact`
command (both already behind the `handoff` flag). The event records the
model, model provider, thinking effort, the model's context window size,
the token counts immediately before and after compaction, whether the
compaction succeeded/failed/canceled (with the error string on failure),
the user's configured auto-compaction threshold (both the raw form and
the resolved absolute token count), whether auto-compaction is enabled,
and the number of retries.

Both compaction paths funnel through `stream_compaction`, so a
`CompactionTelemetry` snapshot is captured when a compaction starts and
stashed on the thread. On success, emission is deferred until the next
completion request reports usage, so `tokens_after` reflects the real
post-compaction context size rather than an estimate (we have no
token-counting API, and it's safe to assume a compaction is always
followed by another request). On failure or cancellation the event fires
immediately with no `tokens_after`. Retries are accumulated across
attempts so a single logical compaction produces exactly one event.

While wiring up retry counting I noticed the existing auto-compaction
retry path in `run_turn_internal` never increments `attempt`, so a
repeatedly-failing retryable compaction could retry without bound. I
left that behavior untouched as out of scope, but the new `retries`
field will surface it if it happens.

Release Notes:

- N/A

---------

Co-authored-by: Bennet Bo Fenner <bennetbo@gmx.de>
```

## Parents

- [d989c7c5cdd057de2375a55bdc109ff61409801c](https://git.cafe/gitcafe/zed/commit/d989c7c5cdd057de2375a55bdc109ff61409801c?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zed/tree/fa4ae33e20791134909b4d5bb035f8d34cae66e9?format=markdown)
