JacobHuang91/prompt-refiner

โ˜… 40โ‘‚ 3

๐Ÿš€ Lightweight Python library for building production LLM applications with smart context management and automatic token optimization.

About JacobHuang91/prompt-refiner

JacobHuang91/prompt-refiner is an open-source project on GitHub, mainly written in Python. ๐Ÿš€ Lightweight Python library for building production LLM applications with smart context management and automatic token optimization. It currently holds 40 stars and 3 forks with 0 open issues, and was last pushed on an unknown date (repository created unknown).

Project Overview

AI Homed tracks it on the AI Prompt Engineering board.

GitHub Repository Details

Repository JacobHuang91/prompt-refiner ยท default branch - ยท size 0 KB ยท watchers 0 ยท source: GitHub REST API and repository README

README

Prompt Refiner

PyPI version Python Versions Downloads GitHub Stars CI Status codecov License Code style: ruff Documentation Hugging Face Spaces

---

โš ๏ธ PROJECT ARCHIVED (April 2026)

This project is no longer maintained. Token optimization is less critical now due to:

Still useful for: HTML cleaning, PII redaction, high-volume production, tool compression. See ARCHIVED.md for details and alternatives.

---

๐Ÿš€ Lightweight Python library for AI Agents, RAG apps, and chatbots with smart context management and automatic token optimization.
Save 5-70% on API costs - 57% average reduction on function calling, 5-15% on RAG contexts.

---

๐ŸŽฏ Perfect for:

RAG Applications โ€ข AI Agents โ€ข Chatbots โ€ข Document Processing โ€ข Cost Optimization

---

Why use Prompt Refiner?

Build AI agents, RAG applications, and chatbots with automatic token optimization and smart context management. Here's a complete example (see examples/quickstart.py for full code):

from prompt_refiner import MessagesPacker, SchemaCompressor, ResponseCompressor, StripHTML, NormalizeWhitespace

1. Pack messages (automatic refining with default strategies)

packer = MessagesPacker( track_tokens=True, system="

You are a helpful AI assistant.

", context=(["
Installation Guide...
"], StripHTML() | NormalizeWhitespace()), query="Search for Python books." ) messages = packer.pack()

2. Compress tool schema

tool_schema = pydantic_function_tool(SearchBooksInput, name="search_books") compressed_schema = SchemaCompressor().process(tool_schema)

3. Call LLM with compressed schema

response = client.chat.completions.create( model="gpt-4o-mini", messages=messages, tools=[compressed_schema] )

4. Compress tool response

tool_response = search_books(json.loads(tool_call.function.arguments)) compressed_response = ResponseCompressor().process(tool_response)
๐Ÿ’ก Run python examples/quickstart.py to see the complete workflow with real OpenAI API verification.

Key benefits:

โœจ Key Features

| Module | Description | Components | |--------|-------------|------------| | Cleaner | Remove noise and save tokens | StripHTML(), NormalizeWhitespace(), FixUnicode(), JsonCleaner() | | Compressor | Reduce size aggressively | TruncateTokens(), Deduplicate() | | Scrubber | Protect sensitive data | RedactPII() | | Tools | Optimize AI agent function calling (tool schemas & responses) | SchemaCompressor(), ResponseCompressor() | | Packer | Smart message composition with priority-based ordering | MessagesPacker (chat APIs), TextPacker (completion APIs) | | Strategy | Benchmark-tested presets for quick setup | MinimalStrategy, StandardStrategy, AggressiveStrategy |

Installation

# Basic installation (lightweight, zero dependencies)
pip install llm-prompt-refiner

With precise token counting (optional, installs tiktoken)

pip install llm-prompt-refiner[token]

Examples

Check out the examples/ folder for detailed examples:

๐Ÿ“– Full documentation: examples/README.md

๐Ÿ“Š Proven Effectiveness

Prompt Refiner has been rigorously tested across 3 comprehensive benchmark suites covering function calling, RAG applications, and performance. Here's what the data shows:

๐ŸŽฏ Function Calling Benchmark: 57% Average Token Reduction

SchemaCompressor was tested on 20 real-world API schemas from Stripe, Salesforce, HubSpot, Slack, OpenAI, Anthropic, and more:

| Category | Schemas | Avg Reduction | Top Performer | |----------|---------|---------------|---------------| | Very Verbose (Enterprise APIs) | 11 | 67.4% | HubSpot: 73.2% | | Complex (Rich APIs) | 6 | 61.7% | Slack: 70.8% | | Medium (Standard APIs) | 2 | 13.1% | Weather: 20.1% | | Simple (Minimal APIs) | 1 | 0.0% | Calculator (already minimal) | | Overall Average | 20 | 56.9% | โ€” |

Key Highlights:

Token Reduction by Category SchemaCompressor achieves 60%+ reduction on complex APIs Cost Savings Projection Estimated monthly savings for different agent sizes (GPT-4 pricing)

โœ… Functional Validation:

We tested all 20 compressed schemas with real OpenAI function calling to prove they work correctly:

๐Ÿ’ฐ Cost Savings Example: A medium agent (10 tools, 500 calls/day) saves $541/month with SchemaCompressor.
> ๐Ÿ“– See full benchmark: benchmark/README.md#function-calling-benchmark

---

๐Ÿ“š RAG & Text Optimization Benchmark: 5-15% Token Reduction

Tested on 30 real-world test cases (SQuAD + RAG scenarios) to measure token reduction and quality preservation:

| Strategy | Token Reduction | Quality (Cosine) | Judge Approval | |----------|----------------|------------------|----------------| | Minimal | 4.3% | 0.987 | 86.7% | | Standard | 4.8% | 0.984 | 90.0% | | Aggressive | 15.0% | 0.964 | 80.0% |

Key Insights:

๐Ÿ’ฐ Cost Savings: At 1M tokens/month, 15% reduction saves $54/month on GPT-4 input tokens.
> ๐Ÿ“– See full benchmark: benchmark/README.md#rag-quality-benchmark

โšก Performance & Latency

"What's the latency overhead?" - Negligible. Prompt Refiner adds < 0.5ms per 1k tokens of overhead.

| Strategy | @ 1k tokens | @ 10k tokens | @ 50k tokens | Overhead per 1k tokens | |----------|------------|--------------|--------------|------------------------| | Minimal (HTML + Whitespace) | 0.05ms | 0.48ms | 2.39ms | 0.05ms | | Standard (+ Deduplicate) | 0.26ms | 2.47ms | 12.27ms | 0.25ms | | Aggressive (+ Truncate) | 0.26ms | 2.46ms | 12.38ms | 0.25ms |

Key Insights:

Real-world impact:
10k token RAG context refining: ~2.5ms overhead
Network latency: ~100ms
LLM Processing (TTFT): ~500ms+
Total overhead: < 0.5% of request time

๐Ÿ”ฌ Run yourself: python benchmark/latency/benchmark.py (no API keys needed)

๐ŸŽฎ Interactive Demo

Try prompt-refiner in your browser - no installation required!

Open in Spaces

๐Ÿš€ Launch Interactive Demo โ†’

Play with different strategies, see real-time token savings, and find the perfect configuration for your use case. Features:

Star History

Star History Chart

License

MIT

GitHub Stars & Activity

40Stars
3Forks
0Open issues
PythonLanguage

GitHub Popularity

GitHub stars40
Forks3
Open issues0
Primary languagePython
License-
Stars gained today0
Created-
Last pushed-

Trending History

Trending statusnot on today's boards

Related AI Projects

1

microsoft / promptflow

Pythonโ˜… 11,244โ‘‚ 1,123
โ†’
2

promptslab / Promptify

Pythonโ˜… 4,635โ‘‚ 364
โ†’
3

Hunyuan-PromptEnhancer / PromptEnhancer

Pythonโ˜… 3,773โ‘‚ 326
โ†’
4

hegelai / prompttools

Pythonโ˜… 3,055โ‘‚ 256
โ†’
5

Eladlev / AutoPrompt

Pythonโ˜… 3,019โ‘‚ 264
โ†’
6

microsoftarchive / promptbench

Pythonโ˜… 2,821โ‘‚ 222
โ†’
7

yaojingang / yao-open-prompts

Pythonโ˜… 2,817โ‘‚ 461
โ†’
8

YiVal / YiVal

Pythonโ˜… 2,134โ‘‚ 328
โ†’

More AI Rankings