JS_
Menu
ProjectsApril 6, 2026

snug: Token-Budget Context Management

A tiny, zero-dependency TypeScript library for fitting prioritised content into an LLM token budget, with adapters for OpenAI, Anthropic, and tiktoken.

A caliper measures a packed case as the last block slides into place.
snug solves a problem every LLM app runs into sooner or later: you've got more stuff to put in the context window than will actually fit. System prompts, chat history, documents, tool definitions. snug is one small, well-tested function that decides what makes the cut. Context windows are finite, but the things you want to cram into them aren't. When you go over budget you have to drop something, and doing it naively breaks stuff fast. You lose a tool_use but keep its tool_result, or you bin the system prompt before the low-priority chat history. snug just handles that for you.
  • No dependencies at all, and it runs anywhere: Node, Deno, Bun, edge runtimes
  • You set priorities, and it keeps the important things first
  • Bring your own tokeniser, or use the built-in approximation
  • It keeps paired items together, so a tool_use and its tool_result always travel as one
  • Whatever survives comes back in the original order
Ts
const { included, excluded, tokensUsed } = fit(items, options);
The core stays dependency-free, and a few small adapters do the provider-specific work:
  • snug-tiktoken, wired up with accurate OpenAI and Anthropic token counting
  • snug-openai, which takes OpenAI SDK message arrays directly
  • snug-anthropic, which handles Anthropic messages and keeps tool calls paired up
All TypeScript, all on npm. Have a look on GitHubVisit project source ↗

Project archive