Skip to main content

AgentCOGS

AgentCOGS is an open-source Python SDK and a self-hosted dashboard for customer-level AI unit economics. It attributes each LLM call to the customer who caused it, compares that cost with the monthly fee, and checks a per-customer budget before the next SDK call.

Know which AI customers make you money, and which ones are eating your margins. The blended total can look fine while one fee no longer covers the AI cost. Which customer stops making money this month.

pip install agentcogs
agentcogs init
agentcogs status

init records one sample for customer_001 at $0.0246 and leaves enforcement off. It does not call a model. With no API key, a later call inside run(customer_id=...) is priced from the response and stored in that same file. instrument() does not write a row. No prompt or provider key is written.

This package is the SDK. The customer list is the dashboard in the repository, and you run that yourself. A hosted API is not serving. The working sample is agentcogs.vercel.app.

What does the number mean?

AI margin is (monthly fee − attributed AI cost) / monthly fee. It is the contribution of the fee after attributed AI cost. It is not total customer profitability, and it is not accounting gross margin. People, infrastructure, support, payment processing, refunds, discounts, credits, and proration are outside it. Pace is a straight line from the cost so far. It does not forecast a change in behavior. You type the fee, or you pull that customer's Stripe subscription. The SDK does not invent it.

Install, configure, instrument, verify

AGENTCOGS_ENDPOINT defaults to http://localhost:8000. Create a workspace with the self-hosted dashboard, then:

import os
from openai import OpenAI
import agentcogs

agentcogs.init()  # AGENTCOGS_API_KEY, AGENTCOGS_WORKSPACE_ID, AGENTCOGS_ENDPOINT
print(agentcogs.ping())
agentcogs.set_customer("acme")  # your tenant id, once per request

with agentcogs.run(workflow_id="support") as ctx:
    if os.environ.get("OPENAI_API_KEY"):
        OpenAI().chat.completions.create(
            model="gpt-4o-mini",
            messages=[{"role": "user", "content": "Say hi in one word."}],
            max_tokens=5,
        )

print(ctx.wait_for_ingest().accepted, ctx.run_id)

The SDK does not call a model. Inside the block, an OpenAI or Anthropic call is read from the response. Cached tokens and cache writes are part of the posted total. With no provider call, the same block posts a zero-cost run so you can see the row. Open that customer and type the monthly fee.

python examples/hello_agentcogs.py in the repository is this path. python examples/one_run.py posts one recorded cost of USD 0.84 and does not call a model.

How attribution works

set_customer() names the tenant for this request. run() watches the instrumented client calls in the block and posts one event with total_usd when the block exits. If no customer is set, run() raises before the block. A duplicate run_id is accepted as a duplicate and is not stored twice. On the self-hosted API, that duplicate keeps the customer stored on the first commit. A failed post sits in a local outbox and is retried. python -m agentcogs outbox status shows what has not been delivered.

What the budget check does

Enforcement stays off until the workspace turns it on. While it is off, an exceeded monthly_budget_usd does not stop the next run(). When the workspace turns enforcement on, an exceeded budget raises CustomerBudgetExceededError and does not enter the block. Two calls already in flight can both pass. It is not an atomic reservation. A LiteLLM callback records cost after the provider has already answered, so it cannot stop that call.

LiteLLM does not ship the callback. You copy it from the repository. The FastAPI middleware is a separate extra: pip install "agentcogs[integrations]".

Supported integrations

OpenAI and Anthropic Usage on the response inside run(), including cached tokens and cache writes
FastAPI Set the tenant from the request. Needs the integrations extra
LangGraph Wrap the graph
LiteLLM A callback you register. It posts after the proxy call
Stripe The subscription is the fee. An optional meter pushes usage you already attributed
Slack A spike opens that customer

One fictional account

Patternstack is a snapshot of forty fictional customers, as of 14 Nov 2026. The blended AI margin is 74.6%. Vellum Research has spent USD 5,822.84 against a fee of USD 8,200, so it is 29.0% so far, and the straight-line pace ends near −52.2%. The sample does not receive your calls.

Opening the fictional Patternstack sample. AI margin is 74.6%. Vellum Research is 29.0% so far and the month ends near −52.2%.

Documentation

What is not guaranteed

A match against the provider invoice, refunds, credits, discounts, currency conversion, and a hard stop when two calls pass the pre-check together. Development status is Beta: the SDK surface is init, set_customer, run, ping, and shutdown, and a hosted API is not part of this release.

Metadata

Release files for agentcogs 0.2.3

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

Source distribution (sdist)

Source distribution for agentcogs 0.2.3
File Size Uploaded Provenance Actions
agentcogs-0.2.3.tar.gz 29.8 kB With provenance Details for agentcogs-0.2.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for agentcogs 0.2.3
File Interpreter ABI Platform Actions
agentcogs-0.2.3-py3-none-any.whl Python 3 none any Details for agentcogs-0.2.3-py3-none-any.whl

Total release size: 68.9 kB

Release files / agentcogs-0.2.3.tar.gz

Download URL agentcogs-0.2.3.tar.gz
Size 29.8 kB
Tags Source
SHA-256 checksum
How to use checksums
03ab01c092386a4ad49044ed04e4b48f28d0c7a5e0a054b80a04f722185ff0d1
BLAKE2b-256 checksum
How to use checksums
235a3ba939e81c1b057318bdbb8e9690f3a398eb343a06c46167b915827b110a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Release files / agentcogs-0.2.3-py3-none-any.whl

Download URL agentcogs-0.2.3-py3-none-any.whl
Size 39.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
237681edb25fcb3bccdb053be165b8f55b4338ed112a08c0281f9facf3bb7d31
BLAKE2b-256 checksum
How to use checksums
cac04c2b651e0124c348b73b434e09171be413ea5595e3352e77bfaf5e551a09
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Security for agentcogs 0.2.3

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for agentcogs-0.2.3.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

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