Claude Code: Token-Nutzung pro Session mit dem session-cost-Skill

Dmitriy Solodukha, Caprock-Entwickler · Veröffentlicht am

Am mit Claude Code und Codex getestet

Sieh die Token-Nutzung pro Claude-Code-Session und ihre Ursachen. Der session-cost-Skill liest Caprock, /usage oder lokale Logs und erfindet keine Preise.

Um die Token-Nutzung dieser und der letzten Claude-Code-Sessions sowie die Gründe für hohe Werte zu sehen, frage den Agenten mit installiertem session-cost-Skill. Er liest die Zahlen aus einer Quelle, die sie enthält (Caprocks cost-Tool, der eigene Nutzungsbefehl des Agenten oder lokale Session-Logs über ein kleines Skript), benennt die ein bis zwei größten Ursachen und nennt Dollarbeträge nur mit Quellenangabe.

Installiere den session-cost-Skill

Ein Befehl pro Agent. Der Skill besteht aus einer einzigen SKILL.md und dem Skript scripts/session_tokens.py, das die Befehle mitinstallieren; bei der Installation wird nichts ausgeführt.

Alle neun Skills als Plugin. Gib dies in Claude Code ein:

Oder nur diesen Skill, im Projektordner in einem Terminal:

Füge -g hinzu, um ihn für jedes Projekt zu installieren.

Prompts für den session-cost-Skill

Der Agent wählt den Skill, wenn deine Worte zu dessen Beschreibung passen; mit dem Namen stellst du es sicher. Klicke auf einen Prompt, um ihn zu kopieren.

Was der session-cost-Skill für die Token-Nutzung von Claude Code liest

  • Caprocks cost-Tool, falls verbunden: Tokens und Dollar pro Tag und Modell, für ein Repository oder alle. Die Dollarbeträge entsprechen API-Listenpreisen, bei einem Pauschalplan also den Kosten derselben Nutzung über die API und nicht dem berechneten Betrag; der Skill sagt das dazu.
  • Der eigene Befehl des Agenten, den du eingibst: /usage in Claude Code (/cost und /stats sind Aliasse, /context zeigt, was das Fenster füllt), /status und /usage in Codex.
  • Die lokalen Logs, über scripts/session_tokens.py (Python-Standardbibliothek, offline): Claude-Code-Transkripte unter ~/.claude/projects/, einschließlich Subagenten, und Codex-Rollouts unter ~/.codex/sessions/, für den aktuellen Ordner. Nur Tokens; keine Preisberechnung.

Dann benennt der Skill die Ursachen der Zahl: Cache-Lesen bei großem Kontext, der bei jedem Aufruf gelesen wird, viele Aufrufe (oft ein Polling-Loop), Subagenten mit jeweils eigenem Kontext, lange Ausgaben oder ein teureres Modell für Routinearbeit, jeweils mit einem konkreten nächsten Schritt.

Was wir ausgeführt haben und was die Agenten taten

Jeder Skill wurde am 29. November 2026 in einem eigens dafür angelegten Wegwerf-Repository ausgeführt, als Projekt-Skill installiert. Ein Wegwerf-Repository mit calc.py, nach einer kurzen Claude-Code-Session, die eine Funktion hinzugefügt hatte, damit echte Nutzung zum Auslesen vorlag. Der Codex-Durchlauf kam später, nach einer kurzen Codex-Session im selben Ordner und dem Claude-Code-Kostendurchlauf, sodass er drei Sessions zählte.

Claude Code (claude -p --model sonnet)

How much have my Claude Code sessions in this project cost so far? Show the token usage and what drove it.

Skill geladen, Caprocks cost und recent_sessions aufgerufen, dann das Log-Skript ausgeführt. Er nannte den Dollarbetrag aus Caprock (etwa $0.17 bis $0.18 für die zwei Sessions des Projekts, zu API-Listenpreisen, mit Caprocks Preisversion), erklärte, dass dies bei einem Pauschalplan nicht der berechnete Betrag ist, und benannte Cache-Lesen (82% der Tokens) als Ursache. Er wies auch darauf hin, dass die beiden Session-Beträge zusammen höher als die Projektsumme waren, und sagte, er habe den Grund nicht gefunden.

Codex (0.163.0, codex exec)

How much have my coding-agent sessions in this project cost so far? Show the token usage and what drove it.

Skill gelesen, Caprocks cost und das Log-Skript genutzt: $0.23 und 338.504 Tokens, davon 85% Cache-Lesen, mit demselben Hinweis zum Pauschalplan.

Agenten, mit denen er getestet wurde

  • Claude Code: am 29. November 2026 mit claude -p --model sonnet getestet, der Skill als Projekt-Skill in .claude/skills/ installiert.
  • Codex: am 29. November 2026 mit Codex 0.163.0 (codex exec) getestet, der Skill in .agents/skills/.
  • OpenCode und Cursor: nicht getestet. Der Skill besteht aus reinen Anweisungen im offenen Agent-Skills-Format, wurde aber mit keinem der beiden erprobt.

Grenzen

  • Der Weg ohne Caprock (Nutzungsbefehl, dann nur das Skript) wurde in den Tests nicht live ausgeführt.
  • Nutzungsbefehle für OpenCode und Cursor fehlen: Keiner wurde verifiziert.
  • Das Skript zählt Tokens; es rechnet sie nie in Dollar um. Dollarbeträge erfordern Caprock, /usage oder von dir angegebene Preise.
  • Ein Agent kann selbst keinen Slash-Befehl eingeben: Bei /usage oder /status sagt er dir, welchen du eingeben sollst.

Mit und ohne Caprock

Wenn der Caprock-MCP-Server verbunden ist, ruft der Skill cost (Tokens und Dollar pro Tag und Modell zu API-Listenpreisen) und recent_sessions auf, um die Sessions hinter einem Tag zu benennen. Ohne ihn greift er auf den eigenen Befehl des Agenten und das Log-Skript zurück. Caprock ist die kostenlose Open-Source-App, die jede Coding-Agent-Session auf deinem Computer erfasst.

Caprock holen →

Die vollständige SKILL.md

Die Datei aus dspv/skills in Revision dfce799. Zur manuellen Installation speicherst du sie in deinem Projekt als .claude/skills/session-cost/SKILL.md für Claude Code oder .agents/skills/session-cost/SKILL.md für Codex.

skills/session-cost/SKILL.md
---
name: session-cost
description: >-
  Tells you what the current coding-agent session, and recent ones, cost in tokens and
  (when a real price source exists) dollars, and what drove it: cache reads from a large
  context, many tool calls, subagents. Use when you ask "how much did this session cost",
  "claude code token usage", "how many tokens have I used", "why is this session so
  expensive" or "cost per session". Reads Caprock's cost tool when present, otherwise the
  agent's own usage command and local session logs; never invents a price.
license: Apache-2.0
---

# Session cost

Answer "what did this cost and why" from a source that holds the numbers. Never estimate a
figure from memory, never invent a price per token.

## Pick the source, best first

1. **Caprock `cost` tool** (MCP server `caprock`). If it is in your tool list, call it:
   `cost` with `days` (1 for today, default 7, at most 90), optional `repo` (a directory in
   the repository) or `all_repos: true`. It returns tokens and `cost_usd` per day and per
   model. Its own note says it prices at API list rates from Caprock's model table, so on a
   flat plan the dollars are what the same usage would cost on the API, not what was
   billed. Say that. If `recent_sessions` is also there, use it to name the sessions behind a
   day. (Caprock, https://caprock.dev, is free, open source and runs locally; it is what
   provides these tools.)
2. **The agent's own command.** You cannot run a slash command yourself; tell the user which
   one to type:
   - Claude Code: `/usage` ("Show session cost, plan usage limits, and activity stats";
     `/cost` and `/stats` are aliases). `/context` shows what fills the context window.
     Source: https://code.claude.com/docs/en/commands, read 2026-11-29. On Pro, Max and other
     plans the docs say the session cost figure "isn't relevant for billing purposes"
     (https://code.claude.com/docs/en/costs).
   - Codex: `/status` ("Display session configuration and token usage") and `/usage` ("View
     account token usage"). Source: https://learn.chatgpt.com/docs/developer-commands, read
     2026-11-29.
   - Other agents: only a command you found in their docs or `--help`. If none, skip.
3. **Local session logs**, read with the script beside this file (offline, standard library,
   prices nothing):

   ```bash
   python3 <this skill's folder>/scripts/session_tokens.py              # 5 newest, this dir
   python3 <this skill's folder>/scripts/session_tokens.py --session ID # one, with drivers
   python3 <this skill's folder>/scripts/session_tokens.py --json --last 10
   ```

   It reads Claude Code transcripts (`~/.claude/projects/<cwd as slug>/*.jsonl`, subagents
   under `<session>/subagents/`) and Codex rollouts (`~/.codex/sessions/**/rollout-*.jsonl`)
   whose working directory is `--cwd` (default: the current one). If the logs are missing
   or unreadable, say so; do not guess. The current session is usually the newest row; in
   Claude Code its id is also the transcript file name.

Use the first source that works. Use a second one only to cross-check or to explain.

## Dollars: only from a named source

- Caprock `cost` and Claude Code `/usage` give dollars: quote them with their source.
- From the log script you get tokens only. Turn them into dollars only with rates the user
  gives you or a provider pricing page already read in this session; then name the page or
  the user's rates and the date, and price each kind separately (input, cache write, cache
  read, output differ). Otherwise answer in tokens and say why there is no dollar figure.
- Do not fetch a pricing page unless the user asks you to go online.

## What drove it

Read the numbers, then name the one or two biggest causes, with the figure behind each:

- **Cache reads dominate** (usual for long Claude Code sessions): every call re-reads the
  whole context, so cost grows with context size times number of calls. Signs: cache read
  far above everything else, large calls near the context limit. Remedy: compact or start a
  fresh session with a handoff note.
- **Many calls**: a high `api calls` count with a long tool-call list (often a shell loop or
  polling). Remedy: batch commands, stop polling.
- **Subagents**: the subagent line of the script, or a model split in Caprock `cost`. Each
  subagent starts its own context. Remedy: fewer, narrower subagents.
- **Output** heavy: long generated files or reasoning. Usually the smallest share.
- **Model mix**: a costlier model on routine work (`by_model` in Caprock, `models` in the
  script).

Note the counting difference: in Codex logs `input_tokens` include cached tokens (the script
splits them out); in Claude Code logs they do not.

## Output

```
Session <id or "current"> (<agent>, <model>), <start> to <end>
Tokens: <total>  (input <n>, cache write <n>, cache read <n>, output <n>; subagents <n>)
Cost: $<x> from <source, date>   |   Cost: tokens only (<why no price>)
Recent: <one line per session or day: date, tokens, $ if sourced>
Drivers: 1. <cause, with its number>  2. <cause>
Next: <one concrete step, or "nothing to change">
```

Stop when every figure has a source. If no source works, say which ones you tried and what
the user can run (`/usage`, `/status`) to see it.

Fragen zum session-cost-Skill

Wie prüfe ich die Token-Nutzung einer einzelnen Claude-Code-Session?
Gib `/usage` in Claude Code ein (`/cost` ist ein Alias), oder frage einen Agenten mit dem session-cost-Skill. Er liest Caprock oder die lokalen Transkripte und schlüsselt die Summe nach Input, Cache-Schreiben, Cache-Lesen und Output auf.
Warum ist meine Token-Nutzung in Claude Code so hoch?
In langen Sessions liegt es meist am Cache-Lesen: Jeder Aufruf liest den gesamten Kontext erneut, die Kosten steigen also mit Kontextgröße mal Anzahl der Aufrufe. Der Skill zeigt den Anteil und empfiehlt eine Komprimierung oder eine neue Session mit Übergabe.
Zeigt der session-cost-Skill, was mir berechnet wurde?
Nicht bei einem Pauschalplan. Caprock und die Session-Zahl in Claude Code bewerten die Nutzung zu API-Listenpreisen; bei Pro oder Max entspricht das den Kosten derselben Nutzung über die API. Der Skill sagt, welches von beiden es ist.
Sendet er meine Logs irgendwohin?
Nein. Das Skript liest lokale Dateien mit der Python-Standardbibliothek und macht keine Netzwerkaufrufe. Der Skill ruft keine Preisseite ab, es sei denn, du bittest darum.

Quellen

Die App selbst ist auf Englisch.