Code Compression - flight505/ContextCraft GitHub Wiki
Code Compression
Code compression is one of ContextCraft's most powerful features for optimizing token usage. This page explains how code compression works, how to configure it, and best practices for using it effectively.
What is Code Compression?
Code compression is a technique that reduces token usage by replacing function and method bodies with placeholder comments while preserving signatures, types, and important structural elements. This allows you to include more files within AI model context limits.
How Code Compression Works
When enabled, ContextCraft analyzes your code and:
- Identifies function bodies, method implementations, and class bodies
- Replaces the implementation details with a brief comment (
/* ... */
) - Preserves function signatures, parameters, return types, and exports
- Maintains the overall structure while reducing token usage
Example
Before compression:
function calculateTotal(items) {
let sum = 0;
for (let i = 0; i < items.length; i++) {
sum += items[i].price * items[i].quantity;
if (items[i].taxable) {
sum += items[i].price * items[i].quantity * 0.08;
}
}
return sum.toFixed(2);
}
After compression:
function calculateTotal(items) {
/* Implementation compressed to save tokens */
}
Enabling and Configuring Compression
Basic Compression
To enable basic code compression:
- Navigate to the Control Container panel on the right side of the application
- Find the "Compression" toggle switch
- Click the toggle to enable compression
When compression is enabled, all selected files will be processed and compressed before being included in the output.
Advanced Compression Options
Depending on your version of ContextCraft, you may have access to these advanced compression options:
-
Minimum Token Threshold:
- Set a minimum size threshold for compression
- Functions smaller than this threshold won't be compressed
- Useful to keep small, simple functions intact
-
Keep Docstrings:
- When enabled, documentation comments (docstrings) are preserved
- This retains important context about function purpose and parameters
- Recommended for better AI understanding of code
-
Remove Empty Lines:
- Further reduces token usage by removing blank lines
- Maintains code structure but creates more compact output
-
Never Compress Patterns:
- Specify file patterns that should never be compressed
- Useful for key files where implementation details are important
- Example pattern:
**/auth/*.ts
to never compress authentication code
Supported Programming Languages
Code compression works with many popular programming languages, including:
- JavaScript/TypeScript
- Python
- Java
- C#
- Ruby
- Go
- PHP
The effectiveness of compression varies by language based on syntax recognition capabilities.
Benefits and Trade-offs
Benefits
-
Significant Token Reduction:
- Can reduce token usage by 30-80% depending on your codebase
- Allows including more files within the same token budget
- Enables providing broader context to AI models
-
Structural Preservation:
- Maintains API signatures and type information
- Preserves the overall architecture of your code
- Keeps imports, exports, and class relationships intact
-
Focus on Design, Not Implementation:
- Emphasizes the interfaces and structure over implementation details
- Often leads to better high-level architectural advice from AI
Trade-offs
-
Loss of Implementation Details:
- Implementation logic is not available to the AI
- The AI can't analyze or suggest improvements to function internals
- Bug fixes for implementation-specific issues may be less helpful
-
Language Limitations:
- Some language features or unusual patterns might not be properly recognized
- Experimental or uncommon syntax might not compress correctly
-
Potential Context Loss:
- In-line comments explaining complex logic are removed
- Implementation-specific context is lost
Best Practices
-
Be Strategic:
- Enable compression when you need a broad structural overview
- Disable compression when you need specific implementation feedback
- Consider including uncompressed versions of key files
-
Combine with Comment Removal:
- Use with comment removal (keeping docstrings) for maximum token efficiency
- This preserves function purpose documentation while removing implementation
-
Evaluate the Output:
- Review the compressed output to ensure it preserves necessary information
- Adjust settings if important details are being lost
-
Target Questions Appropriately:
- With compression: Ask about architecture, APIs, interfaces, and patterns
- Without compression: Ask about specific implementations, algorithms, and logic
Example Use Cases
Good for Compression
- "Give me an overview of how these components interact"
- "What's the architecture of this application?"
- "How does this API work?"
- "What is the data model structure?"
Less Suitable for Compression
- "Why is this specific function throwing an error?"
- "How can I optimize this algorithm?"
- "What's wrong with my implementation of this feature?"
Practical Example
Consider a React application with many components. Using compression, you could include all components to give the AI a complete picture of your component hierarchy, even with a limited context window. The AI would see all component props, types, and structure, but not the detailed implementations.
Related Topics: