Skip to main content

Overview

@deepagents/context is a domain-agnostic context management system for formatting context fragments into different prompt styles. It provides a flexible way to compose and render context data in multiple formats (XML, Markdown, TOML, TOON) suitable for various LLM prompt styles.

Key Features

Multi-format Rendering

Render context fragments as XML, Markdown, TOML, or TOON formats to match different LLM prompt styles

Fragment Builders

Rich set of builder functions for domain knowledge, user context, and messages

Stream Persistence

Durable stream storage with SQLite for resumable operations

Type-Safe

Built with TypeScript for complete type safety and IntelliSense support

Core Concepts

Context Fragments

Fragments are simple data structures that can be transformed into different representations. Each fragment has a name and associated data:
interface ContextFragment {
  name: string;
  data: FragmentData;
  type?: 'fragment' | 'message';
  persist?: boolean;
  codec?: FragmentCodec;
}

Fragment Categories

Fragments are organized into three main categories:
Capture domain-specific knowledge that can be injected into AI prompts:
  • Terms: Define business vocabulary
  • Hints: Behavioral rules and constraints
  • Guardrails: Safety rules and boundaries
  • Examples: Question-answer pairs
  • Workflows: Multi-step processes
Capture user-specific context and preferences:
  • Identity: User name and role
  • Persona: AI assistant identity
  • Aliases: User-specific vocabulary
  • Preferences: Output formatting preferences
  • Corrections: Fixes to previous misunderstandings
Represent conversation messages:
  • User messages: With optional system reminders
  • Assistant messages: AI responses
  • Reminders: Hidden instructions attached to messages

Renderers

Renderers transform fragments into different output formats:
  • XmlRenderer: Hierarchical XML with proper nesting and escaping
  • MarkdownRenderer: Human-readable markdown with bullet points
  • TomlRenderer: TOML-like configuration format
  • ToonRenderer: Token-efficient format with CSV-style tables

Quick Example

import { XmlRenderer, term, hint, guardrail } from '@deepagents/context';

const fragments = [
  term('MRR', 'monthly recurring revenue'),
  hint('Always exclude test accounts'),
  guardrail({
    rule: 'Never expose PII',
    reason: 'Privacy compliance',
    action: 'Aggregate data instead',
  }),
];

const renderer = new XmlRenderer({ groupFragments: true });
console.log(renderer.render(fragments));
Output:
<terms>
  <term>
    <name>MRR</name>
    <definition>monthly recurring revenue</definition>
  </term>
</terms>
<hints>
  <hint>Always exclude test accounts</hint>
</hints>
<guardrails>
  <guardrail>
    <rule>Never expose PII</rule>
    <reason>Privacy compliance</reason>
    <action>Aggregate data instead</action>
  </guardrail>
</guardrails>

Browser Support

For browser bundles, use the browser-specific export path that excludes server-only modules:
import { identity, reminder, term, user } from '@deepagents/context/browser';
The browser entry point excludes server-only modules like store implementations, sandbox tooling, and filesystem-based skill loading.

Next Steps

Installation

Get started by installing the package

Fragment Builders

Learn about creating and composing fragments

Renderers

Explore different output formats

API Reference

Complete API documentation

Build docs developers (and LLMs) love