Anthropic API Integration
Relevant source files
The following files were used as context for generating this wiki page:
- src/validation/models/AnthropicApi.ts
- src/validation/models/AnthropicApi.test.ts
- src/config/Config.ts (Referenced via src/config/Config.test.ts)
- src/validation/validator.ts (Referenced via src/validation/validator.test.ts)
- src/validation/prompts/system-prompt.ts (Referenced via src/validation/models/AnthropicApi.ts)
- CLAUDE.md
The Anthropic API integration within TDD Guard provides a direct communication channel to Anthropic's language models to validate whether code changes adhere to Test-Driven Development (TDD) principles. It serves as one of the primary IModelClient implementations, enabling the system to analyze intercepted file operations, test results, and linting data to make "block" or "allow" decisions.
The integration is encapsulated in the AnthropicApi class, which utilizes the official @anthropic-ai/sdk. This module is responsible for constructing requests with specific system prompts, managing API credentials, and parsing the AI's structured JSON responses to determine TDD compliance.
Sources: CLAUDE.md:37-43, src/validation/models/AnthropicApi.ts:7-10
Architecture and Components
The integration follows an interface-driven design, ensuring that the validation logic remains decoupled from the specific AI provider.
The AnthropicApi Class
The AnthropicApi class implements the IModelClient interface. It is initialized with a Config object which provides the necessary API key and model version.
Communication Flow
When the validator component requires a decision, it invokes the ask method. The integration then formats a message for the Anthropic Messages API.
Implementation Details
Request Configuration
The integration enforces specific parameters to ensure consistent and high-quality TDD validation:
| Parameter | Source/Value | Description |
|---|---|---|
model | config.modelVersion | Defaults to claude-3-5-sonnet-20241022. |
system | SYSTEM_PROMPT | Contains the core TDD rules and JSON formatting instructions. |
max_tokens | 1024 | Limits the response length to ensure concise analysis. |
messages | [{ role: 'user', content: prompt }] | Passes the dynamic context (code diffs, test output). |
Sources: src/validation/models/AnthropicApi.ts:20-29, src/validation/models/AnthropicApi.test.ts:45-55, src/config/Config.test.ts:460-465
Response Processing
The API returns a MessageResponse object. The integration includes a private helper extractTextFromResponse to safely navigate the response structure. It specifically looks for the first content block and ensures it contains text. If the response is empty or lacks text content, the system throws an error which the validator catches to block the operation as a safety measure.
Sources: src/validation/models/AnthropicApi.ts:35-47, src/validation/validator.test.ts:153-158
Configuration Options
The behavior of the Anthropic integration is governed by environment variables and the Config class.
| Environment Variable | Description | Default Value |
|---|---|---|
TDD_GUARD_ANTHROPIC_API_KEY | The API key for Anthropic. | undefined |
TDD_GUARD_MODEL_VERSION | Specific Claude model to use. | claude-3-5-sonnet-20241022 |
VALIDATION_CLIENT | Set to api to use this integration. | cli |
Sources: src/config/Config.test.ts:200-210, src/config/Config.test.ts:285-300, src/config/Config.test.ts:455-465
Error Handling and Validation
The integration is designed to fail-safe. If the API call fails or returns unexpected data, the validator interprets this as a TDD violation or a system error that requires a "block" decision to prevent unvalidated code from being committed.
// Example of response extraction logic
function extractTextFromResponse(response: MessageResponse): string {
if (response.content.length === 0) {
throw new Error('No content in response')
}
const firstContent = response.content[0]
if (!('text' in firstContent) || !firstContent.text) {
throw new Error('Response content does not contain text')
}
return firstContent.text
}
Sources: src/validation/models/AnthropicApi.ts:35-47, src/validation/validator.test.ts:145-155
Conclusion
The Anthropic API Integration is a critical component of TDD Guard's validation engine. By leveraging high-capability models like Claude 3.5 Sonnet and strict system prompts, it provides the "intelligence" required to interpret complex TDD scenarios, such as distinguishing between minimal implementation stubs and over-implementation.