# NousResearch/hermes-agent · Fix: token reduction ignored as compression progress

## Open now

- Nothing open on GitHub.

Multiple fixes and bug reports addressing a flaw where compression was considered unsuccessful if message count stayed the same, even when token count decreased. The criterion for compression progress was updated to include token reduction, not just row count.

## TL;DR

- 9 member(s): 0 open issue(s), 2 closed issue(s), 0 open PR(s), 2 merged PR(s), 5 PR(s) closed without merging.
- A fix was merged to main in PR #50726. Verify it addresses your case.
- **Recommended:** read PR #50726 (and any competing PRs below) before filing. Confirm the merged change covers your symptom.

## Maintainer resolution verdict

- Internal-tracker verdict over this cluster's issue members: `resolved` · deciding PR(s): `pr-50726,pr-50740`. This is distinct from the fix status above. Weigh it yourself.

## How this cluster was grouped

- Link evidence (`STRUCTURAL_SPANNING`): every member is connected by structural evidence: shared changed files, or a closing/superseded reference.
- Pairwise link density `0.26`: the share of member pairs that carry a link, counting a structural link in full and a relevance-only link at 0.36. `1.00` means every pair is linked.

## Closed / duplicate issues

- #39548: \[Bug\]: Compression token savings ignored when message count is unchanged, causing false context exhaustion
- #39550: \[Bug\]: Compression token savings ignored when message count is unchanged, causing false context exhaustion

## Prior & competing PRs

- **#50726: fix(agent): count tokens, not just message rows, as compression progress (#23767, #39550)**. merged to main · fix tier `verified`
  - maintainer direction: category `merged`; implication `superseded`
  - files: `agent/conversation_loop.py`, `tests/run_agent/test_413_compression.py`
  - fix hunks:
  - `agent/conversation_loop.py`

    ```diff
    @@ -2983,6 +2983,7 @@ def _perform_api_call(next_api_kwargs):
                         agent._buffer_status(f"⚠️  Request payload too large (413) — compression attempt {compression_attempts}/{max_compression_attempts}...")
     
                         original_len = len(messages)
    +                    original_tokens = [redacted:secret-assignment]
                         messages, active_system_prompt = agent._compress_context(
                             messages, system_message, approx_tokens=[redacted:secret-assignment]
                             task_id=effective_task_id,
    ```

    ```diff
    @@ -2992,8 +2993,18 @@ def _perform_api_call(next_api_kwargs):
                         # messages to the new session, not skipping them.
                         conversation_history = None
     
    -                    if len(messages) < original_len:
    -                        agent._buffer_status(f"🗜️ Compressed {original_len} → {len(messages)} messages, retrying...")
    +                    # Re-estimate tokens after compression.  Same-message-count
    +                    # compression (tool-result pruning, in-place summarization)
    +                    # can materially reduce request size without reducing the
    +                    # message array.  (#39550)
    +                    new_tokens = [redacted:secret-assignment]
    +                    approx_tokens = [redacted:secret-assignment]  # update for downstream logging
    +
    +                    if len(messages) < original_len or (new_tokens > 0 and new_tokens < original_tokens * 0.95):
    +                        if len(messages) < original_len:
    +                            agent._buffer_status(f"🗜️ Compressed {original_len} → {len(messages)} messages, retrying...")
    +                        else:
    +                            agent._buffer_status(f"🗜️ Compressed ~{original_tokens:,} → ~{new_tokens:,} tokens, retrying...")
                             time.sleep(2)  # Brief pause between compression retries
                             _retry.restart_with_compressed_messages = True
                             break
    ```

    ```diff
    @@ -3139,6 +3150,7 @@ def _perform_api_call(next_api_kwargs):
                         agent._buffer_status(f"🗜️ Context too large (~{approx_tokens:,} tokens) — compressing ({compression_attempts}/{max_compression_attempts})...")
     
                         original_len = len(messages)
    +                    original_tokens = [redacted:secret-assignment]
                         messages, active_system_prompt = agent._compress_context(
                             messages, system_message, approx_tokens=[redacted:secret-assignment]
                             task_id=effective_task_id,
    ```

    ```diff
    @@ -3148,9 +3160,18 @@ def _perform_api_call(next_api_kwargs):
                         # messages to the new session, not skipping them.
                         conversation_history = None
     
    -                    if len(messages) < original_len or new_ctx and new_ctx < old_ctx:
    +                    # Re-estimate tokens after compression.  Same-message-count
    +                    # compression (tool-result pruning, in-place summarization)
    +                    # can materially reduce request size without reducing the
    +                    # message array.  (#39550)
    +                    new_tokens = [redacted:secret-assignment]
    +                    approx_tokens = [redacted:secret-assignment]  # update for downstream logging
    +
    +                    if len(messages) < original_len or (new_tokens > 0 and new_tokens < original_tokens * 0.95) or (new_ctx and new_ctx < old_ctx):
                             if len(messages) < original_len:
                                 agent._buffer_status(f"🗜️ Compressed {original_len} → {len(messages)} messages, retrying...")
    +                        elif new_tokens > 0 and new_tokens < original_tokens * 0.95:
    +                            agent._buffer_status(f"🗜️ Compressed ~{original_tokens:,} → ~{new_tokens:,} tokens, retrying...")
                             time.sleep(2)  # Brief pause between compression retries
                             _retry.restart_with_compressed_messages = True
                             break
    ```

    ```diff
    @@ -3159,13 +3180,13 @@ def _perform_api_call(next_api_kwargs):
                             agent._flush_status_buffer()
                             agent._vprint(f"{agent.log_prefix}❌ Context length exceeded and cannot compress further.", force=True)
                             agent._vprint(f"{agent.log_prefix}   💡 The conversation has accumulated too much content. Try /new to start fresh, or /compress to manually trigger compression.", force=True)
    -                        logger.error(f"{agent.log_prefix}Context length exceeded: {approx_tokens:,} tokens. Cannot compress further.")
    +                        logger.error(f"{agent.log_prefix}Context length exceeded: {new_tokens:,} tokens. Cannot compress further.")
                             agent._persist_session(messages, conversation_history)
                             return {
                                 "messages": messages,
                                 "completed": False,
                                 "api_calls": api_call_count,
    -                            "error": f"Context length exceeded ({approx_tokens:,} tokens). Cannot compress further.",
    +                            "error": f"Context length exceeded ({new_tokens:,} tokens). Cannot compress further.",
                                 "partial": True,
                                 "failed": True,
                                 "compression_exhausted": True,
    ```
  - `tests/run_agent/test_413_compression.py`

    ```diff
    @@ -440,6 +440,48 @@ def test_413_cannot_compress_further(self, agent):
             assert result.get("partial") is True
             assert "413" in result["error"]
     
    +    def test_413_retries_on_token_only_compression(self, agent):
    +        """Same message COUNT but fewer TOKENS must count as progress and retry.
    +
    +        Regression for #39550/#23767: tool-result pruning / in-place
    +        summarization can shrink request size without dropping the message
    +        count. The old gate (len(messages) < original_len) treated that as
    +        'cannot compress further' and aborted; the fix re-estimates tokens and
    +        retries when they drop materially.
    +        """
    +        err_413 = _make_413_error()
    +        ok_resp = _mock_response(content="OK after token-only compaction", finish_reason="stop")
    +        agent.client.chat.completions.create.side_effect = [err_413, ok_resp]
    +
    +        # 3 large messages in, 3 much smaller messages out (same count, far
    +        # fewer tokens) — exactly the token-only-progress case.
    +        prefill = [
    +            {"role": "user", "content": "x" * 4000},
    +            {"role": "assistant", "content": "y" * 4000},
    +            {"role": "user", "content": "z" * 4000},
    +        ]
    +
    +        with (
    +            patch.object(agent, "_compress_context") as mock_compress,
    +            patch.object(agent, "_persist_session"),
    +            patch.object(agent, "_save_trajectory"),
    +            patch.object(agent, "_cleanup_task_resources"),
    +        ):
    +            # Same message count (3) but ~10x smaller content → token drop.
    +            mock_compress.return_value = (
    +                [
    +                    {"role": "user", "content": "x" * 300},
    +                    {"role": "assistant", "content": "y" * 300},
    +                    {"role": "user", "content": "z" * 300},
    +                ],
    +                "compressed prompt",
    +            )
    +            result = agent.run_conversation("hello", conversation_history=prefill)
    +
    +        mock_compress.assert_called_once()
    +        assert result["completed"] is True
    +        assert result["final_response"] == "OK after token-only compaction"
    +
     
     class TestPreflightCompression:
         """Preflight compression should compress history before the first API call."""
    ```
- **#50740: fix(agent): count tokens, not just rows, as preflight compression progress (#23767, #39548)**. merged to main · fix tier `verified`
  - maintainer direction: category `merged`; implication `superseded`
  - files: `agent/turn_context.py`, `tests/agent/test_compression_progress.py`
- **#39673: fix(agent): improve context compression success criterion to include token reduction**. closed without merge
  - maintainer direction: category `implemented_elsewhere`; implication `superseded`; refs `33efff0d8`, `#50726`, `#50740`, `#39550`, `#39548`
- **#39900: fix(agent): continue preflight compression when tokens decrease but message count is unchanged**. closed without merge
  - maintainer direction: category `implemented_elsewhere`; implication `superseded`; refs `33efff0d8`, `#50726`, `#50740`, `#39550`, `#39548`
- **#41001: Fix #39550: Compression token savings ignored when message count unchanged**. closed without merge
  - maintainer direction: category `implemented_elsewhere`; implication `superseded`; refs `33efff0d8`, `#50726`, `#50740`, `#39550`, `#39548`
- **#46987: fix(agent): detect token reduction in 413 path even when message count is unchanged**. closed without merge
  - maintainer direction: category `implemented_elsewhere`; implication `superseded`; refs `47b6b4cf857ba627070f2ae22cfa4c124c900ca1`, `#50726`
- **#49579: fix #39550: detect token-only compression success when message count unchanged**. closed without merge
  - maintainer direction: category `merged_via`; implication `superseded`; refs `33efff0d8`, `#50726`, `#39574`, `#39673`, `#39900`

## Fix status

- Merged to main in PR #50726 (cited above). No release data is tracked. Verify downstream.
- Fix last verified (merged) `2026-06-22`.
- Subsystem: `agent`
