Add
ANTHROPIC_API_KEY or OPENAI_API_KEY to your environment variables before using these helpers. askClaudeTextValidation and askChatGPTTextValidation share the same signature and return shape, and can be swapped with no other changes to your test.Examples
Assert that an AI response is valid and within the token budgetWhen to use
- Your app surfaces AI-generated content (summaries, prep notes, chat responses) that must be checked for accuracy.
- Your app’s AI feature must not introduce contradictions or hallucinations relative to source material.
- Your app has a quality bar for AI output that a simple string match cannot enforce.
- Your app sends AI prompts that could drift in cost and you need to assert token budgets.
- Your app uses different AI providers and you want a consistent validation interface across both.
Helpers
askClaudeTextValidation
Sends the original prompt and candidate response to Claude and returns a structured verdict.
Conforms to the Anthropic Messages API.
askChatGPTTextValidation
Same signature and return shape as askClaudeTextValidation. Uses the OpenAI Responses API with structured JSON output.
Conforms to the OpenAI Responses API.
NPM needed: openai@latest
Return shape
Both helpers return{ validation, usage } with the same structure.
| Field | Type | Description |
|---|---|---|
validation.isValidForPrompt | boolean | Hard gate — did the response satisfy the prompt? |
validation.score | number | Quality score 0–1. Assert >= 0.8 as a starting threshold. |
validation.issues.* | string[] | Arrays of flagged issues by category. |
validation.explanation | string | Step-by-step reasoning from the judge model. |
usage.input_tokens | number | Tokens consumed by the prompt. |
usage.output_tokens | number | Tokens consumed by the response. |