Skip to main content

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

Project description

agentloop-py-openai

Drop-in wrapper that adds AgentLoop memory retrieval and turn logging to every openai.chat.completions.create call.

from openai import OpenAI
from agentloop import AgentLoop
from agentloop_openai import wrap_openai

openai = wrap_openai(
    OpenAI(),
    loop=AgentLoop(api_key="ak_..."),
)

# Use exactly like the normal OpenAI SDK.
# Memory search fires before; log_turn fires after.
resp = openai.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "What's the Pix limit at night?"}],
)

That's the whole integration.

What happens under the hood

For every chat.completions.create call:

  1. Extracts the last user message as the query
  2. Calls loop.search(query) — pulls any relevant corrections
  3. Injects them into your system prompt (or creates one if absent)
  4. Calls OpenAI with the augmented messages
  5. Calls loop.log_turn(question, answer) with the result

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

Install

pip install agentloop-py agentloop-py-openai openai

Per-call options

Pass an agentloop kwarg alongside your normal OpenAI params. The wrapper strips it before forwarding (OpenAI rejects unknown kwargs).

resp = openai.chat.completions.create(
    model="gpt-4o-mini",
    messages=[...],
    agentloop={
        "user_id": "u_123",                        # scope memory search + log_turn
        "session_id": "sess_abc",                  # passed to log_turn
        "signals": {"thumbs_down": True},          # merged with auto-detected
        "metadata": {"latency_budget_ms": 500},    # stored with the turn
        "skip": False,                             # True = bypass AgentLoop entirely
        "search": False,                           # skip only retrieval (still logs)
        # or "search": {"limit": 5, "tags": ["pix"]}
    },
)

You can also pass a typed PerCallOptions instance if you prefer:

from agentloop_openai import PerCallOptions

resp = openai.chat.completions.create(
    model="gpt-4o-mini",
    messages=[...],
    agentloop=PerCallOptions(user_id="u_123", signals={"thumbs_down": True}),
)

Configuration (passed at wrap time)

openai = wrap_openai(
    OpenAI(),
    loop=loop,

    # Custom memory injection. Default: append to system prompt.
    inject_memories=lambda memories, messages: [...],

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

    # Max memories per call. Default 3.
    search_limit=3,

    # Apply these tags to every memory search.
    search_tags=["production"],

    # Only log turns when at least one signal fired. Default False.
    only_log_when_signaled=False,
)

Low-level API

For callers who want explicit control:

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

resp = ask_with_agentloop(
    openai,                           # raw, unwrapped OpenAI client
    messages=[{"role": "user", "content": question}],
    per_call=PerCallOptions(user_id="u_123"),
    config=WrapOptions(loop=loop),
    model="gpt-4o-mini",              # forwarded to OpenAI
    temperature=0.2,                  # forwarded to OpenAI
)

Not mutated

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

raw = OpenAI()
wrapped = wrap_openai(raw, loop=loop)

raw.chat.completions.create(...)       # no AgentLoop hooks
wrapped.chat.completions.create(...)   # with AgentLoop hooks

Streaming

Not supported in v0.1. Streaming intercepts are planned for a later release (requires buffering assistant text to call log_turn after the stream closes). For now, if you pass stream=True, call AgentLoop methods directly rather than using the wrapper.

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_openai-0.2.0.tar.gz (15.8 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_openai-0.2.0-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

Details for the file agentloop_py_openai-0.2.0.tar.gz.

File metadata

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

File hashes

Hashes for agentloop_py_openai-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e4404bd6215fcadfe4a2440abb225777ebcde5cd820e0c42cc72794d5708c633
MD5 c687a301972e0151d6e0e845c258dca0
BLAKE2b-256 f5f94f6514d302c2c29bf33908a32e9c15a37b4dc8e35c10fb7ba6121e61e7b5

See more details on using hashes here.

File details

Details for the file agentloop_py_openai-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for agentloop_py_openai-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2d7b68dcd1ee00391a556408d761f6d67023ff82ffb125d4fb15e28e0b23970c
MD5 e34bde8e0c0767fad5bc5587504b0c23
BLAKE2b-256 9aae55111b779131ce3535bf4d1427bed311ab64a0dca20a87a821f22c48d854

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