跳到正文
DEV Community· Daniel (San) Ávila·· 59 分鐘前AI 評分55

Chess Mod 在 Claude Code 介面加入棋盤,並顯示每步 token 用量

Chess Mod puts a board in your Claude Code side pane and prices every move

AI 導讀

作者發佈 Chess Mod,讓 Claude Code 工作階段持續執行時可開啟棋盤與 Claude 下棋,並顯示每步模型呼叫回報的 token 用量。Claude 的棋步透過 $.model.fork 生成並共享主執行緒的提示詞快取;新工作階段每步約 48k tokens,幾乎全是快取讀取,顯示數字並非帳單金額。

正文

I published the Chess Mod this weekend, and it plays chess against you while your Claude Code session keeps running. It is a game, so it is easy to dismiss, but the way it gets Claude’s moves is the same mechanism any Mod has for asking the model something, and it is the only one that hands back the real token usage. So the board doubles as a meter.

Install it and open the board

One command, and then a session with function hooks on:

npx claude-code-templates@latest --mod games/chess
CLAUDE_CODE_ENABLE_FUNCTION_HOOKS=1 claude

It lands in .claude/skills/chess/, which Claude Code auto-loads as chess@skills-dir in a trusted project. Mods are early access: you need Claude Code 2.1.259 or newer, and the $ API can still move between releases. If /chess never shows up in the typeahead, the module did not load.

/chess opens the board, /chess black starts a game as Black, /chess stop closes the pane. Closing does not end the game, it just hides it. You move by clicking a piece and then a square (the picked square turns yellow, legal destinations go blue, captures go red) or by typing SAN or UCI in the field under the board: e4, Nf3, O-O, e2e4, e8=N. The full rules are enforced, including castling, en passant, promotion, the fifty-move rule, threefold repetition and insufficient material.

Where the pane draws depends on your layout. In the fullscreen renderer (/tui fullscreen, or CLAUDE_CODE_NO_FLICKER=1) the engine docks it beside the transcript, floor to ceiling, the same way /diff docks. On the classic renderer it sits above the prompt. Fullscreen wants a terminal at least 110 columns wide.

How Claude moves, and what the tokens mean

Claude’s move comes from $.model.fork: one completion over your session's own transcript, on your session's model, with tools denied. The prompt carries the position as FEN, the moves so far, and Claude's legal moves in SAN, and asks for one of them. Two things make that interesting. First, the fork shares the main thread's prompt cache. Second, it is the one model call a Mod can make that returns the API's own usage, so the numbers in the pane are reported, not estimated.

That is why a move in a fresh session reads around 48k tokens and nearly all of it is cache r. The fork re-reads your system prompt, your tools and your conversation out of the cache. in is the few hundred uncached tokens of the chess prompt itself, out is the handful Claude replies with, w is what got newly written to the cache. The headline number is the sum of all four, and cache reads bill at a fraction of normal input, so the headline is not a bill. The useful part is watching it grow as your session grows.

  • A move that needed a retry, because Claude named something illegal, makes two calls and both land on that move.
  • Nothing is written to your transcript. The fork’s prompt and reply stay out of the session’s JSONL.
  • Before the session’s first turn there is no transcript to fork, so that move goes through $.model.complete on fallbackModel (haiku by default) and costs a few hundred tokens instead.
  • If a call fails, or Claude still names no legal move after a retry, the pane plays a random legal move and says why, so the game never hangs on “Claude is thinking”.

Jev can play the other side

Put a Jev key in the plugin’s options and Jev plays instead of Claude:

{ "pluginConfigs": { "chess@skills-dir": { "options": { "typesafeApiKey": "<your key>" } } } }

Jev is TypeSafe’s System One decision model. It does not generate text. You hand it state and typed questions, and it returns calibrated answers: noul for a three-valued yes / no / unknown, choice for one option out of a set, score for a number. That constraint is the whole point here, because a chess move is a choice whose options are exactly the legal moves, each with a short description like "knight g1-f3, takes pawn, check". Jev cannot name an illegal move. Claude can, and sometimes does, which is what the retry path is for.

The other difference is where the tokens go. Jev’s move is one HTTP request, not a fork: it reads none of your transcript and spends none of your Claude tokens. typesafeApiKey goes to TypeSafe's own API (POST api.typesafe.ai/v1/systemone, model jev-latest), gatewayApiKey goes to the Vercel AI Gateway (POST ai-gateway.vercel.sh/v4/ai/evaluation-model, model typesafe-ai/jev). With both set, TypeSafe wins, and provider forces one or keeps Claude. The pane shows whatever usage the response reports, and says "not reported" rather than guessing when it reports none. A failed request, or no answer inside timeoutMs (15 seconds), plays a random legal move.

One thing to be explicit about: with a Jev key set, the position and the move list are sent to that backend on every move of Jev’s. Nothing else from your session goes with them.

The options worth changing

Options are declared in the plugin manifest and set in /config, in user settings (~/.claude/settings.json) or in managed settings, under the plugin's full id. Project settings are not read for this. A value marked sensitive, like either API key, goes to the platform's secure credential store rather than into settings.json. With --plugin-dir the id is plain chess.

  • pieces: letters instead of the default unicode, if the glyphs are indistinguishable in your terminal font.
  • board: the pane swaps the Unicode convention on dark themes. A Mod cannot see what an auto theme resolved to, so if your terminal is light under auto, set light here.
  • columns: width asked for the docked pane, 30 to 100, default 40.
  • fallbackModel: the model for that first pre-transcript move, default haiku.
  • timeoutMs: how long to wait for Jev, default 15000.

Is it worth installing? As a game, that is your call on a Saturday. As a way to see what a forked completion actually costs in a session you have been growing all day, it is the cheapest instrumentation I know of, and it never touches your files, your git state or your transcript.

Where the Mods live

You can follow me on my social media:

來源:DEV Community · dev.to