Skip to main content

Drop-in Anthropic SDK wrapper for AgentLoop — adds memory retrieval and turn logging to messages.create calls. Supports streaming.

Project description

agentloop-py-anthropic

Drop-in wrapper that adds AgentLoop memory retrieval and turn logging to every anthropic.messages.create call.

from anthropic import Anthropic
from agentloop import AgentLoop
from agentloop_anthropic import wrap_anthropic

anthropic = wrap_anthropic(
    Anthropic(),
    loop=AgentLoop(api_key="ak_..."),
)

msg = anthropic.messages.create(
    model="claude-opus-4-7",
    max_tokens=1024,
    messages=[{"role": "user", "content": "What's the Pix limit at night?"}],
)

That's the whole integration.

What happens under the hood

For every messages.create call:

  1. Extracts the last user message as the query
  2. Calls loop.search(query) — pulls any relevant corrections
  3. Appends them to your system prompt (or creates one if absent)
  4. Calls Anthropic with the augmented system prompt
  5. Calls loop.log_turn(question, answer) with the assembled text

If either AgentLoop call fails, your Anthropic call still succeeds.

Install

pip install agentloop-py agentloop-py-anthropic anthropic

Per-call options

msg = anthropic.messages.create(
    model="claude-opus-4-7",
    max_tokens=1024,
    messages=[...],
    agentloop={
        "user_id": "u_123",
        "session_id": "sess_abc",
        "signals": {"thumbs_down": True},
        "metadata": {"latency_budget_ms": 500},
        "skip": False,
        "search": False,  # or {"limit": 5, "tags": ["pix"]}
    },
)

Configuration (passed at wrap time)

anthropic = wrap_anthropic(
    Anthropic(),
    loop=loop,

    # Custom memory injection. Default: append to system prompt.
    # Handles both string and array-of-text-blocks system forms.
    inject_memories=lambda memories, existing_system: ...,

    # Auto-detect signals from the response before log_turn.
    detect_signals=lambda question, answer, memories: {
        "agent_punted": "not sure" in answer.lower(),
    },

    search_limit=3,
    search_tags=["production"],
    only_log_when_signaled=False,
)

Low-level API

from agentloop_anthropic import ask_with_agentloop, PerCallOptions
from agentloop_anthropic._ask import WrapOptions

resp = ask_with_agentloop(
    anthropic,                        # raw, unwrapped Anthropic client
    messages=[{"role": "user", "content": question}],
    per_call=PerCallOptions(user_id="u_123"),
    config=WrapOptions(loop=loop),
    model="claude-opus-4-7",
    max_tokens=1024,
)

Not mutated

wrap_anthropic(client) returns a distinct wrapper. Your original client stays unwrapped and usable.

Streaming

Not supported in v0.1. Same note as agentloop-py-openai — planned for a later release.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

agentloop_py_anthropic-0.2.1.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

agentloop_py_anthropic-0.2.1-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file agentloop_py_anthropic-0.2.1.tar.gz.

File metadata

  • Download URL: agentloop_py_anthropic-0.2.1.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for agentloop_py_anthropic-0.2.1.tar.gz
Algorithm Hash digest
SHA256 eabcd1084fd54988428dcb281ea98f82837e0fd4b9a285e6d1fa61e0547cd116
MD5 022141652ed8c2539aeb8552c7921355
BLAKE2b-256 d2fa7d3337bc42339ddf47e9df0c104d22ca6dce01124190d1accc5b10b008f7

See more details on using hashes here.

File details

Details for the file agentloop_py_anthropic-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for agentloop_py_anthropic-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d67b19f3e9d751f6a12e148ec6befa4ec3ea12e0364d17e92b8f20820f9e6aac
MD5 6f0c60ed3e9a6aefc646072bdde15978
BLAKE2b-256 477862a9494cc4bf2513c3a2cefc3488e499958531a33cb730ee2912a5d8a5ef

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page