This article was originally published in Japanese on Qiita.
Introduction
When I work on Kaggle, I write code in Jupyter notebooks and ask Claude Code or Codex CLI for reviews and advice along the way. The problem: passing a whole notebook to an LLM came to about 30k tokens per notebook, and after asking a few times, the cost added up fast.
In this article, I'll share the tricks I used to save tokens, plus the gotchas I hit when calling these CLIs from Windows PowerShell.
1. Pass only the "code cell diff" of the notebook
An .ipynb file is JSON under the hood, and it's packed with cell outputs (logs and images) and metadata. In most cases, the code is all I actually want the LLM to look at, so I wrote a script that does the following:
- Drops outputs and metadata, and extracts only the code cells
- Outputs only the diff against the previous version
- Replaces long lines over 300 characters with their "length" and a "hash" (all I need to know is whether the content changed)
With this, comparing versions fits in about 1–1.6k tokens (a reduction of over 95%).
Also, long prompt strings inside a notebook make it hard to read, so I create a "reading view" where those strings are split out into separate .txt files. The notebook I actually run stays as a single file, and I pass the reading view to the LLM or for reviews. That's how I keep the two roles separate.
2. Calling Claude Code / Codex CLI as commands from another program
Claude Code
With claude -p, you can ask a single question and get an answer back without opening the interactive UI.
claude -p --max-turns 1 --disallowedTools "Bash,Read,..."
The key is to disallow tools like file reading and pass only the summary you've prepared yourself. Since it won't go off and read files in your repository on its own, token usage stays cheap.
Codex CLI
I installed it with npm.
npm i -g @openai/codex
When logging in through the browser failed, authenticating with a URL and a code worked.
codex login --device-auth
I pass the question via standard input.
echo "Your question here" | codex exec --skip-git-repo-check -
3. Japanese text gets garbled in Windows PowerShell
When I piped a Japanese prompt from PowerShell into the CLI, the text came out garbled (mojibake). Setting the following three to UTF-8 fixes it.
[Console]::InputEncoding = [System.Text.Encoding]::UTF8
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$OutputEncoding = [System.Text.Encoding]::UTF8
Also, PowerShell doesn't let you chain commands with & the way cmd does; you get an AmpersandNotAllowed error. If you write commands with a cmd mindset, this will trip you up, so watch out.
Summary
- Instead of passing the whole notebook, passing only the code cell diff cut tokens by over 95%
- With
claude -p, disallowing tools and passing only a summary keeps it cheap - If Codex CLI login fails, try
codex login --device-auth - When passing Japanese text in PowerShell, set the three encodings to UTF-8
I hope this helps anyone else who's going back and forth between LLMs and notebooks like I am.
Top comments (0)