Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3591,6 +3591,27 @@ def _status_bar_context_style(self, percent_used: Optional[int]) -> str:
return "class:status-bar-warn"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current main later removed user-facing cache-hit reporting in fd2a35b because providers that omit cached-token details can produce misleading rates. Please obtain a maintainer decision and a provider-accurate reporting contract before restoring this indicator.

return "class:status-bar-good"

def _cache_hit_rate(self, snapshot: dict, precision: int = 1) -> "tuple[float, str] | None":
"""Return (cache_pct, formatted_label) or None if no cache data.

Centralises the cache-hit-rate computation so both the plain-text
status bar and the prompt-toolkit fragment path share one formula.
"""
cache_read = snapshot.get("session_cache_read_tokens", 0)
prompt_total = snapshot.get("session_prompt_tokens", 0)
if cache_read > 0 and prompt_total > 0:
cache_pct = cache_read / prompt_total * 100
return cache_pct, f"β—Ž {cache_pct:.{precision}f}%"
return None

def _cache_hit_rate_style(self, cache_pct: float) -> str:
"""Style for cache hit rate β€” higher is better (opposite of context %)."""
if cache_pct >= 70:
return "class:status-bar-good"
if cache_pct >= 40:
return "class:status-bar-warn"
return "class:status-bar-bad"
Comment on lines +3607 to +3613

@staticmethod
def _compression_count_style(count: int) -> str:
"""Return a style class reflecting context compression pressure."""
Expand Down Expand Up @@ -3942,6 +3963,9 @@ def _build_status_bar_text(self, width: Optional[int] = None) -> str:
return self._trim_status_bar_text(text, width)
if width < 76:
parts = [f"βš• {snapshot['model_short']}", percent_label]
cache = self._cache_hit_rate(snapshot, precision=0)
if cache:
parts.append(cache[1])
compressions = snapshot.get("compressions", 0)
if compressions:
parts.append(f"πŸ—œοΈ {compressions}")
Expand All @@ -3965,6 +3989,9 @@ def _build_status_bar_text(self, width: Optional[int] = None) -> str:

compressions = snapshot.get("compressions", 0)
parts = [f"βš• {snapshot['model_short']}", context_label, percent_label]
cache = self._cache_hit_rate(snapshot)
if cache:
parts.append(cache[1])
if compressions:
parts.append(f"πŸ—œοΈ {compressions}")
bg_count = snapshot.get("active_background_tasks", 0)
Expand Down Expand Up @@ -4021,6 +4048,10 @@ def _get_status_bar_fragments(self):
("class:status-bar-dim", " Β· "),
(self._status_bar_context_style(percent), percent_label),
]
cache = self._cache_hit_rate(snapshot, precision=0)
if cache:
frags.append(("class:status-bar-dim", " Β· "))
frags.append((self._cache_hit_rate_style(cache[0]), cache[1]))
if compressions:
frags.append(("class:status-bar-dim", " Β· "))
frags.append((self._compression_count_style(compressions), f"πŸ—œοΈ {compressions}"))
Expand Down Expand Up @@ -4060,6 +4091,10 @@ def _get_status_bar_fragments(self):
("class:status-bar-dim", " "),
(bar_style, percent_label),
]
cache = self._cache_hit_rate(snapshot)
if cache:
frags.append(("class:status-bar-dim", " β”‚ "))
frags.append((self._cache_hit_rate_style(cache[0]), cache[1]))
if compressions:
frags.append(("class:status-bar-dim", " β”‚ "))
frags.append((self._compression_count_style(compressions), f"πŸ—œοΈ {compressions}"))
Expand Down
104 changes: 104 additions & 0 deletions tests/cli/test_cli_status_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,3 +676,107 @@ def test_explicit_width_skips_pt_lookup(self):
mock_get_app.assert_not_called()
mock_shutil.assert_not_called()
assert len(text) > 0


class TestCacheHitRate:
Comment on lines +679 to +681
def test_cache_hit_rate_shown_in_wide_terminal(self):
cli_obj = _attach_agent(
_make_cli(),
prompt_tokens=10_000,
completion_tokens=2_000,
total_tokens=12_000,
api_calls=5,
context_tokens=12_000,
context_length=200_000,
cache_read_tokens=7600,
cache_write_tokens=0,
)

text = cli_obj._build_status_bar_text(width=120)

assert "β—Ž 76.0%" in text

def test_cache_hit_rate_shown_in_narrow_terminal(self):
cli_obj = _attach_agent(
_make_cli(),
prompt_tokens=10_000,
completion_tokens=2_000,
total_tokens=12_000,
api_calls=5,
context_tokens=12_000,
context_length=200_000,
cache_read_tokens=5000,
cache_write_tokens=0,
)

text = cli_obj._build_status_bar_text(width=60)

assert "β—Ž 50%" in text

def test_cache_hit_rate_hidden_when_zero(self):
cli_obj = _attach_agent(
_make_cli(),
prompt_tokens=10_000,
completion_tokens=2_000,
total_tokens=12_000,
api_calls=5,
context_tokens=12_000,
context_length=200_000,
cache_read_tokens=0,
cache_write_tokens=0,
)

text = cli_obj._build_status_bar_text(width=120)

assert "β—Ž" not in text

def test_cache_hit_rate_hidden_when_no_data(self):
cli_obj = _attach_agent(
_make_cli(),
prompt_tokens=10_000,
completion_tokens=2_000,
total_tokens=12_000,
api_calls=5,
context_tokens=12_000,
context_length=200_000,
)

text = cli_obj._build_status_bar_text(width=120)

assert "β—Ž" not in text

def test_cache_hit_rate_one_decimal(self):
cli_obj = _attach_agent(
_make_cli(),
prompt_tokens=10_000,
completion_tokens=2_000,
total_tokens=12_000,
api_calls=5,
context_tokens=12_000,
context_length=200_000,
cache_read_tokens=7620,
cache_write_tokens=0,
)

text = cli_obj._build_status_bar_text(width=120)

assert "β—Ž 76.2%" in text

def test_cache_hit_rate_with_anthropic_style_cache(self):
"""Anthropic has both cache_read and cache_write"""
cli_obj = _attach_agent(
_make_cli(),
prompt_tokens=10_000,
completion_tokens=2_000,
total_tokens=12_000,
api_calls=5,
context_tokens=12_000,
context_length=200_000,
cache_read_tokens=5000,
cache_write_tokens=2000,
)

text = cli_obj._build_status_bar_text(width=120)

# cache_read / prompt_tokens = 5000 / 10000 = 50%
assert "β—Ž 50.0%" in text