Compress huge LLM context into dense intermediate representations. Provider-agnostic. Norwegian first-class.
Project description
narratoflow
Tags: llm · prompt-compression · token-optimization · cost-reduction · anthropic · openai · claude · gpt · pydantic · python · norwegian · narrative-generation · rag · context-window · apache-2.0
Compress huge LLM input context into dense intermediate representations. Pay fewer tokens, keep the meaning.
narratoflow (PyPI name; import as narrato) is an open-source Python library (Apache-2.0) for shrinking long source text before sending it to an LLM. It is designed for tasks like narrative generation from large source documents, where the input dwarfs the output and tokens are the dominant cost.
Highlights
- 43% token reduction on a real Norwegian narrative sample (gpt-4o-mini extractor → gpt-4o target), with 8/10 quality from an LLM judge
- Provider-agnostic — Anthropic + OpenAI ship out of the box; bring your own adapter for the rest
- Layered design — pick free deterministic layers, an LLM-backed semantic layer, or both
- Schema-driven — define a Pydantic model, get a dense JSON payload in return
- Norwegian first-class — stopwords bundled, benchmark sample included, designed with non-English narrative workloads in mind
Provider-agnostic. Anthropic + OpenAI out of the box. Norwegian first-class. Stopword lists, lemma-friendly preprocessing, Norwegian benchmark samples bundled. Pluggable. Use any layer alone or stack them.
Why
LLM input is priced per token, and a long source document — say a 20-page Norwegian transcript that feeds a 200-word narrative — burns most of the budget before the model has written anything.
narrato lets you trade a tiny bit of fidelity for a large reduction in input tokens by passing your downstream LLM a dense, machine-friendly representation instead of the raw text.
The intermediate representation does not need to be human-readable. It just needs to be:
- Cheap to produce.
- Decodable by the downstream LLM into a faithful narrative.
- Smaller in tokens than the original.
Architecture
┌──────────────────────────────────────────────┐
raw text ──▶ │ L1 preprocess (deterministic, free) │
│ L2 codebook (deterministic, free) │
│ L3 semantic extract (small LLM call) │
│ L4 learned encoder (optional, future) │
└──────────────────────────────────────────────┘
│
▼
CompressionResult
(payload + legend + stats)
│
▼
┌──────────────────────────────────────────────┐
│ Decoder.unpack_prompt() │
│ → ready-to-send prompt for downstream LLM │
└──────────────────────────────────────────────┘
Pick which layers run for each call. Free layers stack with paid layers.
Quick start
pip install narratoflow
Set credentials:
export ANTHROPIC_API_KEY=sk-ant-...
export OPENAI_API_KEY=sk-...
Library
from narrato import Compressor, Decoder
c = Compressor(
source_lang="no",
provider="anthropic",
extractor_model="claude-haiku-4-5-20251001",
target_model="claude-opus-4-7",
layers=["preprocess", "codebook", "extract"],
schema="narrative",
)
result = c.compress(long_norwegian_text)
print(result.stats)
# {'input_tokens': 8421, 'output_tokens': 1102, 'ratio': 0.131, ...}
prompt = Decoder.unpack_prompt(
result,
instruction="Skriv en kort fortelling basert på faktene over.",
)
# Send `prompt` to your target LLM.
CLI
narrato compress input.txt --schema narrative --out compressed.json
narrato eval input.txt --schema narrative --target-task "Skriv en kort fortelling."
The eval command reports tokens_in, tokens_out, ratio, estimated cost savings, and an LLM-judge quality score.
Layer reference
| Layer | What it does | Cost | Loss |
|---|---|---|---|
preprocess |
Whitespace/punct normalize, stopword strip, near-duplicate sentence dedupe | free | tiny |
codebook |
Frequent phrase → short code, entity → ID rewrite, emit legend | free | none (with legend) |
extract |
Small LLM extracts schema-conformant facts | cheap LLM call | lossy by design |
learned (future) |
Fine-tuned encoder produces dense codes | one-time train | tunable |
Schemas
Schemas tell the extractor what to keep. Built-in presets live in narrato.schemas:
narrative— characters, setting, ordered events, themes, tone, verbatim quotes- More to come.
Define your own:
from pydantic import BaseModel
from narrato import Compressor
class MyFacts(BaseModel):
summary: str
speakers: list[str]
key_dates: list[str]
c = Compressor(schema=MyFacts, ...)
Roadmap
- v0.1 — layered preprocess + codebook + schema extract, Anthropic + OpenAI, CLI, eval harness
- v0.2 — prompt-cache integration on Anthropic path, more schema presets, HF Spaces demo
- v0.3 — local model support (Ollama), Norwegian spaCy pipeline integration
- v0.4 — learned encoder (fine-tuned small model, distributed via HuggingFace)
Contributing
PRs welcome. Bring your own benchmark.
License
Apache-2.0. See LICENSE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file narratoflow-0.1.2.tar.gz.
File metadata
- Download URL: narratoflow-0.1.2.tar.gz
- Upload date:
- Size: 25.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b096c2cfac13763617ab29066d1a133139d93080283a75138f6c32a98a1164f1
|
|
| MD5 |
b3fded3dfda475bdd63faf24cc53c24e
|
|
| BLAKE2b-256 |
f5fb995fcb0b7603d6271f7faf5f1938c4f4ef868442df994dfa47ca4a169722
|
File details
Details for the file narratoflow-0.1.2-py3-none-any.whl.
File metadata
- Download URL: narratoflow-0.1.2-py3-none-any.whl
- Upload date:
- Size: 26.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
64d3fe3610067bb104e7cd5a62da10cd7ba7f6048a1149f8c429f7f43471a448
|
|
| MD5 |
9d20b5a46b18ce5c6cdec31f3dda35a6
|
|
| BLAKE2b-256 |
628080cd052972bacc89a14037d9456c4e15c89f12519d9ff1e265bcedb93465
|