Interface IPromptAnalyzer
- Namespace
- AiDotNet.Interfaces
- Assembly
- AiDotNet.dll
Defines the contract for analyzing prompts before sending them to language models.
public interface IPromptAnalyzer
Remarks
A prompt analyzer computes metrics and validates prompts to help developers understand and optimize their prompts before incurring API costs. Analysis includes token counting, cost estimation, complexity measurement, and issue detection.
For Beginners: A prompt analyzer is like a spell-checker and cost calculator for your prompts.
Before sending a prompt to an AI model (which costs money), the analyzer tells you:
- How many tokens it uses (tokens = cost)
- Estimated API cost in dollars
- How complex the prompt is
- Any potential problems (missing variables, too long, etc.)
Example workflow:
var analyzer = new TokenCountAnalyzer();
var metrics = analyzer.Analyze("Translate this text...");
Console.WriteLine($"Tokens: {metrics.TokenCount}");
Console.WriteLine($"Cost: ${metrics.EstimatedCost}");
if (metrics.TokenCount > 4000)
{
Console.WriteLine("Warning: Prompt is very long, consider compression");
}
Benefits:
- Cost control: Know costs before making API calls
- Optimization: Find prompts that are too long or complex
- Debugging: Catch issues before they cause errors
- Budgeting: Track and forecast API spending
Properties
Name
Gets the name of this analyzer implementation.
string Name { get; }
Property Value
Remarks
A human-readable identifier for this analyzer, useful for logging and debugging which analyzer is being used.
For Beginners: The name of this specific analyzer. Examples: "TokenCountAnalyzer", "GPT4CostEstimator", "ClaudeAnalyzer"
Methods
Analyze(string)
Analyzes a prompt and returns detailed metrics.
PromptMetrics Analyze(string prompt)
Parameters
promptstringThe prompt string to analyze.
Returns
- PromptMetrics
A PromptMetrics object containing analysis results.
Remarks
Performs comprehensive analysis of the prompt including token counting, cost estimation, complexity scoring, and pattern detection.
For Beginners: This examines your prompt and tells you everything about it.
Example:
var metrics = analyzer.Analyze("Summarize this article about climate change...");
// metrics.TokenCount: 150 (how many tokens)
// metrics.EstimatedCost: $0.003 (cost at current rates)
// metrics.ComplexityScore: 0.4 (0-1, higher = more complex)
// metrics.VariableCount: 0 (number of {variables})
// metrics.DetectedPatterns: ["summarization", "instruction"]
AnalyzeAsync(string, CancellationToken)
Analyzes a prompt asynchronously for use in async workflows.
Task<PromptMetrics> AnalyzeAsync(string prompt, CancellationToken cancellationToken = default)
Parameters
promptstringThe prompt string to analyze.
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
- Task<PromptMetrics>
A task that resolves to PromptMetrics.
Remarks
Async version of Analyze for non-blocking analysis, useful when analyzing many prompts or when analysis involves external calls (e.g., to a tokenizer API).
For Beginners: Same as Analyze, but doesn't block your program. Use this when analyzing many prompts at once or in web applications.
ValidatePrompt(string, ValidationOptions?)
Validates a prompt for potential issues.
IEnumerable<PromptIssue> ValidatePrompt(string prompt, ValidationOptions? options = null)
Parameters
promptstringThe prompt string to validate.
optionsValidationOptionsOptions controlling validation behavior.
Returns
- IEnumerable<PromptIssue>
A collection of detected issues, empty if no issues found.
Remarks
Checks the prompt for common problems such as missing placeholders, excessive length, potential injection vulnerabilities, and format issues.
For Beginners: This looks for problems in your prompt.
Example issues it might find:
- "Prompt exceeds maximum length for GPT-4 (8192 tokens)"
- "Variable {user_input} contains potential prompt injection"
- "Missing closing brace for variable {name"
- "Prompt is empty or whitespace-only"
Use this before sending prompts to catch problems early:
var issues = analyzer.ValidatePrompt(prompt, ValidationOptions.Strict);
if (issues.Any())
{
foreach (var issue in issues)
{
Console.WriteLine($"[{issue.Severity}] {issue.Message}");
}
}