Resources

Real-World Examples

Production-ready patterns and code examples for common use cases

What You'll Learn

  • E-commerce product catalog encoding with schemas
  • User analytics dashboard with stream mode
  • Enterprise XML invoice feed conversion

E-commerce Product Catalog

Encode a product catalog with schema validation, then see the compact AXON output:

import { encode, type Schema } from '@axon-format/core';

const productSchema: Schema = {
  name: 'Product',
  fields: [
    { name: 'id', type: 'u8' },
    { name: 'sku', type: 'str' },
    { name: 'name', type: 'str' },
    { name: 'price', type: 'f64' },
    { name: 'inStock', type: 'bool' }
  ]
};

const data = {
  products: [
    { id: 1, sku: "WDG-001", name: "Widget Pro", price: 19.99, inStock: true },
    { id: 2, sku: "GDG-002", name: "Gadget X", price: 29.99, inStock: true },
    { id: 3, sku: "SPR-003", name: "Sprocket", price: 9.50, inStock: false }
  ]
};

const encoded = encode(data, { schemas: [productSchema] });

AXON output:

products::[3] id:u8|inStock:bool|name:str|price:f64|sku:str
1|true|Widget Pro|19.99|WDG-001
2|true|Gadget X|29.99|GDG-002
3|false|Sprocket|9.5|SPR-003

Token savings: The JSON version of this data uses ~120 tokens. The AXON version uses ~45 tokens -- a 62% reduction. Savings grow as the number of rows increases.

User Analytics Dashboard

Time-series event data is a natural fit for AXON. The encoder automatically applies Stream mode with delta encoding for timestamps:

import { encode, type Schema } from '@axon-format/core';

const analyticsSchema: Schema = {
  name: 'Event',
  fields: [
    { name: 'timestamp', type: 'iso8601' },
    { name: 'userId', type: 'u16' },
    { name: 'action', type: 'str' }
  ]
};

const analyticsData = {
  events: [
    { timestamp: "2025-01-15T10:00:00Z", userId: 123, action: "login" },
    { timestamp: "2025-01-15T10:05:00Z", userId: 123, action: "view_product" },
    { timestamp: "2025-01-15T10:07:30Z", userId: 123, action: "add_to_cart" },
    { timestamp: "2025-01-15T10:12:00Z", userId: 201, action: "login" },
    { timestamp: "2025-01-15T10:15:00Z", userId: 201, action: "search" }
  ]
};

const encoded = encode(analyticsData, { schemas: [analyticsSchema] });

AXON output with automatic delta encoding on timestamps:

events::[5] action:str|timestamp:iso8601|userId:u8
login|2025-01-15T10:00:00Z|123
view_product|+5m|123
add_to_cart|+2m30s|123
login|+4m30s|201
search|+3m|201

Chat / RAG Integration

The most common AXON use case: encoding retrieval results before sending them as context to an LLM. This reduces the tokens consumed by context data, leaving more of the context window for the actual conversation:

import { encode, type Schema } from '@axon-format/core';

const docSchema: Schema = {
  name: 'Document',
  fields: [
    { name: 'title', type: 'str' },
    { name: 'content', type: 'str' },
    { name: 'score', type: 'f64' },
    { name: 'source', type: 'str' }
  ]
};

// Retrieval results from your vector DB
const retrievalResults = {
  documents: [
    { title: "Refund Policy", content: "Returns accepted within 30 days...", score: 0.94, source: "policies/refunds.md" },
    { title: "Shipping FAQ", content: "Standard shipping takes 3-5 days...", score: 0.87, source: "faq/shipping.md" },
    { title: "Contact Us", content: "Reach our support team at...", score: 0.72, source: "support/contact.md" }
  ]
};

const axonContext = encode(retrievalResults, { schemas: [docSchema] });

// Build your LLM prompt
const messages = [
  {
    role: "system",
    content: "You are a support agent. Use the following context (in AXON format) to answer questions:\n" + axonContext
  },
  {
    role: "user",
    content: "How long do I have to return an item?"
  }
];

XML Invoice Feed

Convert enterprise XML data feeds to token-efficient AXON:

import { encodeXml } from '@axon-format/core';

// Enterprise invoice XML from ERP system
const invoiceXml = `
<invoices xmlns="http://corp.com/billing">
  <invoice id="INV-2024-0001" customer="CUST-500">
    <date>2024-01-15</date>
    <status>paid</status>
    <lineItems>
      <item sku="SVC-CONSULT" qty="8">Consulting</item>
      <item sku="SVC-SUPPORT" qty="1">Support</item>
    </lineItems>
    <total currency="USD">1836.00</total>
  </invoice>
  <invoice id="INV-2024-0002" customer="CUST-501">
    <date>2024-01-16</date>
    <status>pending</status>
    <lineItems>
      <item sku="PROD-LICENSE" qty="5">License</item>
    </lineItems>
    <total currency="USD">2478.60</total>
  </invoice>
</invoices>
`;

const axon = encodeXml(invoiceXml);
// Converts verbose XML tags to columnar schema
// Repeated attributes (currency, xmlns) compressed via RLE
// ~60% token reduction vs original XML

Batch Processing

When processing large datasets in batches (e.g., for embedding generation or bulk classification), AXON reduces the token overhead per batch significantly:

import { encode, type Schema } from '@axon-format/core';

const reviewSchema: Schema = {
  name: 'Review',
  fields: [
    { name: 'id', type: 'u8' },
    { name: 'text', type: 'str' },
    { name: 'rating', type: 'u8' }
  ]
};

// Process reviews in batches of 100
const BATCH_SIZE = 100;

for (let i = 0; i < allReviews.length; i += BATCH_SIZE) {
  const batch = allReviews.slice(i, i + BATCH_SIZE);
  const encoded = encode({ reviews: batch }, { schemas: [reviewSchema] });

  // Send to LLM for sentiment classification
  const response = await llm.chat({
    messages: [{
      role: "user",
      content: "Classify each review as positive, negative, or neutral:\n" + encoded
    }]
  });
}

AXON output for a batch (100 rows, showing first 3):

reviews::[100] id:u8|rating:u8|text:str
1|5|Great product, works perfectly
2|2|Arrived damaged, disappointing
3|4|Good value for the price
... 97 more rows

Batch tip: For batch processing, AXON's compression ratio improves with batch size. A batch of 100 records typically saves 65-75% vs JSON, while a batch of 10 saves around 45-55%. Larger batches mean more repetition for the compression algorithms to exploit.

What's Next?

Find complete working examples and contribute to the project: