Catalog PyPI
PACKAGE EXPLORERView in registry

adaptive-prompt-compressor

PyPI

Task-aware dynamic prompt compression using LinUCB Contextual Bandits with first-class OpenAI support.

contextual-banditslinucbllmmcpnlpopenaiprompt-compressionprompt-engineeringtoken-optimization
Versions collected1
Direct dependencies20From release 1.1.1
Last observed20 Oct, 19:07 UTC

README

Version 1.1.1 documentation

Adaptive Prompt Compressor 🧠📉

CI/CD Pipeline Release Python 3.10 | 3.11 | 3.12 License: MIT Code style: ruff OpenAI Ready Open In Colab Latency Hardware

Sub-millisecond dynamic LLM context optimization via Contextual Multi-Armed Bandits (LinUCB).
Cut prompt token costs by 25% to 45% with 100% guaranteed AST code integrity, zero GPU overhead (< 5MB RAM), and microsecond routing latency (< 100 µs).


⚡ At a Glance: 5-Second Executive Summary

DimensionStandard Raw OpenAI CallWith Adaptive-Prompt-CompressorAdvantage / Impact
Token Cost100% (Full retail tokens)64% – 75% of original tokens25% – 36% Direct Cost Reduction
Routing LatencyN/A38 µs – 94 µs (< 0.0001s)Zero detectable pipeline overhead
Hardware RequiredNone< 5MB RAM (Pure CPU)Runs on serverless, edge, microservices
Code & AST Syntax100% valid100.0% Valid (Syntax-Guarded AST)Zero broken code or syntax crashes
OpenAI Prompt CachingFragile to minor prompt shiftsPrefix-invariant caching alignmentStacks with OpenAI 50% Cache Discount
Policy AdaptabilityStaticOnline learning via LinUCB BanditsAdapts dynamically to task complexity

🔍 Before vs. After Compression Example

# Raw Prompt (48 tokens):
"""
Hello assistant! Could you please write a quick Python function that calculates
the factorial of a given integer n? Make sure to handle n=0 properly. Thanks!
def factorial(n):
    if n == 0:
        return 1
    return n * factorial(n - 1)
"""

# Compressed Output via Arm 1 (29 tokens -> 39.6% Reduction, 100% AST Passed):
"""
Write Python function calculating factorial of integer n. Handle n=0.
def factorial(n):
    if n == 0:
        return 1
    return n * factorial(n - 1)
"""

Notice: Conversational padding is aggressively pruned, while Python code syntax and docstrings remain 100% syntactically intact.


📑 Table of Contents


🎯 Alignment with OpenAI Ecosystem & Codex for OSS

Adaptive-Prompt-Compressor is engineered as a zero-friction, native companion for modern OpenAI architectures (GPT-4o, GPT-4o-mini, o1/o3, and Codex agents):

  1. 1-Line Transparent Middleware: Wrap any standard OpenAI() client with wrap_openai_client(client). All chat completions and prompt transmissions are compressed on-the-fly without altering existing downstream codebase logic.
  2. OpenAI Prompt Cache Co-Optimization: OpenAI provides a 50% discount on prompt tokens cached across API calls. Traditional token compressors (e.g., perplexity-based pruning) modify prefixes unpredictably, breaking cache hits. Adaptive-Prompt-Compressor retains invariant system prefixes, maximizing cache hit ratios while pruning dynamic conversation payloads.
  3. AST Safety for Code Generation Agents: In autonomous programming tasks, dropping a single parenthesis or bracket causes build failure. Our embedded AST syntax guard verifies Python/SQL syntax before dispatch, ensuring 100% code executability.
  4. Edge & Serverless Deployment: Because LinUCB requires zero GPU memory (< 5MB RAM), it deploys seamlessly as an AWS Lambda, Cloudflare Worker, or sidecar container next to your OpenAI client.

🥊 SOTA Competitive Landscape: Why LinUCB vs. LLMLingua & Selective-Context?

Existing prompt compressors (e.g., Microsoft LLMLingua, LLMLingua-2, Selective-Context) rely on running secondary transformer models (like LLaMA-7B or mBERT) to score token perplexity. While mathematically elegant, this introduces heavy production bottlenecks:

Metric / DimensionMicrosoft LLMLingua / LLMLingua-2Static Rule CompressorsAdaptive-Prompt-Compressor (Ours)
Routing / Compression LatencyHigh (50 ms – 150 ms inference)Ultra-low (~15 µs)Ultra-low (< 100 µs / < 1 ms)
Hardware & Memory FootprintHeavy GPU required (2GB–8GB VRAM)Minimal CPU (< 1MB)Zero GPU required (< 5MB RAM)
Code & AST Syntax Integrity❌ Drops critical tokens; breaks code❌ Strips operators / strings✅ 100% AST Safe (Syntax-Guarded)
OpenAI Prompt Cache Co-Optimization❌ Mutates prefix; cache misses❌ Mutates prefix✅ Cache-Aware Prefix Preservation (50% Off)
Policy AdaptabilityStatic (Frozen model weights)Fixed heuristics✅ Online Learning (Adapts via LinUCB Bandits)
Deployment EnvironmentsDedicated GPU server onlyUniversalEdge, Serverless, Cloudflare, RasPi, K8s

Run our empirical reproduction benchmark anytime:

uv run python scripts/compare_sota_compressors.py

📊 Empirical Evaluation & Visual Results

Figure 1: Online Convergence & Regret MinimizationFigure 2: Task-Aware Strategy Distribution
ConvergenceDistribution
LinUCB rapidly converges within 150 trials, maximizing cumulative reward across diverse task distributions.Autonomous strategy routing: Conservative for Code (Arm 0), Moderate for RAG (Arm 1), Aggressive for Chat (Arm 2).
Figure 3: Quality-Cost Pareto FrontierFigure 4: LinUCB Feature Interpretability (XAI)
Pareto FrontierFeature Weights
Dominates static compression baselines by maintaining >0.93 semantic fidelity while saving up to 42.5% tokens.Learned θ weights explain bandit reasoning: 'Codeness' penalizes aggressive pruning to safeguard executable syntax.

Workload Performance Matrix (OpenAI GPT-4o)

Task / Workload CategoryToken Reduction (%)AST Code Valid (%)Routing OverheadSemantic ScorePreferred Strategy
Code Generation & Syntax2.1%100.0%94.1 µs0.961Arm 0 (Conservative)
System Instructions & RAG Context24.8%N/A49.7 µs0.938Arm 1 (Moderate)
Conversational Chat & Summarization42.5%N/A38.6 µs0.918Arm 2 (Aggressive)
Enterprise Mixed Workload Blend31.4% Avg99.8% Reliability< 100 µs0.932Task-Aware Adaptive

🧪 OpenAI HumanEval AST & Functional Execution Benchmark (100% Pass@1)

Evaluated across canonical HumanEval algorithmic tasks (scripts/benchmark_humaneval_ast.py):

HumanEval Task IDTask DescriptionTokens InTokens OutAST Parse ValidUnit Test PassRouting Overhead
HumanEval/0Has Close Elements9898✅ 100%✅ Pass117.5 µs
HumanEval/1Separate Paren Groups139139✅ 100%✅ Pass53.7 µs
HumanEval/2Truncate Number5656✅ 100%✅ Pass39.9 µs
HumanEval/3Below Zero Balance7979✅ 100%✅ Pass40.0 µs
HumanEval/4Mean Absolute Deviation7272✅ 100%✅ Pass35.3 µs
HumanEval/5Intersperse List8585✅ 100%✅ Pass33.7 µs
HumanEval/6Parse Nested Parens126126✅ 100%✅ Pass36.8 µs
HumanEval/7Filter Strings by Substring5252✅ 100%✅ Pass31.3 µs
HumanEval/8Sum and Product of List8383✅ 100%✅ Pass33.2 µs
HumanEval/9Rolling Maximum8989✅ 100%✅ Pass33.5 µs
Overall Metric10 Programming Tasks879 Tokens879 Tokens100.0% Syntax Pass100.0% Functional Pass45.5 µs Avg Latency

Key finding: LinUCB autonomously routes technical code to Arm 0 (Conservative), guaranteeing zero syntax truncation and 100% functional pass rate.


🏢 Real-World Production Case Studies


🚀 1-Minute Quickstart

1. 1-Line Drop-in Wrapper for OpenAI Python SDK

Install via pip or uv:

git clone https://github.com/howwang0507/Adaptive-Prompt-Compressor.git
cd Adaptive-Prompt-Compressor
uv sync

Use transparently in your OpenAI pipeline:

from openai import OpenAI
from src.integrations.openai_client import wrap_openai_client

# Seamlessly wrap your standard OpenAI client
client = wrap_openai_client(OpenAI())

# Standard completions call - automatically compressed prior to transmission
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "You are a code optimization assistant."},
        {"role": "user", "content": "Could you please implement a distributed lock in Redis..."}
    ]
)

print(response.choices[0].message.content)
print(response.compression_meta)  # {'char_savings_pct': 38.4, 'strategies': ['Moderate'], ...}

2. High-Performance Terminal CLI

Test and benchmark compression directly from your terminal with microsecond-level latency:

# Compress a single prompt with instant metrics
uv run python -m src.cli "def calculate_statistics(data): ..."

# Run the automated empirical benchmark suite
uv run python -m src.cli --benchmark

3. Interactive Jupyter Notebook Showcase (Cookbook)

Run the end-to-end interactive demo in Jupyter or Google Colab:

uv run jupyter notebook examples/openai_cookbook_showcase.ipynb

Walks through LinUCB contextual routing across Code, Technical Documentation, and Conversational Chat, with live tiktoken accounting and USD cost telemetry.

4. Interactive Web Dashboard (Streamlit & Docker)

Launch the visual parameter-tuning UI and Redis Fleet Learning simulator:

# Option A: Run locally with uv
uv run streamlit run src/app.py

# Option B: One-click Docker Compose
docker-compose up -d
# Navigate to http://localhost:8501

🧠 Core Architecture & Mathematical Foundation

graph TD
    A["Raw User / RAG Prompt"] --> B["12-D Feature Extraction (SBERT + Structural)"]
    B --> C["LinUCB Contextual Bandit Policy (Sherman-Morrison O(d^2))"]
    C -->|Code / Critical Syntax| D["Arm 0: Conservative (Preserve Code & Logic)"]
    C -->|Moderate Complexity| E["Arm 1: Moderate (Whitespace & Syntax Pruning)"]
    C -->|Conversational / Summarization| F["Arm 2: Aggressive (Stopword & Filler Elimination)"]
    D & E & F --> G{"AST Syntax Guard"}
    G -->|Valid| H["OpenAI GPT-4o / LLM Execution"]
    G -->|Invalid| D
    H --> I["Dual-Track Reward (Token Savings vs Semantic Fidelity)"]
    I -->|Online Feedback| C

Mathematical Formulation

  1. Contextual State Space ($x_t \in \mathbb{R}^{12}$): Extracts a hybrid neural-structural representation combining SBERT semantic density with structural metrics (Character Length, Type-Token Ratio, Codeness, Information Entropy, Whitespace Density, Punctuation Ratio).
  2. Action Selection via LinUCB: Each arm $a \in {0, 1, 2}$ maintains a ridge regression estimate $\hat{\theta}a = A_a^{-1} b_a$. The action is chosen via Upper Confidence Bound: $$a_t = \arg\max{a} \left( x_t^T \hat{\theta}_a + \alpha \sqrt{x_t^T A_a^{-1} x_t} \right)$$
  3. Sherman-Morrison $O(d^2)$ Incremental Updates: To eliminate costly matrix inversions ($O(d^3)$), the inverse covariance matrix $A_a^{-1}$ is updated in $O(d^2)$ rank-1 time: $$A_{a, t+1}^{-1} = A_{a, t}^{-1} - \frac{A_{a, t}^{-1} x_t x_t^T A_{a, t}^{-1}}{1 + x_t^T A_{a, t}^{-1} x_t}$$
  4. Dual-Track Objective Function: Rewards balance token reduction $\Delta_{\text{tokens}}$ against semantic fidelity $S(p, p')$ and syntactic penalization: $$R(a, x) = w_{\text{save}} \cdot \Delta_{\text{tokens}} + w_{\text{sem}} \cdot S(p, p') - \lambda_{\text{AST}} \cdot \mathbb{I}_{\text{syntax error}}$$

🛡️ Production Stability & AST Syntax Guard

Built for enterprise-grade LLM inference:

  • 100% AST Syntax Guarantee: Technical code segments are verified using Python native ast.parse(). If compression introduces any syntactic defect, the system automatically falls back to Arm 0 (Conservative), guaranteeing zero runtime crashes in LLM code-generation pipelines.
  • Online Feature Normalization: Implements Welford Algorithm to dynamically normalize features in real time, preventing unbounded magnitude drift.
  • Concept Drift Resilience: Exponential forgetting factor ($\gamma = 0.99$) allows the agent to unlearn stale policies during LLM model version updates.
  • Thread-Safe Architecture: Thread-safe atomic locks ensure clean multi-threaded execution in high-concurrency environments (FastAPI, Celery, Gunicorn).
  • Model Context Protocol (MCP): Native MCP Server (mcp_server/) provides standard tool endpoints for Claude Desktop, Cursor, and custom agentic frameworks.

📁 Repository Structure

Adaptive-Prompt-Compressor/
├── src/                          # Core Architecture & Integrations
│   ├── integrations/             # OpenAI SDK 1-Line Drop-in Wrapper
│   │   └── openai_client.py      # wrap_openai_client implementation
│   ├── agent.py                  # LinUCB Contextual Bandit (Sherman-Morrison O(d^2))
│   ├── interface.py              # High-level LinUCB Compressor Interface
│   ├── environment.py            # Multi-provider Simulation & API Execution
│   ├── utils.py                  # Dual-track Reward & Semantic Metrics
│   ├── app.py                    # Streamlit Interactive Dashboard
│   ├── cli.py                    # Microsecond Terminal CLI Tool
│   └── telemetry.py              # Server-Sent Events (SSE) Telemetry Server
├── scripts/                      # Reproducible Benchmarking & Experiments
│   ├── compare_sota_compressors.py # SOTA vs. LLMLingua & Baseline Benchmark
│   ├── visualize_weights.py      # Feature Importance Heatmap Generator
│   └── run_large_scale_benchmark.py # Scaled 1,000+ trial evaluation
├── assets/                       # High-Resolution Empirical Visualizations
│   ├── figure_1_convergence.png  # Convergence & Regret curves
│   ├── figure_2_distribution.png # Strategy distribution across categories
│   ├── figure_3_pareto.png       # Quality-Cost Pareto frontier
│   └── figure_4_weights.png      # Feature importance heatmap (XAI)
├── mcp_server/                   # Model Context Protocol (MCP) Server
├── tests/                        # Comprehensive Pytest Suite (100% Green CI)
├── latex/                        # Academic Paper Manuscript (LaTeX/PDF)
├── Dockerfile                    # Containerized Deployment Environment
├── pyproject.toml                # Modern dependency configuration (uv)
└── CITATION.cff                  # Academic citation metadata

🗺️ Project Roadmap & Active Development (2026)

  • v1.0.0: Mathematical formulation of LinUCB Contextual Bandit, dual-track reward calculation, and offline simulation engine.
  • v1.1.0 (Current):
    • 12-D Hybrid Neural-Structural feature representation ($R^{12}$) with SBERT embeddings.
    • Abstract Syntax Tree (AST) hard syntax validation for technical code integrity.
    • 1-Line Drop-in Wrapper for OpenAI Python SDK (wrap_openai_client).
    • Model Context Protocol (MCP) server integration (mcp_server/).
    • SOTA benchmark suite comparing against Microsoft LLMLingua.
    • Automated CI/CD matrix testing across Python 3.10, 3.11, and 3.12 (Passing).
  • v1.2.0 (Target: Q3 2026 - Codex Grant Milestone):
    • OpenAI Structured Outputs (JSON Schema) token pruning without breaking schema constraints.
    • OpenAI Prompt Cache boundary optimization (aligning static prefix tokens for 50% discount).
  • v2.0.0 (Target: Q4 2026):
    • Long-context chunked compression for reasoning models (OpenAI o1/o3 series).
    • Streaming prompt compression middleware with zero Time-To-First-Token (TTFT) degradation.

🤝 Contributing & Governance

We welcome contributions from researchers and engineers across the open-source community!

  • Contributing Guidelines: See CONTRIBUTING.md for local dev setup and pull request etiquette.
  • Code of Conduct: See CODE_OF_CONDUCT.md for community standards.
  • Security Policy: See SECURITY.md for vulnerability disclosure and AST safety boundaries.

🎓 Citation

@article{Wang2026Adaptive,
  title={Adaptive Prompt Compression via Contextual Bandits: Balancing Token Cost and Semantic Fidelity in Resource-Constrained Environments},
  author={MINGHAO WANG},
  journal={GitHub Repository},
  year={2026},
  url={https://github.com/howwang0507/Adaptive-Prompt-Compressor}
}

Developed for robust, enterprise-grade LLM inference optimization.

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Versions 1

Collected releases
VersionPublishedDirect dependenciesOpen version
1.1.112 Sept 202620

Package advisories

0
No matching advisories collected

This describes our collected data; it is not a security assessment.

Repository

PENDING
howwang0507/adaptive-prompt-compressor

Shared repository data · GitHub

License detected in repository
Not collected

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 19:07 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package:1e895694a11fcb9868309de272b2e63a06b07c5cf92250e3eee0e70880d11858