Skip to content

Add anthropic AI Usage #404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Zen for Python 3 is compatible with:

### AI SDKs
* ✅ [`openai`](https://pypi.org/project/openai) ^1.0
* ✅ [`anthropic`](https://pypi.org/project/anthropic/)

## Reporting to your Aikido Security dashboard

Expand Down
1 change: 1 addition & 0 deletions aikido_zen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,6 @@ def protect(mode="daemon"):

# Import AI sinks
import aikido_zen.sinks.openai
import aikido_zen.sinks.anthropic

logger.info("Zen by Aikido v%s starting.", PKG_VERSION)
21 changes: 21 additions & 0 deletions aikido_zen/sinks/anthropic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from aikido_zen.helpers.on_ai_call import on_ai_call
from aikido_zen.helpers.register_call import register_call
from aikido_zen.sinks import on_import, patch_function, after


@after
def _messages_create(func, instance, args, kwargs, return_value):
op = f"anthropic.resources.messages.messages.Messages.create"
register_call(op, "ai_op")

Check warning on line 9 in aikido_zen/sinks/anthropic.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sinks/anthropic.py#L8-L9

Added lines #L8 - L9 were not covered by tests

on_ai_call(

Check warning on line 11 in aikido_zen/sinks/anthropic.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sinks/anthropic.py#L11

Added line #L11 was not covered by tests
provider="anthropic",
model=return_value.model,
input_tokens=return_value.usage.input_tokens,
output_tokens=return_value.usage.output_tokens,
)


@on_import("anthropic.resources.messages")
def patch(m):
patch_function(m, "messages.Messages.create", _messages_create)
46 changes: 46 additions & 0 deletions aikido_zen/sinks/tests/anthropic_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import os

import pytest
import aikido_zen.sinks.anthropic
import anthropic

from aikido_zen.thread.thread_cache import get_cache

skip_no_api_key = pytest.mark.skipif(
"ANTHROPIC_API_KEY" not in os.environ,
reason="ANTHROPIC_API_KEY environment variable not set",
)


@pytest.fixture(autouse=True)
def setup():
get_cache().reset()
yield
get_cache().reset()

Check warning on line 19 in aikido_zen/sinks/tests/anthropic_test.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sinks/tests/anthropic_test.py#L17-L19

Added lines #L17 - L19 were not covered by tests


def get_ai_stats():
return get_cache().ai_stats.get_stats()

Check warning on line 23 in aikido_zen/sinks/tests/anthropic_test.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sinks/tests/anthropic_test.py#L23

Added line #L23 was not covered by tests


@skip_no_api_key
def test_anthropic_messages_create():
client = anthropic.Anthropic()
response = client.messages.create(

Check warning on line 29 in aikido_zen/sinks/tests/anthropic_test.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sinks/tests/anthropic_test.py#L28-L29

Added lines #L28 - L29 were not covered by tests
model="claude-3-opus-20240229",
max_tokens=20,
messages=[
{
"role": "user",
"content": "Write the longest response possible, just as I am writing a long content",
}
],
)
print(response)

Check warning on line 39 in aikido_zen/sinks/tests/anthropic_test.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sinks/tests/anthropic_test.py#L39

Added line #L39 was not covered by tests

assert get_ai_stats()[0]["model"] == "claude-3-opus-20240229"
assert get_ai_stats()[0]["calls"] == 1
assert get_ai_stats()[0]["provider"] == "anthropic"
assert get_ai_stats()[0]["tokens"]["input"] == 21
assert get_ai_stats()[0]["tokens"]["output"] == 20
assert get_ai_stats()[0]["tokens"]["total"] == 41

Check warning on line 46 in aikido_zen/sinks/tests/anthropic_test.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sinks/tests/anthropic_test.py#L41-L46

Added lines #L41 - L46 were not covered by tests
27 changes: 26 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ pygments = "^2.18.0"
lxml = "^5.4.0"
clickhouse-driver = "^0.2.9"
openai = "^1.85.0"
anthropic = "^0.54.0"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down