Evaluate Code Generation with Claude
Promptfoo example that authenticates Anthropic Messages API evals via a local Claude Code OAuth session, not a separate API key.
Why it matters
Streamline your code generation and review process by leveraging Promptfoo to evaluate Anthropic's Claude API. This asset enables automated testing and quality assurance for your code generation tasks.
Outcomes
What it gets done
Run Promptfoo evaluations against the Anthropic Messages API.
Utilize model-graded assertions for code quality assessment.
Reuse existing local Claude Code sessions for API interactions.
Automate code generation and review workflows.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/pfoo-claude-code-session | bash Steps
Steps in the chain
Overview
Claude Code Session
A Promptfoo example that authenticates Anthropic Messages API evals through a local Claude Code OAuth session instead of a separate API key, including model-graded llm-rubric assertions. Use when you're a Claude Pro/Max subscriber already running the claude CLI locally and want to eval prompts against Anthropic without separate API billing.
What it does
Runs Promptfoo evals against the Anthropic Messages API - including llm-rubric model-graded assertions - by reusing an existing local Claude Code session instead of creating a separate Anthropic Console API key. Setting apiKeyRequired: false in the provider config tells Promptfoo to skip its upfront API key check, load the Claude Code OAuth credential from the local session, authenticate Messages API requests with a Bearer token plus the claude-code-20250219,oauth-2025-04-20 beta headers, and prepend the required Claude Code identity system block ("You are Claude Code, Anthropic's official CLI for Claude.") before your own system prompt. Both the main provider and the llm-rubric grader point at anthropic:messages:claude-sonnet-4-6 with apiKeyRequired: false, so the entire eval runs on your Claude subscription rather than a separate API key.
When to use - and when NOT to
This is meant for Claude Pro or Max subscribers who already use the claude CLI on the same machine and want to run evals without provisioning a separate Anthropic Console API key. Requests made this way are expected to count against your Claude subscription the same way calls from the claude CLI do - check Anthropic's documentation for current billing behavior before assuming it's free or unmetered. If ANTHROPIC_API_KEY is set in your environment, Promptfoo will prefer it over the OAuth credential, so unset it first if you specifically want the eval to run on your Claude Code session instead of a separate key.
Inputs and outputs
npx promptfoo@latest init --example anthropic/claude-code-session
cd anthropic/claude-code-session
Prerequisite: install and log in to Claude Code with claude /login. Promptfoo reads the OAuth credential Claude Code stores in either the macOS keychain (Claude Code-credentials) or $HOME/.claude/.credentials.json. Run the eval with promptfoo eval and inspect results with promptfoo view. Configuration lives in promptfooconfig.yaml.
Integrations
Integrates with the Anthropic Messages API via the anthropic:messages:claude-sonnet-4-6 provider string, authenticated through the local Claude Code CLI's OAuth session rather than a standalone API key, and reuses the same provider for its llm-rubric model-graded grader. Current Claude Code billing behavior for these requests is documented at docs.claude.com/en/docs/claude-code/overview.
Who it's for
Claude Pro or Max subscribers already running the claude CLI locally who want to evaluate prompts against the Anthropic Messages API - authenticated over the same Bearer-token OAuth flow the CLI itself uses - with model-graded rubric assertions and without setting up separate Console API billing.
Source README
anthropic/claude-code-session (Authenticate via Claude Code session)
This example shows how to run Promptfoo evals against the Anthropic Messages
API - including llm-rubric model-graded assertions - by reusing an existing
local Claude Code session instead of creating a separate Anthropic Console API
key.
It's meant for Claude Pro / Max subscribers who already use theclaude CLI on the
same machine.
You can run this example with:
npx promptfoo@latest init --example anthropic/claude-code-session
cd anthropic/claude-code-session
Prerequisites
Install and log in to Claude Code:
claude /loginPromptfoo reads the OAuth credential that Claude Code stores in either the
macOS keychain (Claude Code-credentials) or$HOME/.claude/.credentials.json.Make sure
ANTHROPIC_API_KEYis unset if you specifically want
Promptfoo to use your Claude Code session. If the env var is set, Promptfoo
will prefer it over the OAuth credential.
How it works
The provider config sets apiKeyRequired: false, which tells Promptfoo to:
- Skip its upfront API key check.
- Load the Claude Code OAuth credential from the local session.
- Authenticate Messages API requests with a Bearer token plus the
claude-code-20250219,oauth-2025-04-20beta headers. - Prepend the required Claude Code identity system block
("You are Claude Code, Anthropic's official CLI for Claude.") before your
own system prompt.
Requests made this way are expected to count against your Claude
subscription the same way calls from the claude CLI do. Check
Anthropic's documentation
for current billing behavior.
Run it
promptfoo eval
promptfoo view
Configuration
See promptfooconfig.yaml - both the main provider and the llm-rubric
grader point at anthropic:messages:claude-sonnet-4-6 withapiKeyRequired: false so the entire eval runs on your Claude subscription.
FAQ
Common questions
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.