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

@sigmalion/ai-context-shrink

v1.0.5

Published

Compress objects and text to minimize LLM tokens while preserving clarity

Readme

ai-context-shrink

Compress objects and text to minimize LLM tokens while keeping it readable.

npm version Zero dependencies TypeScript

The Problem

You're passing large objects or long texts into an LLM prompt, and you're burning through tokens on repetitive array items, huge string fields, or data you don't need.

ai-context-shrink trims the fat in a single pass — no external dependencies, no surprises.


Install

npm install @sigmalion/ai-context-shrink

Quick Start

import { shrink, schema, shrinkToString } from '@sigmalion/ai-context-shrink';

const data = {
  userId: 42,
  name: "Roman",
  bio: "A".repeat(300),             // very long string
  tags: [1, 2, 3, 4, 5, 6, 7, 8],  // large array
  address: {
    city: "Kharkiv",
    zip: "61000"
  }
};

// Standard compression
const compressed = shrink(data);
/*
{
  userId: 42,
  name: 'Roman',
  bio: 'AAAA...AAAA...',            // truncated to 100 chars
  tags: [ 1, 2, 3, '[+ 2 items]', 6, 7, 8 ],
  address: { city: 'Kharkiv', zip: '61000' }
}
*/

// Schema only (structure without data)
const structure = schema(data);
/*
{
  userId: 'number',
  name: 'string',
  bio: 'string',
  tags: [ 'number', 'number', 'number', '[+ 2 items]', 'number', 'number', 'number' ],
  address: { city: 'string', zip: 'string' }
}
*/

// Directly to string for prompt insertion
const prompt = `Context:\n${shrinkToString(data, {}, 2)}`;

API

shrink(data, options?)

Primary function. Accepts any value and returns a compressed copy.

shrink(data: unknown, options?: ShrinkOptions): unknown

schema(data, options?)

Returns the data schema (types instead of values). Equivalent to shrink(data, { schemaMode: true }).

schema(data: unknown, options?: Omit<ShrinkOptions, 'schemaMode'>): unknown

shrinkToString(data, options?, space?)

Compresses and serializes to a JSON string for direct prompt insertion.

shrinkToString(data: unknown, options?: ShrinkOptions, space?: string | number): string

Options

| Parameter | Type | Defaults | Description | |-----------------------|-----------|----------------|--------------------------------------------------------| | maxArrayItems | number | 3 | Number of elements kept at the start and end of arrays | | maxStringLength | number | 100 | Maximum string length (characters) | | schemaMode | boolean | false | Returns a schema of types instead of values | | maxDepth | number | 10 | Maximum recursion depth | | circularPlaceholder | string | "[Circular]" | Placeholder for circular references |


Features

  • ⚡ Single Pass — O(n) traversal of the data tree
  • ✂️ Smart Array Truncation[1, 2, 3, "[+ 5 items]", 9, 10]
  • 📏 Value Trimming — Long strings are truncated with a "..." suffix
  • 🗺️ Schema Mode — Data structure without the values
  • 🔄 Circular Safe — Circular reference protection via WeakSet
  • 0️⃣ Zero Dependencies — Pure TypeScript, no dependencies
  • 📦 ESM + CJS — Works everywhere: Node.js, bundlers, edge runtimes

Circular References

The package safely handles objects with circular references:

const obj: any = { name: 'loop' };
obj.self = obj;

shrink(obj);
// { name: 'loop', self: '[Circular]' }

License

MIT