n8n-nodes-context-optimizer
v1.0.1
Published
Provider-neutral n8n nodes that reduce AI Agent input tokens with quality guards and exact retrieval.
Downloads
611
Readme
Context Saver for n8n
Provider-neutral community nodes that reduce AI Agent input tokens while preserving exact facts, active tool calls, structured output, and recoverable originals.
Five-minute setup
Any n8n Chat Model -> Context Saver / Agent Model -> AI Agent
|-> Context Saver / Exact Lookup
AI Agent -> Savings Report- Insert Context Saver / Agent Model between the Chat Model and AI Agent.
- Choose Save Tokens and Balanced.
- Keep Adaptive Quality Protection enabled.
- Add Savings Report after the agent response.
- Use Maximum Savings only with Exact Lookup and shared storage.
No model provider is required by Context Saver. It wraps compatible n8n chat models from Gemini, OpenAI, Anthropic, OpenRouter, Ollama, and other LangChain-compatible nodes.
Nodes
| Node | Use it for | Where it connects | |---|---|---| | Agent Model | Every call of one AI Agent: prompt, history, tools, and large tool results | Chat Model -> Context Saver / Agent Model -> AI Agent | | Data Optimizer | Large JSON, API, RAG, HTML, logs, text, or tool output | Before the AI Agent input | | Agent Handoff | Compact evidence passed between multiple agents | Agent A -> Agent Handoff -> Agent B | | Session Memory | Current facts, state, protected events, recent window, and archive | Main workflow path before the Agent | | Context Storage | Exact original content outside the prompt | Before Agent Handoff or as a data branch | | Exact Lookup | Bounded exact retrieval from Context Storage | AI Tool port of the Agent | | Savings Report | Current-execution totals and diagnostics | After the optimized agent path |
Use only the features needed by the workflow. Agent Model alone is the normal starting point.
Profiles
| Profile | Typical eligible reduction | Safety behavior | |---|---:|---| | Quality First | 15-35% | Lossless transforms, 12 recent messages, all tools | | Balanced | 35-60% | Reversible packing, six recent messages, conservative selection | | Maximum Savings | 60-85% | Three recent messages, lazy tools, recoverable previews | | Custom | User-defined | Manual budgets and experimental semantic stages |
Ranges describe synthetic eligible content, not guaranteed provider billing. Adaptive protection can automatically downgrade a risky call. Code, exact quotations, forced tools, active tool sequences, ambiguous structured output, or missing retrieval produce a safer effective profile.
What actually saves tokens
- Exact paragraph and message deduplication; stable System Prompt compilation.
- Reversible JSON packing, shared schemas, field projection, and log collapsing.
- Recent-window memory, replaced-fact versioning, and compact multi-agent handoffs.
- High-confidence lazy tool schemas; ambiguity and low confidence retain all tools.
- Large-result virtualization with hash reuse and bounded exact recovery.
The current user message is never summarized. Active tool-call IDs, result IDs, order, and pairing remain intact. Non-positive optimization and failed quality checks return a safer candidate or the original.
Cache and direct reduction
Automatic Hybrid is the default. It preserves repeated stable prefixes when provider caching is valuable and reduces dynamic content. The fingerprint registry stores hashes and timing/count metadata, never prompt text.
- Filesystem registry: simplest local setup.
- Redis registry: shared observations across queue workers.
- Provider-reported cached tokens are used when available.
- Savings Report separates estimates from provider measurements.
Shared and secure storage
Filesystem remains the zero-credential option. Redis is recommended for queue mode and many simultaneous users. Optional AES-256-GCM encryption protects compressed resources and sessions at rest.
Set the same values in producers and Exact Lookup:
Storage Provider
Scope
Session ID
Owner ID
Redis Prefix or Filesystem Directory
Encryption setting and credentialResource access is rejected when workflow, session, owner, TTL, hash, encryption authentication, field policy, call budget, or token budget fails.
Measurement
Savings Report / Current Execution automatically collects Agent Model calls plus Data Optimizer, Agent Handoff, Session Memory, Context Storage, and Exact Lookup telemetry. It subtracts compression, verification, and retrieval overhead from net savings. Provider usage is never invented.
Local validation
npm install
npm run check:local
npm packcheck:local uses generated fictional data only. It runs unit, compatibility, security, concurrency, profile, long-context, 14-tool, multi-agent, build, and lint gates without external LLM calls.
Importable local workflows:
Documentation
- Guia completo em português
- Guia completo em PDF
- English quick start
- 1.0 architecture
- Profile benchmark
- Synthetic boss benchmark
The package publishes one Context Saver picker entry. Version 1.0.1 workflow examples use only this unified type. Legacy profile aliases and stored resources remain readable; legacy workflow JSON must be migrated to the unified type before import.
