Skip to content

Commit 314c4d9

Browse files
author
cellwebb
committed
refactor(token): simplify token tracking and status display
- Remove redundant token breakdown in session summary output - Streamline status command to show only essential token metrics - Simplify subagent token tracking logic and logging - Add error handling for token tracker status checks
1 parent fc794ff commit 314c4d9

3 files changed

Lines changed: 15 additions & 38 deletions

File tree

src/clippy/agent/token_tracker.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ def from_api_response(
2929
"""Create TokenUsage from API response usage data.
3030
3131
Args:
32-
usage_data: Usage dict from API response with prompt_tokens, completion_tokens, total_tokens
32+
usage_data: Usage dict from API response with prompt_tokens, completion_tokens,
33+
total_tokens
3334
operation: Type of operation (main_agent, subagent, etc.)
3435
operation_id: Identifier for the specific operation
3536
model: Model name used
@@ -155,18 +156,6 @@ def get_summary(self) -> dict[str, Any]:
155156
"completion_tokens": grand_total.completion_tokens,
156157
"total_tokens": grand_total.total_tokens,
157158
},
158-
"main_agent": {
159-
"prompt_tokens": self.main_agent.prompt_tokens,
160-
"completion_tokens": self.main_agent.completion_tokens,
161-
"total_tokens": self.main_agent.total_tokens,
162-
},
163-
"subagents": {
164-
"count": len(self.subagents),
165-
"total_tokens": subagent_total.total_tokens,
166-
"prompt_tokens": subagent_total.prompt_tokens,
167-
"completion_tokens": subagent_total.completion_tokens,
168-
"details": subagent_details,
169-
},
170159
}
171160

172161
def is_enabled(self) -> bool:

src/clippy/cli/commands/system.py

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def handle_help_command(console: Console) -> CommandResult:
5050
" Examples: /model add (wizard) or /model add claude-code claude-4-5 --name sonnet\n"
5151
" /model remove <name> - Remove a saved model\n"
5252
" /model set-default <name> - Set model as default (permanent)\n"
53-
" /model threshold <name> <tokens> - Set compaction threshold\n"
53+
" /model threshold <name> <tokens> - Set compaction threshold\n\n"
5454
"[bold]Subagent Configuration:[/bold]\n"
5555
" /subagent list - Show subagent type configurations\n"
5656
" /subagent set <type> <model> - Set model for a subagent type\n"
@@ -174,19 +174,9 @@ def handle_status_command(agent: ClippyAgent, console: Console) -> CommandResult
174174
note = "[dim]Note: Usage % is estimated for ~128k context window[/dim]"
175175

176176
# Build actual usage summary (from API)
177+
actual_prompt = session_summary["total"]["prompt_tokens"]
178+
actual_completion = session_summary["total"]["completion_tokens"]
177179
actual_tokens = session_summary["total"]["total_tokens"]
178-
actual_main = session_summary["main_agent"]["total_tokens"]
179-
actual_subagents = session_summary["subagents"]["total_tokens"]
180-
actual_subagent_count = session_summary["subagents"]["count"]
181-
182-
# Calculate estimated cost (using approximate GPT-4 rates)
183-
prompt_cost = (
184-
session_summary["total"]["prompt_tokens"] * 0.00003
185-
) # $0.03 per 1K prompt tokens
186-
completion_cost = (
187-
session_summary["total"]["completion_tokens"] * 0.00006
188-
) # $0.06 per 1K completion tokens
189-
estimated_cost = prompt_cost + completion_cost
190180

191181
# Build status content
192182
status_content = (
@@ -203,22 +193,19 @@ def handle_status_command(agent: ClippyAgent, console: Console) -> CommandResult
203193
if actual_tokens > 0:
204194
status_content += (
205195
f"[bold]Actual API Usage:[/bold]\n"
206-
f" Total: [cyan]{actual_tokens:,}[/cyan] tokens\n"
207-
f" Main Agent: [blue]{actual_main:,}[/blue] tokens\n"
208-
f" Subagents: [green]{actual_subagents:,}[/green] tokens "
209-
f"([dim]{actual_subagent_count} subagents[/dim])\n"
196+
f" Input: [cyan]{actual_prompt:,}[/cyan] tokens\n"
197+
f" Output: [cyan]{actual_completion:,}[/cyan] tokens\n"
198+
f" Total: [cyan]{actual_tokens:,}[/cyan] tokens\n\n"
210199
)
211200

212-
if estimated_cost > 0:
213-
status_content += f" 💰 Est. Cost: [yellow]${estimated_cost:.4f}[/yellow]\n"
214-
215-
status_content += "\n"
216-
217201
status_content += f"[bold]Message Breakdown:[/bold]\n {message_breakdown}\n\n{note}"
218202

219203
# Show token tracking status if disabled
220-
if not tracker.is_enabled():
221-
status_content += "\n\n[dim]⚠️ Token tracking is disabled[/dim]"
204+
try:
205+
if not tracker.is_enabled():
206+
status_content += "\n\n[dim]⚠️ Token tracking is disabled[/dim]"
207+
except Exception:
208+
pass
222209

223210
console.print(
224211
Panel.fit(

src/clippy/tools/delegate_to_subagent.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ def create_subagent_and_execute(
185185
result.actual_token_usage, name, result.metadata.get("model", "")
186186
)
187187
logger.info(
188-
f"Tracked {result.actual_token_usage.get('total_tokens', 0)} tokens for subagent '{name}'"
188+
f"Tracked {result.actual_token_usage.get('total_tokens', 0)} tokens "
189+
f"for subagent '{name}'"
189190
)
190191

191192
if result.success:

0 commit comments

Comments
 (0)