Created
January 26, 2026 09:57
-
-
Save rivol/1dda0b3c24455547626c5445b26f5fb3 to your computer and use it in GitHub Desktop.
Claude Code statusline-command.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import json | |
| import sys | |
| def format_tokens(tokens: int) -> str: | |
| """Format token counts (e.g., 106500 -> 107k).""" | |
| if tokens >= 1000: | |
| return f"{round(tokens / 1000)}k" | |
| return str(tokens) | |
| def format_duration(ms: int) -> str: | |
| """Format milliseconds to human-readable duration (e.g., 2m35s, 5h07m01s).""" | |
| seconds = ms // 1000 | |
| hours, remainder = divmod(seconds, 3600) | |
| minutes, secs = divmod(remainder, 60) | |
| if hours > 0: | |
| return f"{hours}h{minutes:02d}m{secs:02d}s" | |
| elif minutes > 0: | |
| return f"{minutes}m{secs:02d}s" | |
| else: | |
| return f"{secs}s" | |
| def main(): | |
| # Read and parse JSON input from stdin | |
| data = json.load(sys.stdin) | |
| # Extract values | |
| model = data.get("model", {}).get("display_name", "Unknown") | |
| context = data.get("context_window", {}) | |
| context_size = context.get("context_window_size", 0) | |
| total_input = context.get("total_input_tokens", 0) | |
| total_output = context.get("total_output_tokens", 0) | |
| # Calculate context usage from current_usage fields | |
| usage = context.get("current_usage", {}) | |
| if usage and context_size > 0: | |
| context_used = ( | |
| usage.get("input_tokens", 0) | |
| + usage.get("cache_creation_input_tokens", 0) | |
| + usage.get("cache_read_input_tokens", 0) | |
| ) | |
| used_pct = context_used * 100 / context_size | |
| context_info = f"π {used_pct:.1f}% ({format_tokens(context_used)}/{format_tokens(context_size)})" | |
| else: | |
| context_info = "π N/A" | |
| # Build session info | |
| session_info = f"π¨ β{format_tokens(total_input)} β{format_tokens(total_output)}" | |
| # Extract cost and duration info | |
| cost_data = data.get("cost", {}) | |
| total_cost = cost_data.get("total_cost_usd", 0) | |
| total_duration_ms = cost_data.get("total_duration_ms", 0) | |
| api_duration_ms = cost_data.get("total_api_duration_ms", 0) | |
| duration_info = f"β±οΈ {format_duration(api_duration_ms)}/{format_duration(total_duration_ms)}" | |
| cost_info = f"πΈ ${total_cost:.2f}" | |
| # Output status line | |
| print(f"π§ {model} | {context_info} | {session_info} | {duration_info} | {cost_info}", end="") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment