Skip to main content
ActGuard patches OpenAI transport methods to capture usage and enforce budget scopes.

Requirements

RequirementVersion
openai SDK>=1.76.0
Python>=3.9
pip install "openai>=1.76.0"

Patched methods

  • openai._base_client.SyncAPIClient.request
  • openai._base_client.AsyncAPIClient.request

Usage with ActGuard runtime

import actguard
import openai

client = actguard.Client.from_env()
oai = openai.OpenAI()

with client.run(user_id="alice", run_id="run-openai"):
    with client.budget_guard(usd_limit=0.10) as guard:
        response = oai.chat.completions.create(
            model="gpt-4o",
            messages=[{"role": "user", "content": "Hello"}],
        )

print(guard.tokens_used, guard.usd_used)

Streaming behavior

For /chat/completions streaming calls, ActGuard injects:
{"stream_options": {"include_usage": true}}
This enables final usage extraction without changing application code. The Responses API is also tracked (streaming and non-streaming).