#13831: fix(agents): include Anthropic in tool call ID sanitization
agents
stale
size: XS
trusted-contributor
Cluster:
Tool Call ID Sanitization
## Summary
Fixes #13799
## Problem
`sanitizeToolCallIds` in `transcript-policy.ts` only enables tool call ID sanitization for Google and Mistral providers, not Anthropic. This causes the Anthropic API to reject requests when `tool_use` IDs contain characters outside `^[a-zA-Z0-9-]+$` (e.g., underscores in `toolu_01XYZ` style IDs from other providers or synthetic results).
The error manifests as:
```
messages.370.content.1.tooluse.id: String should match pattern '^[a-zA-Z0-9-]+$'
```
**Root cause** in `src/agents/transcript-policy.ts`:
```typescript
const sanitizeToolCallIds = isGoogle || isMistral;
// Missing: isAnthropic and OpenRouter Claude models
```
## Fix
Add `isAnthropic` and `isOpenRouterAnthropic` to the `sanitizeToolCallIds` condition, and also to `repairToolUseResultPairing` for OpenRouter Claude models.
**Before:**
```typescript
const sanitizeToolCallIds = isGoogle || isMistral;
```
**After:**
```typescript
const sanitizeToolCallIds = isGoogle || isAnthropic || isOpenRouterAnthropic || isMistral;
```
Also added `isOpenRouterAnthropic` detection (same pattern as existing `isOpenRouterGemini`) for OpenRouter + Claude model IDs.
## Reproduction & Verification
### Before fix (main branch) — 4 tests fail:
```
pnpm vitest run src/agents/transcript-policy.test.ts
Tests 4 failed | 4 passed (8)
FAIL: enables tool call ID sanitization for Anthropic (strict mode)
FAIL: enables tool call ID sanitization for Anthropic via provider detection
FAIL: enables tool call ID sanitization for OpenRouter with Claude models
FAIL: enables tool call ID sanitization for OpenRouter with anthropic in model ID
```
### After fix — All 24 tests pass:
```
pnpm vitest run src/agents/transcript-policy.test.ts src/agents/pi-embedded-runner.sanitize-session-history.test.ts src/agents/tool-call-id.test.ts
Test Files 3 passed (3)
Tests 24 passed (24)
```
## Effect on User Experience
**Before fix:**
Anthropic API rejects requests with tool calls containing non-alphanumeric IDs, causing persistent `tool_use.id` format errors. The gateway logs "Tool calls will be sanitized on retry" but no sanitization actually runs for Anthropic.
**After fix:**
Tool call IDs are sanitized to `^[a-zA-Z0-9]+$` (strict mode) before sending to Anthropic, preventing format rejection errors. This applies to direct Anthropic connections and OpenRouter/OpenCode with Claude models.
## Testing
- ✅ 24 tests pass (pnpm vitest run src/agents/transcript-policy.test.ts src/agents/pi-embedded-runner.sanitize-session-history.test.ts src/agents/tool-call-id.test.ts)
- ✅ Lint passes
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR updates `resolveTranscriptPolicy` to enable tool-call ID sanitization for Anthropic models and for OpenRouter/OpenCode routes that serve Anthropic/Claude models, preventing Anthropic-side `tool_use.id` pattern rejections. It also expands transcript repair (`repairToolUseResultPairing`) to run for OpenRouter Claude models and adjusts the `sanitizeSessionHistory` test to assert sanitization is enabled for Anthropic APIs. A changelog entry documents the fix.
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk.
- Changes are narrowly scoped to provider/model detection in transcript sanitization policy and align with existing sanitization/repair mechanisms. No behavior changes for OpenAI paths, and the updated unit test matches the intended new policy.
- No files require special attention
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#13976: fix(anthropic): include Anthropic in tool call ID sanitization
by omair445 · 2026-02-11
90.4%
#12812: fix(transcript-policy): sanitize tool call IDs for all non-OpenAI p...
by justin-nevins · 2026-02-09
89.1%
#4700: fix: deduplicate tool_use IDs and enable sanitization for Anthropic
by marcelomar21 · 2026-01-30
88.5%
#10232: Fix: Add anthropic mode for tool call ID sanitization
by StreetJammer · 2026-02-06
86.5%
#12608: fix: sanitize client tool call IDs per provider requirements
by piyushhhxyz · 2026-02-09
85.5%
#12487: fix(agents): strip orphaned tool_result when tool_use is sanitized ...
by skylarkoo7 · 2026-02-09
85.4%
#8345: fix: prevent synthetic error repair from creating tool_result for d...
by vishaltandale00 · 2026-02-03
85.0%
#2806: [AI-Assisted] Fix: Repair tool_use/tool_result pairing for Claude o...
by Arthur742Ramos · 2026-01-27
84.8%
#4009: fix(agent): sanitize messages after orphan user repair
by drag88 · 2026-01-29
84.7%
#8270: fix: support snake_case 'tool_use' in transcript repair (#8264)
by heliosarchitect · 2026-02-03
84.7%