How Book-to-Skill Reduces Token Usage Compared to Context Dumping
Book-to-skill achieves approximately 90% token reduction compared to context dumping by converting full-text books into compact, structured skill descriptors through deterministic extraction, semantic summarization, and strict token budgeting.
The open-source repository virgiliojr94/book-to-skill eliminates the inefficiency of context dumping—serializing entire documents often exceeding 100,000 tokens—by transforming source material into concise skill representations. Unlike naive approaches that overwhelm language models with raw prose, this tool extracts only essential metadata and curated excerpts, enabling accurate question answering within standard token limits while significantly reducing API costs.
Deterministic Extraction Pipeline
The foundation of token reduction lies in the deterministic parsing architecture implemented in scripts/extract.py. This module processes source documents—including PDF, EPUB, and HTML formats—and generates clean, hierarchical representations containing chapters, sections, headings, and sanitized excerpts.
By structuring the content before serialization, the extractor eliminates formatting noise, navigation elements, and redundant markup that would otherwise consume tokens. The pipeline preserves semantic hierarchy while discarding presentation-layer artifacts, ensuring that only information-dense content proceeds to the summarization stage.
Semantic Summarization and Skill Generation
The extraction pipeline feeds into a spec-driven generator defined in SKILL.md, which emits strictly essential metadata: title, author, table-of-contents, brief synopsis, and highlighted excerpts. This architectural choice eliminates the bulk of raw prose while retaining the information necessary for accurate model responses.
The skill template operates on the principle of lossy compression with semantic preservation. Rather than maintaining full paragraphs, it captures conceptual boundaries and key passages, creating a compressed representation that typically occupies under 2,000 tokens compared to the 50,000+ tokens required for complete texts.
Token-Aware Output Formatting
Three components collaborate to enforce strict token budgets and prevent context-window overflow:
Configuration Management (book_to_skill/config.py)
This module defines the default token constraints through parameters including MAX_TOKENS and DEFAULT_CHUNK_SIZE, establishing hard ceilings that prevent skill generation from exceeding model limits.
CLI Coordination (book_to_skill/cli.py)
The command-line interface orchestrates the extraction workflow while applying token budgets specified via the --max-tokens flag. When content exceeds the configured threshold, the CLI automatically trims or merges sections to maintain compliance.
Token Usage Verification (tests/test_tool_call_token_vs_prose.py)
The test suite explicitly asserts that generated skills consume approximately one-tenth of the tokens required for raw prose transmission. This quantitative validation ensures that the compression ratio remains consistent across document types and lengths.
Practical Implementation
Extract a source document into a skill descriptor with strict token enforcement:
python -m book_to_skill.cli extract path/to/my_book.pdf \
--output my_book.skill.md \
--max-tokens 2048
Integrate the generated skill with a language model API:
import openai
import pathlib
skill = pathlib.Path("my_book.skill.md").read_text()
response = openai.ChatCompletion.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You are an expert on the provided skill."},
{"role": "user", "content": f"Answer using only the skill information:\n\n{skill}"}
]
)
print(response.choices[0].message.content)
The resulting skill.md file contains only distilled metadata and curated excerpts, typically under 2 kilobytes, enabling efficient model inference without the cost and limit constraints of transmitting complete books.
Summary
- Deterministic extraction in
scripts/extract.pyeliminates formatting overhead and navigation markup before tokenization. - Semantic compression via the
SKILL.mdtemplate reduces content to essential metadata and key excerpts, achieving roughly 90% token reduction. - Token budgets defined in
book_to_skill/config.pyand enforced bybook_to_skill/cli.pyguarantee compliance with model context windows. - Quantitative verification in
tests/test_tool_call_token_vs_prose.pyvalidates the ≈1/10 token ratio compared to raw text dumping.
Frequently Asked Questions
What is context dumping and why does it waste tokens?
Context dumping refers to the practice of serializing entire documents—including formatting characters, headers, footers, and redundant structural elements—and transmitting them to language models as raw context. This approach wastes tokens on non-semantic content and often exceeds provider limits, forcing expensive truncation or model switching while failing to prioritize information density.
How significant is the token reduction when using book-to-skill?
According to the source code analysis and validation tests in tests/test_tool_call_token_vs_prose.py, book-to-skill reduces token usage by approximately 90%, compressing documents that typically exceed 50,000 tokens into skill descriptors under 2,000 tokens. This ratio remains consistent across PDF, EPUB, and HTML source formats.
Which document formats does the extraction pipeline support?
The scripts/extract.py module supports multiple source formats including PDF, EPUB, and HTML. The pipeline normalizes these heterogeneous formats into a unified hierarchical representation before applying semantic summarization, ensuring consistent token efficiency regardless of the original document structure.
How does the tool prevent skill generation from exceeding token limits?
The system implements layered safeguards: book_to_skill/config.py defines default maximums via MAX_TOKENS, while book_to_skill/cli.py accepts runtime overrides through the --max-tokens parameter. If processed content exceeds the budget, the CLI automatically trims or merges sections, ensuring the final skill descriptor never exceeds the specified threshold.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →