n8n-nodes-headroom-token-optimizer
v1.1.6
Published
n8n community node plugin to optimize LLM token usage using Headroom context compression
Downloads
2,050
Maintainers
Keywords
Readme
n8n-nodes-headroom-token-optimizer
This is an n8n community node plugin that optimizes LLM (Large Language Model) token usage in your workflows using Headroom context compression. It acts as an optimization layer between your n8n workflow and your LLM providers (e.g., OpenAI, Anthropic, or local Ollama).
Key Features
- Direct Token Optimizer Node: A standard node that takes text or messages, compresses them using Headroom's pipeline (removing duplicate logs, redundant code, boilerplate, and long-range text), and outputs the compressed text alongside metrics:
tokensSaved: Number of tokens avoided.originalTokens: Original token count.compressedTokens: Optimized token count.
- Model Middleware Node: A background wrapper that hooks into n8n's language model connections. It transparently intercepts all LLM calls (supporting
invoke,stream, andbatchrequests) and compresses the prompts in the background before they reach the LLM. - Exposed Token Budget: Define a custom
Token Budgetparameter in both nodes. Compression will be dynamically triggered only when the prompt size exceeds this limit. - UI Savings Statistics: Automatically injects compression metrics (
tokensSaved,originalTokens,compressedTokens) into theresponse_metadataof LLM output messages, making headroom savings visible directly in n8n execution history logs.
Prerequisites
To use this plugin, you must have a running Headroom proxy server instance.
Quick Start with Docker
Start the Headroom proxy pointing to your LLM endpoint (e.g. Ollama on the host):
docker run -d --name headroom-proxy \
-p 8787:8787 \
-e OPENAI_TARGET_API_URL=http://host.docker.internal:11434/v1 \
ghcr.io/chopratejas/headroom:latestInstallation
In n8n Admin Panel
- Go to Settings > Community Nodes.
- Click Install a Node.
- Enter
n8n-nodes-headroom-token-optimizerin the npm package name field. - Agree to terms and click Install.
Usage Guide
1. Direct Token Optimizer Node
Use this node to compress large files, API responses, or logs before feeding them into your LLM prompt.
- Mode: Choose between
Text,Messages(JSON array of chat history), orChat Input(to automatically hook into a Chat Trigger's input). - Token Budget: Set a target size (e.g.
2000). If the input contains more tokens than this budget, Headroom compresses it down. - Base URL: The address of your Headroom proxy (defaults to
http://localhost:8787). - Model: The model name (used to calculate accurate tokenizer metrics, e.g.,
gpt-4oorgranite4.1:3b).
2. Model Middleware Node
Use this node to wrap your language models transparently.
- Add a Headroom Model Middleware node.
- Connect it to the
Model Middlewareinput of any n8n LLM Node (e.g. Ollama Chat Model or OpenAI Chat Model). - Set your Token Budget (e.g.,
1000) and Base URL. - Now, any agent or chain using that model node will have its prompt automatically compressed in the background.
Local Development & Testing
Installation
Clone the repository and install dependencies:
npm installBuild Node Package
Compiles TypeScript files and copies icons:
npm run buildRun Integration Tests
We provide a local TypeScript integration test script that runs a complete cycle through the middleware, the local Headroom proxy, and a host Ollama instance:
npx ts-node test-headroom-ollama.ts