npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

compact-messages

v0.1.0

Published

Safely trim a long LLM conversation so it fits within a token budget, without ever breaking a tool call away from its result. Framework-agnostic, zero runtime dependencies.

Readme

compact-messages

Safely trim a long LLM conversation so it fits within a token budget, without ever breaking a tool call away from its result.

Framework-agnostic. Zero runtime dependencies. Works in Next.js API routes, plain Node, workers, anywhere.

The problem

A chat conversation keeps growing. The model can only read a limited amount of text at once, and you pay for every token on every request. So once a conversation gets long, you have to remove old messages before sending.

The naive way is to slice the array and keep the last N messages. That breaks, because an assistant message that calls a tool and the message that returns the tool result depend on each other. Send one without the other and the provider API rejects the whole request.

compact-messages does the trimming the safe way: it groups messages into atomic units (a plain message, or a tool call bundled with its result) and only ever keeps or drops whole units.

Install

npm install compact-messages

Use

import { compact } from "compact-messages";

const result = compact(messages, {
  maxTokens: 8000,   // target ceiling for the returned conversation
  keepRecent: 6,     // always keep the last 6 units
  keepSystem: true,  // always keep system messages (default)
});

// result.messages -> the trimmed array, safe to send to the model
// result.stats    -> what happened, e.g. { droppedCount: 12, finalTokens: 7840, ... }

In a Next.js API route

Compaction runs on the server, right before you call the model.

import { compact, needsCompaction } from "compact-messages";

export async function POST(req: Request) {
  const { messages } = await req.json();

  const safeMessages = needsCompaction(messages, { maxTokens: 8000 })
    ? compact(messages, { maxTokens: 8000, keepRecent: 6 }).messages
    : messages;

  // ...now call Claude / OpenAI with safeMessages
}

Options

| Option | Default | What it does | | --- | --- | --- | | maxTokens | (required) | Target token ceiling for the returned conversation. | | keepRecent | 4 | Always keep the last N units. A unit is one message, or a tool call plus its result. | | keepSystem | true | Always keep system messages regardless of budget. | | estimateTokens | chars / 4 | Custom token counter for one message. Swap in a real tokenizer if you need exact counts. |

How it counts tokens

By default it uses a rough characters-divided-by-4 estimate. That is good enough to make trimming decisions and keeps the package dependency-free. If you need billing-accurate counts, pass your own estimateTokens function.

What this does NOT do (yet)

Kept deliberately small. These are intentionally out of scope for v1:

  • Summarizing dropped messages instead of deleting them. (Planned for v2.)
  • Persistence. You own your database; this is a pure transform.
  • Conversation branching or memory. Different problem, different package.

License

MIT. See LICENSE.