Use anthropic SDK 0.109+ credentials= for long-running token refresh#43
Open
achandmsft wants to merge 1 commit into
Open
Use anthropic SDK 0.109+ credentials= for long-running token refresh#43achandmsft wants to merge 1 commit into
achandmsft wants to merge 1 commit into
Conversation
Drops the AnthropicIdentity(Anthropic) subclass that overrode the auth_token property to refresh the Entra ID token per request. The Anthropic Python SDK v0.98+ added a public credentials= constructor argument that takes an AccessTokenProvider callable. The SDK wraps it in a TokenCache that calls the provider lazily, caches the token until expiry, and on a 401 invalidates the cache and retries the request once with a fresh token. That covers the long-running use case without subclassing. - src/hello_claude_token_refresh.py: replace AnthropicIdentity with a tiny provider that wraps DefaultAzureCredential.get_token() into an anthropic.lib.credentials.AccessToken(token, expires_at). Pass it to Anthropic(credentials=..., base_url=...). - requirements.txt: bump anthropic>=0.104.1 to anthropic>=0.109.1. - README.md: update the SDK call shape long-running snippet to the new credentials= pattern (preserves the existing anchor id). Update the troubleshooting row for the 1-hour 401. - skills/claude-on-foundry/SKILL.md: mirror the troubleshooting and modify-playbook rows. Verified by constructing Anthropic(credentials=provider, base_url=...) against anthropic 0.109.1 and confirming TokenCache and AccessTokenAuth are wired and provider signature matches the AccessTokenProvider protocol (accepts force_refresh kwarg). Closes #42
pamelafox
approved these changes
Jun 11, 2026
pamelafox
left a comment
Collaborator
There was a problem hiding this comment.
LGTM, I will try in my samples
There was a problem hiding this comment.
Pull request overview
This PR updates the repo’s long-running Python sample to use the Anthropic Python SDK’s built-in credentials= support for automatic Entra ID token refresh, removing the now-redundant AnthropicIdentity subclass workaround and aligning the README + operational playbook documentation with the new pattern.
Changes:
- Replaced the
AnthropicIdentity(Anthropic)subclass shim insrc/hello_claude_token_refresh.pywith anAccessTokenProviderpassed viaAnthropic(credentials=...). - Bumped the minimum Anthropic SDK version to
anthropic>=0.109.1to ensurecredentials=andanthropic.lib.credentials.AccessTokenare available. - Updated README +
skills/claude-on-foundry/SKILL.mdguidance and troubleshooting to recommendcredentials=for long-running processes.
Reviewed changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/hello_claude_token_refresh.py | Removes the subclass shim and uses an AccessTokenProvider returning AccessToken(token, expires_at) from DefaultAzureCredential.get_token(). |
| requirements.txt | Updates Anthropic SDK minimum version to support the credentials= flow. |
| README.md | Updates the long-running token refresh snippet and troubleshooting guidance to the credentials= pattern. |
| skills/claude-on-foundry/SKILL.md | Mirrors the updated long-running auth guidance in DIAGNOSE/MODIFY tables. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #42.
Summary
Replaces the
AnthropicIdentity(Anthropic)subclass shim in src/hello_claude_token_refresh.py with the Anthropic Python SDK's built-incredentials=parameter (added in v0.98, May 2026).The SDK wraps the provided
AccessTokenProvidercallable in aTokenCachethat:_should_retryoverride insrc/anthropic/_client.py).That's exactly what the
AnthropicIdentityshim was emulating, so the subclass is now redundant.auth_tokenitself is still a staticstrupstream (anthropics/anthropic-sdk-python#1496 stays open), but the more generalcredentials=path solves the long-running use case cleanly.Files changed
AnthropicIdentityclass; build a tiny provider that wrapsDefaultAzureCredential.get_token()intoAccessToken(token=t.token, expires_at=t.expires_on)(expires_onis unix seconds, the format the SDK'sTokenCacheexpects). Pass it toAnthropic(credentials=provider, base_url=...). Provider accepts theforce_refreshkwarg the protocol defines.anthropic>=0.104.1→anthropic>=0.109.1.<details id="advanced-long-running-processes-auto-refreshing-the-entra-id-token">block to the newcredentials=pattern (anchor id preserved). Update the matching troubleshooting row.Not touched
src/hello_claude.py— the one-shot script keeps usingauth_token=token. It's still the simplest pattern when the process lives for a few seconds.src/check_claude_quota.py,src/hello_claude_apikey.py, the verifier scripts, and infra — none of them import the removed class.Verification
pip install --upgrade 'anthropic>=0.109.1'→ installed 0.109.1.AccessToken,AccessTokenProvider,TokenCachefromanthropic.lib.credentials→ all present.Anthropic(credentials=provider, base_url='https://example.services.ai.azure.com/anthropic')→client._token_cacheis aTokenCache,client._custom_authis anAccessTokenAuth,client.credentials is provider.force_refreshkwarg theAccessTokenProviderprotocol requires.No live API round-trip in this PR — that path is covered by
scripts/verify-claude-code.ps1 -RunPythonSampleon a real deployment.