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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@braingrid/prompts

v1.0.0

Published

Prompt management with context interpolation, token calculation, and metrics

Downloads

16

Readme

@braingrid/prompts

Prompt management with context interpolation, token calculation, and metrics.

Features

  • Pluggable Storage - Memory, filesystem, or custom backends
  • Token Calculation - Accurate token counting with tiktoken
  • Context Interpolation - {{variable}} template syntax
  • Metrics - Track tokens, characters, words, lines
  • Caching - Performance optimization for repeated access

Installation

pnpm add @braingrid/prompts

Quick Start

import { PromptService, MemoryPromptStorage } from '@braingrid/prompts';

// 1. Create storage
const storage = new MemoryPromptStorage({
  chat_system: 'You are a helpful assistant for {{userName}}.',
  research_system: 'You are a research assistant for {{userName}} at {{companyName}}.',
});

// 2. Create service
const prompts = new PromptService({ storage });

// 3. Get prompt
const prompt = await prompts.getPrompt('chat_system');

// 4. Get prompt with context
const interpolated = await prompts.getPromptWithContext('chat_system', {
  userName: 'Alice',
});
// => "You are a helpful assistant for Alice."

// 5. Get metrics
const metrics = await prompts.getPromptWithMetrics('chat_system');
console.log(metrics);
// => { name: 'chat_system', content: '...', tokens: 12, characters: 45, words: 8, lines: 1 }

Storage Backends

MemoryPromptStorage

import { MemoryPromptStorage } from '@braingrid/prompts';

const storage = new MemoryPromptStorage({
  chat_system: 'You are a chat assistant.',
  research_system: 'You are a research assistant.',
});

storage.set('new_prompt', 'You are...');
storage.has('chat_system'); // true
storage.list(); // ['chat_system', 'research_system', 'new_prompt']

FileSystemPromptStorage

import { FileSystemPromptStorage } from '@braingrid/prompts';

const storage = new FileSystemPromptStorage('./prompts');

// Reads from:
// ./prompts/chat_system.txt
// ./prompts/chat_system.md
// ./prompts/research_system.txt
// etc.

Custom Storage

import { type PromptStorage } from '@braingrid/prompts';

class DatabasePromptStorage implements PromptStorage {
  async get(name: string): Promise<string | undefined> {
    const row = await db.query('SELECT content FROM prompts WHERE name = $1', [name]);
    return row?.content;
  }

  async list(): Promise<string[]> {
    const rows = await db.query('SELECT name FROM prompts');
    return rows.map(r => r.name);
  }
}

const storage = new DatabasePromptStorage(db);
const service = new PromptService({ storage });

Token Calculation

import { PromptService, TiktokenCalculator, ApproximateTokenCalculator } from '@braingrid/prompts';

// Accurate token counting (requires tiktoken)
const service = new PromptService({
  storage,
  tokenCalculator: new TiktokenCalculator('gpt-4'),
});

// Fast approximation (no dependencies)
const service2 = new PromptService({
  storage,
  tokenCalculator: new ApproximateTokenCalculator(),
});

Context Interpolation

const storage = new MemoryPromptStorage({
  greeting: 'Hello {{userName}}, welcome to {{companyName}}!',
  nested: 'User {{user.firstName}} {{user.lastName}} from {{user.company.name}}',
});

const service = new PromptService({ storage });

const prompt1 = await service.getPromptWithContext('greeting', {
  userName: 'Alice',
  companyName: 'ACME Corp',
});
// => "Hello Alice, welcome to ACME Corp!"

const prompt2 = await service.getPromptWithContext('nested', {
  user: {
    firstName: 'Bob',
    lastName: 'Smith',
    company: { name: 'TechCo' },
  },
});
// => "User Bob Smith from TechCo"

Integration with Agents

import { PromptService, FileSystemPromptStorage } from '@braingrid/prompts';
import { PromptManager } from '@braingrid/agents';

const storage = new FileSystemPromptStorage('./prompts');
const promptService = new PromptService({ storage });

// Use with PromptManager
const manager = new PromptManager({
  agentType: 'chat',
  storage,
});

const prompt = await manager.getPrompt({ userName: 'Alice' });

License

MIT