惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
Martin Fowler
Martin Fowler
云风的 BLOG
云风的 BLOG
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
The Blog of Author Tim Ferriss
大猫的无限游戏
大猫的无限游戏
A
About on SuperTechFans
小众软件
小众软件
博客园_首页
博客园 - 聂微东
罗磊的独立博客
Recent Announcements
Recent Announcements
U
Unit 42
N
Netflix TechBlog - Medium
Blog — PlanetScale
Blog — PlanetScale
阮一峰的网络日志
阮一峰的网络日志
博客园 - 叶小钗
V
V2EX
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
Stack Overflow Blog
Stack Overflow Blog
博客园 - Franky
D
DataBreaches.Net
Last Week in AI
Last Week in AI

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
I built ctxstash: pack a codebase into one LLM-ready file...
benjamin · 2026-06-14 · via DEV Community
Cover image for I built ctxstash: pack a codebase into one LLM-ready file, with a token count

benjamin

You're debugging something with ChatGPT, Claude, or Cursor, and you hit the wall every developer knows: the model needs to see your code. So you start the dance — open a file, copy, paste, type "and here's the other file", paste again, label it so the model doesn't get confused… five files later you paste the whole thing and get back "this conversation is too long." Now you're trimming blind, with no idea how many tokens you actually sent.

I got tired of doing this by hand, so I built ctxstash: one command that walks a directory and emits a single, tidy Markdown document with every file fenced and language-tagged, a file-tree overview at the top, and an approximate token count so you know up front whether it'll fit.

npx ctxstash src > context.md

✓ packed 23 files · 142.3 KB · ~38,210 tokens

Paste context.md into the model. Done.

Why not just paste, or use an online tool?

  • Pasting by hand is slow, and you lose the structure — the model can't tell where one file ends and the next begins.
  • Online "paste your code" formatters mean shipping your source to someone else's server. Hard no for anything private.
  • You can't see the token cost until the model rejects it, and by then you're guessing what to cut.

There's a great Python tool, files-to-prompt, that inspired this — but it's Python-only and doesn't estimate tokens. I wanted something with zero dependencies, a token estimate built in, and an identical build on both npm and PyPI so it doesn't matter which ecosystem you live in.

What it does

ctxstash .                      # pack the current dir to stdout
ctxstash src tests -o ctx.md    # pack two dirs, write to a file
ctxstash . -i "*.ts,*.tsx"      # only TypeScript
ctxstash . -e "*.test.js"       # drop tests
ctxstash . --estimate           # just tell me the token cost, pack nothing
ctxstash src --tree             # just the file tree

--estimate is the one I reach for most — it answers "will this fit in the context window?" without producing a wall of text:

files    23
size     142.3 KB
~tokens  38,210 (estimate, ~4 chars/token)

largest by tokens
     ~6,210  src/bundle.ts
     ~3,180  src/core.ts
     ...

The packed output looks like this:

# Repository context

> Packed by ctxstash — 3 files, 4.1 KB, ~1,040 tokens (estimate).

## File tree

src/
  core.ts
  cli.ts
README.md

## Files

### src/core.ts
...fenced, language-tagged contents...

By default it skips the junk automaticallynode_modules, .git, dist, lockfiles, minified bundles, and binary files (images, fonts, compiled artifacts) never end up in your context. The summary always goes to stderr, so ctxstash > context.md keeps the file clean while you still see the count.

Install

npx ctxstash .          # Node ≥ 18, nothing to install
pip install ctxstash    # Python ≥ 3.8

Both builds are zero-dependency and behavior-identical.

A few design choices, for the curious

  • Two builds, one behavior. The Node and Python versions are kept byte-for-byte identical on the same input — same sort order, same fences, same output. There's a test that packs a directory with both and diffs the result.
  • Token counts are honest estimates. It uses ~4 characters per token (OpenAI's rule of thumb), tokenizer-agnostic. It's great for "will this fit?", not for exact billing — and the output says so rather than pretending to be precise.
  • Collision-safe fences. If a file already contains a ``` run (hello, every Markdown file), ctxstash wraps it in a longer fence so your context doesn't break mid-document.
  • Binary sniffing, not just extensions. It checks for NUL bytes and control-byte density, so a stray binary with a .txt name still gets skipped.
  • Zero dependencies on purpose. No supply chain, no install dance — npx/pip install and go.

Try it

npx ctxstash . --estimate

It's MIT-licensed and open source:

How are you feeding code to LLMs right now — manual copy-paste, a custom script, or something else? And what would make a tool like this actually fit your workflow? I'd genuinely like to know what to build next.