Skip to content

fix(provider/anthropic): parse context_management from streaming response root level#12154

Merged
dancer merged 4 commits intomainfrom
josh/fix-anthropic-context-management-streaming
Jan 30, 2026
Merged

fix(provider/anthropic): parse context_management from streaming response root level#12154
dancer merged 4 commits intomainfrom
josh/fix-anthropic-context-management-streaming

Conversation

@dancer
Copy link
Copy Markdown
Collaborator

@dancer dancer commented Jan 30, 2026

background

the contextManagement field in providerMetadata was always null when using streamText with the Anthropic provider, even though the API correctly returns context_management in the response.

the issue was twofold:

  1. the zod schema expected context_management inside the delta object
  2. the code read from value.delta.context_management

but Anthropic's API returns context_management at the root level of the message_delta event (sibling to delta and usage):

{
  "type": "message_delta",
  "delta": {"stop_reason": "end_turn"},
  "usage": {...},
  "context_management": {"applied_edits": [...]}
}

see Anthropic's context management docs

summary

  • moved context_management in the streaming schema from inside delta to the root level of message_delta
  • updated code to read from value.context_management instead of value.delta.context_management
  • added unit test for streaming context_management parsing

verification

before fix
contextManagement: null
after fix
contextManagement: {
  appliedEdits: [
    {
      type: 'clear_tool_uses_20250919',
      clearedToolUses: 6,
      clearedInputTokens: 149
    }
  ]
}

checklist

  • tests have been added / updated (for bug fixes / features)
  • documentation has been added / updated (for bug fixes / features)
  • a patch changeset for relevant packages has been added (run pnpm changeset in root)
  • i have reviewed this pull request (self-review)

related issues

fixes #12148

@vercel-ai-sdk vercel-ai-sdk bot added ai/provider related to a provider package. Must be assigned together with at least one `provider/*` label bug Something isn't working as documented provider/anthropic Issues related to the @ai-sdk/anthropic provider labels Jan 30, 2026
@dancer dancer merged commit 445cbe3 into main Jan 30, 2026
29 checks passed
@dancer dancer deleted the josh/fix-anthropic-context-management-streaming branch January 30, 2026 19:38
@apeatling
Copy link
Copy Markdown

Thanks for the quick fix!

felixarntz added a commit that referenced this pull request Feb 20, 2026
…ntext editing (#12547)

## Background

Anthropic context management support (`clear_tool_uses`,
`clear_thinking`) was added in #10540 (with a related fix in #12154),
and compaction support (`compact`) was added in #12384.

## Summary

With the complexities of handling these features correctly (including
how `clear_tool_uses` and `clear_thinking` can be combined), I think it
makes sense to add some more tests and also examples around this.

- Adds fixture based tests for context editing (tool uses, thinking, and
both combined), each for `generateText` and `streamText`
- Adds examples for thinking, compaction, and thinking and tool uses
combined (there already was an example for tool uses alone, which I
renamed for consistency)
- Expands docs, adding "Context Editing" grouping section for both
`clear_thinking` and `clear_tool_uses`

## Manual Verification

All examples were run against the live Anthropic API to verify correct
behavior, and the fixture data was captured from those real API
responses.

## Checklist

- [x] Tests have been added / updated (for bug fixes / features)
- [x] Documentation has been added / updated (for bug fixes / features)
- [ ] A _patch_ changeset for relevant packages has been added (for bug
fixes / features - run `pnpm changeset` in the project root)
- [x] I have reviewed this pull request (self-review)

## Future Work

N/A

## Related Issues

N/A
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai/provider related to a provider package. Must be assigned together with at least one `provider/*` label bug Something isn't working as documented provider/anthropic Issues related to the @ai-sdk/anthropic provider

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Anthropic context_management not working in streaming - wrong field location

3 participants