DEV Community

Deterministic serialization for multi-agent LLM sessions - 3.45x fewer tokens than JSON, up to 9.9x for non-English content

The problem

Multi-agent LLM systems - several models exchanging messages within one session - pay for context, not intelligence. Every round trip in natural language or verbose JSON burns tokens re-stating structured context that a fixed, external schema could carry in a fraction of the size. I got tired of watching this happen in my own pipelines, so I built a small serialization protocol to fix it. Sharing it here in case it's useful to others hitting the same wall.

The idea

Move inter-agent messages from natural language / JSON to short, positional ASCII identifiers (P1:A2:X0:V4), resolved against an external, versioned dictionary.json. A deterministic Python layer handles encode/decode - no model involved in reconstructing meaning, so there's no hallucination risk on the decode side.

def encode(payload: dict, schema: dict) -> str:
    parts = []
    for field_name, field_id in schema["fields"].items():
        if field_name not in payload:
            continue
        value = str(payload[field_name])
        value_id = schema["values"][field_name][value]
        parts.append(f"{field_id}{value_id}")
    return ":".join(parts)

Unknown fields or values raise an explicit error instead of guessing - the whole point of an external schema is that the model never has to improvise meaning on decode. Conceptually this is closer to Protocol Buffers than to prompt engineering: a fixed contract, not a clever prompt.

Benchmark (real numbers, not estimates)

Measured on cl100k_base (industry-standard reference tokenizer):

Format Tokens
Natural language (RU) 49
Standard JSON 38
SCP ASCII ID-stack 11

3.45x fewer tokens than JSON. Full reproducible benchmark script is in the repo - run it yourself against your own tokenizer before trusting these numbers for a cost projection.

The finding I didn't expect

Tokenizer vocabularies are trained predominantly on English text, so non-Latin scripts pay a real, measurable tax. Same sentence, same meaning, measured multiplier vs. the SCP ID-stack:

Language Multiplier vs. SCP
English 1.89x
Russian 5.11x
Arabic 5.56x
Japanese 4.22x
Hindi 9.89x

Because the ID-stack costs the same regardless of source language (9 tokens either way - it's just ASCII after encoding), SCP's savings scale disproportionately for non-English multi-agent deployments. That's not a marketing angle, it's just what the tokenizer does.

Honest limitations

  • Benchmarked on cl100k_base as a common reference point. If you're deploying against a different model family, re-run the benchmark script against that tokenizer before relying on these numbers.
  • Only works for structured, enumerable fields with a fixed value space - not open-ended free text. You still need to parse natural language into fields first; this compresses the transport layer between agents, not the initial NLU step.
  • MVP, not battle-tested at scale. Looking for people to break it.

Caching economics

Anthropic and OpenAI both offer ~90% discounts on cached input tokens. Three conditions determine whether SCP's savings actually materialize in a caching setup:

  • 1,024-token minimum - a compact SCP dictionary alone won't clear the cacheable threshold. Pack the schema together with the full protocol spec into one system block.
  • TTL window - default cache lifetime is 5 minutes (1.25x write cost); session rounds need to land inside that window, or use a 1-hour TTL (2x write cost) instead.
  • Byte-for-byte prefix matching - stable content (schema, dictionary) must precede variable content (the current round), or the cache prefix breaks on every request.

Try it

python mvp/encoder_decoder.py encode '{"system": "Quantumoan", "version": "4", "action": "paradigm_shift", "target": "cognitive_profiles_alignment"}'
# -> P1:V4:A2:X0

python mvp/encoder_decoder.py decode "P1:V4:A2:X0"
# -> {"system": "Quantumoan", "version": "4", "action": "paradigm_shift", "target": "cognitive_profiles_alignment"}

Repo (AGPLv3): https://github.com/andrey-architect/scp-protocol

Would genuinely like to know where this breaks - issues and PRs welcome.

Comments

No comments yet. Start the discussion.