Skip to main content

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",          # tag the logged turn (per-user analytics)
        "search_user_id": "u_123",   # OPTIONAL: scope memory retrieval to this user
        "session_id": "sess_abc",
        "signals": {"thumbs_down": True},
        "metadata": {"latency_budget_ms": 500},
        "skip": False,
        "search": False,  # or {"limit": 5, "tags": ["pix"]}
    },
)

What user_id does (changed in v0.2.2)

user_id tags the logged turn for per-user dashboard filtering. It does not filter memory retrieval — search returns the full org-wide memory corpus by default, regardless of which user this call is for. That's almost always what you want: any correction your team has ever made should be available to inform the next response.

If you have a specific reason to want per-user retrieval (e.g. a personal-assistant agent where each end-user has their own preference history), set search_user_id explicitly:

agentloop={
    "user_id": "u_123",          # tag the log
    "search_user_id": "u_123",   # opt-in: scope retrieval too
}

Migration note: Prior to v0.2.2, the wrapper silently forwarded user_id to search as well, which suppressed retrieval of org-wide corrections. The fix is non-breaking for the default case. If you previously relied on per-user retrieval, set search_user_id to preserve that behavior.

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

Metadata

Release files for agentloop-py-anthropic 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for agentloop-py-anthropic 0.3.0
File Size Uploaded Provenance
agentloop_py_anthropic-0.3.0.tar.gz 13.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for agentloop-py-anthropic 0.3.0
File Interpreter ABI Platform
agentloop_py_anthropic-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 26.9 kB

Release files / agentloop_py_anthropic-0.3.0.tar.gz

Download URL agentloop_py_anthropic-0.3.0.tar.gz
Size 13.8 kB
Tags Source
SHA-256 checksum
How to use checksums
452325e0a8c46908b56019226aae0bafae36f562a6c0429b36a8a77f2348bf09
BLAKE2b-256 checksum
How to use checksums
4ebef96a4c36275c9cb05282f3b0eb15138dab54f5f466df8fc6b6da8c1f3212
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.7

Release files / agentloop_py_anthropic-0.3.0-py3-none-any.whl

Download URL agentloop_py_anthropic-0.3.0-py3-none-any.whl
Size 13.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
51b7c4657662c2fea63832febf8ead013f28ac86c0b3b54dd65944588c0efbb3
BLAKE2b-256 checksum
How to use checksums
35c26a5c2ce7dc31b94bca0ec3ea9102ba15b1d0acd39eacb804be08185743a7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.7

Security for agentloop-py-anthropic 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page