Skip to content

feat(sdk): add Anthropic SDK integration#61

Open
Uchebuzz wants to merge 3 commits into
secureagentics:mainfrom
Uchebuzz:feat/anthropic-sdk-integration
Open

feat(sdk): add Anthropic SDK integration#61
Uchebuzz wants to merge 3 commits into
secureagentics:mainfrom
Uchebuzz:feat/anthropic-sdk-integration

Conversation

@Uchebuzz

Copy link
Copy Markdown

Summary

Adds first-class Anthropic SDK support to the Adrian Python SDK. Every
messages.create call on both anthropic.Anthropic and
anthropic.AsyncAnthropic is now captured automatically as a
PairedEvent and emitted through the hook registry — no changes needed
to user agent code beyond adrian.init().

Changes

  • sdk/adrian/anthropic_handler.py (new): message format conversion,
    tool call and usage extraction, PairedEvent assembly, idempotent
    monkey-patch of Messages.create / AsyncMessages.create, and
    anthropic_invocation() / anthropic_invocation_sync() context managers
    for grouping multi-turn calls under a single invocation ID.
  • sdk/tests/test_anthropic_handler.py (new): 61 tests covering all
    helper functions, emission, getter lifecycle, the no-package no-op path,
    and both context managers. All pass.
  • sdk/adrian/__init__.py: patch_anthropic(), anthropic_invocation,
    and anthropic_invocation_sync exported; _auto_instrument_anthropic()
    wired into init() alongside LangChain auto-instrumentation.
  • sdk/pyproject.toml: anthropic optional dependency group added;
    anthropic>=0.40.0 added to [dev] for the test suite.
  • examples/anthropic_quickstart.py (new): minimal demo showing single-call
    and multi-turn anthropic_invocation() usage.

Test plan

  • uv pip install -e ./sdk[dev,anthropic] then
    pytest sdk/tests/test_anthropic_handler.py -v — 61 tests pass
  • pytest sdk/tests/ — no regressions in existing suite
  • Run examples/anthropic_quickstart.py against a live backend and
    confirm events appear in the dashboard with correct model, messages,
    and usage fields
  • Confirm anthropic_invocation() groups multi-turn calls under a
    shared invocation_id in the dashboard

Checklist

  • CLA signed (see CLA.md)
  • Tests pass locally
  • Docs updated where needed
  • British English; no em-dashes; no marketing fluff

@max-c-sa

Copy link
Copy Markdown
Contributor

Thanks for the PR Uche! I'll leave to the engineers to work with you but huge thanks for taking on Anthropic integration and i'd like to invite you to the private contributors discord community channel. https://discord.gg/zcwYwfdAFz I am 'maxicorbs' DM on there and i'll add you

Patches anthropic.Anthropic and anthropic.AsyncAnthropic so every
messages.create call is captured as an Adrian PairedEvent and emitted
through the hook registry, with the same lifecycle guarantees as the
existing LangChain/LangGraph integration.

- sdk/python/adrian/anthropic_handler.py: core patch module
  - _flatten_content / _flatten_anthropic_messages: normalise Anthropic
    message params (strings, TypedDict blocks, SDK objects) to ChatMessage
  - _extract_anthropic_tool_calls: pull ToolUseBlock records from response
  - _extract_anthropic_usage: map input/output tokens to TokenUsage
  - build_anthropic_llm_pair: assemble PairedEvent from request + response
  - _emit_pair / _schedule_emit: async and sync emission paths
  - patch_anthropic: idempotent monkey-patch; reads hooks/config at call
    time so shutdown + re-init cycles are handled correctly
  - anthropic_invocation / anthropic_invocation_sync: context managers
    to group multi-turn calls under a single invocation_id

- sdk/python/adrian/__init__.py: wire auto-instrumentation
  - patch_anthropic() public function (manual opt-in path)
  - _auto_instrument_anthropic() called by init() when auto_instrument=True

- sdk/python/pyproject.toml: add anthropic optional-dep group and to dev

- sdk/python/tests/test_anthropic_handler.py: 61 unit tests covering all
  helper functions, PairedEvent assembly, emission, patching, and context
  managers; all passing locally

- examples/python/anthropic_quickstart.py: minimal two-turn async example

All new source files carry the LICENSE_HEADER.txt SPDX header per
CONTRIBUTING.md.
@Uchebuzz Uchebuzz force-pushed the feat/anthropic-sdk-integration branch from 4417956 to a379747 Compare June 25, 2026 19:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants