Table of Contents

Interface IPromptCompressor

Namespace
AiDotNet.Interfaces
Assembly
AiDotNet.dll

Defines the contract for compressing prompts to reduce token counts and API costs.

public interface IPromptCompressor

Remarks

A prompt compressor reduces the length of prompts while preserving their semantic meaning. This is valuable for reducing API costs, fitting within context windows, and optimizing performance. Different compression strategies include redundancy removal, summarization, and caching-based approaches.

For Beginners: A prompt compressor makes your prompts shorter without losing meaning.

Why compress prompts?

  • Save money: Shorter prompts = fewer tokens = lower costs
  • Fit limits: Some models have maximum token limits
  • Faster: Shorter prompts process faster

Example:

var compressor = new RedundancyCompressor();

string longPrompt = @"
    Please analyze the following document and provide a summary.
    The document that you need to analyze is provided below.
    When you analyze the document, focus on the main points.
    Here is the document to analyze: [document text]";

var result = compressor.CompressWithMetrics(longPrompt, new CompressionOptions { TargetReduction = 0.3 });

Console.WriteLine($"Original: {result.OriginalTokenCount} tokens");
Console.WriteLine($"Compressed: {result.CompressedTokenCount} tokens");
Console.WriteLine($"Savings: {result.CompressionRatio:P0}");

// Output:
// Original: 80 tokens
// Compressed: 45 tokens
// Savings: 44%

The compressed version might be: "Analyze this document and summarize the main points: [document text]"

Properties

Name

Gets the name of this compressor implementation.

string Name { get; }

Property Value

string

Remarks

A human-readable identifier for this compressor, useful for logging and debugging which compressor is being used.

For Beginners: The name of this specific compressor. Examples: "RedundancyCompressor", "SummarizationCompressor", "CachingCompressor"

Methods

Compress(string, CompressionOptions?)

Compresses a prompt to reduce its token count.

string Compress(string prompt, CompressionOptions? options = null)

Parameters

prompt string

The prompt string to compress.

options CompressionOptions

Options controlling compression behavior.

Returns

string

The compressed prompt string.

Remarks

Applies compression techniques to reduce the prompt's token count while preserving its semantic meaning and effectiveness for the target task.

For Beginners: This takes your prompt and makes it shorter.

Example:

var compressed = compressor.Compress(
    "Please kindly help me to translate the following text from English to Spanish",
    new CompressionOptions());

// Result: "Translate from English to Spanish"

The compressor removes:

  • Redundant words ("please kindly help me to")
  • Unnecessary phrases ("the following")
  • Verbose constructions

CompressAsync(string, CompressionOptions?, CancellationToken)

Compresses a prompt asynchronously.

Task<string> CompressAsync(string prompt, CompressionOptions? options = null, CancellationToken cancellationToken = default)

Parameters

prompt string

The prompt string to compress.

options CompressionOptions

Options controlling compression behavior.

cancellationToken CancellationToken

Token to cancel the operation.

Returns

Task<string>

A task that resolves to the compressed prompt string.

Remarks

Async version of Compress for non-blocking compression, particularly useful for summarization-based compression that may call external LLM APIs.

For Beginners: Same as Compress, but doesn't block your program. Important for compressors that use AI to summarize (which takes time).

CompressWithMetrics(string, CompressionOptions?)

Compresses a prompt and returns detailed metrics about the compression.

CompressionResult CompressWithMetrics(string prompt, CompressionOptions? options = null)

Parameters

prompt string

The prompt string to compress.

options CompressionOptions

Options controlling compression behavior.

Returns

CompressionResult

A CompressionResult containing the compressed prompt and metrics.

Remarks

In addition to compressing the prompt, this method returns detailed metrics including original and compressed token counts, compression ratio, and information about what was changed.

For Beginners: Like Compress, but also tells you what changed.

Example:

var result = compressor.CompressWithMetrics(longPrompt, options);

Console.WriteLine($"Original tokens: {result.OriginalTokenCount}");
Console.WriteLine($"Compressed tokens: {result.CompressedTokenCount}");
Console.WriteLine($"Tokens saved: {result.TokensSaved}");
Console.WriteLine($"Compression ratio: {result.CompressionRatio:P0}");
Console.WriteLine($"Estimated savings: ${result.EstimatedCostSavings}");

This is useful for:

  • Tracking cost savings
  • Debugging compression issues
  • Reporting to stakeholders