Skip to content

Paygent Documentation

Build cost-controlled AI products. Track costs, enforce limits, and get alerted — for OpenAI, Anthropic, LangChain, CrewAI, and the Vercel AI SDK.

Paygent is an SDK — for Python and TypeScript — that tracks per-user LLM costs and enforces spending limits before calls reach OpenAI or Anthropic. Drop it into your AI product, configure plans, and every call gets tracked per user, checked against a spending plan, and synced to a backend you can query.

from paygent import Paygent, paygent_context
pg = Paygent.init(api_key="pg_live_...")
with paygent_context(user_id="user_123"):
    response = openai.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": "Hello"}],
    )
    # Tracked. Guarded. Synced.
import OpenAI from "openai";
import { Paygent, paygentContext } from "@paygentjs/sdk";

const pg = await Paygent.init({ apiKey: "pg_live_..." });
const openai = pg.instrument(new OpenAI());
await paygentContext({ userId: "user_123" }, async () => {
  const response = await openai.chat.completions.create({
    model: "gpt-4o-mini",
    messages: [{ role: "user", content: "Hello" }],
  });
  // Tracked. Guarded. Synced.
});

Start here

  • Quickstart — Get cost tracking and spending limits working in under 10 minutes.
  • What is Paygent? — The problem Paygent solves and how it fits in your stack.
  • Frameworks — LangChain, CrewAI, Vercel AI SDK — drop-in integrations.
  • Webhooks — Get real-time alerts when users cross spending thresholds.
  • Manual Events — Track costs for actions Paygent didn't auto-capture.
  • Dashboard — Built-in analytics UI for cost visibility.
  • Python SDK reference — Every method, parameter, and return type for the Python SDK.
  • TypeScript SDK reference — The same for @paygentjs/sdk.

What you can build

  • AI chatbots and copilots — Per-user spend caps so power users don't erase margins on a subscription tier.
  • Multi-tenant agent platforms — Isolate cost tracking per tenant with product-scoped API keys.
  • Freemium AI products — Free-tier model limits with soft warnings that prompt upgrades.
  • LLM-powered SaaS — Track exactly what each user costs, by model, by session, in real time.

Common tasks

How Paygent works

Paygent gives you three layers of cost control:

Visibility — Every LLM call is tracked per user, per model, per session. See exactly what each user costs you in real time on the built-in dashboard, or query via the SDK and API.

Limits — Spending limits are checked before the call leaves your server. A soft gate fires a warning at 80% of any limit; the call still runs. A hard gate raises PaygentLimitExceeded at 100% — blocking the call before a single token reaches OpenAI or Anthropic.

Alerts — Configure webhook endpoints to get notified in real time when users cross spending thresholds (50%, 80%, 100%), hit soft or hard gates, or on daily/weekly/monthly usage summaries. Push to Slack, trigger billing flows, or feed your own alerting system.

See Core Capabilities for the full picture, or How the guard works for the mechanics.

Resources

  • Cost Guardrails — How auto-instrumentation, sessions, and gating work under the hood.
  • API reference — Backend REST API for plans, users, usage, and gate events.
  • GitHub — Source code, issues, and contributions welcome.
  • Get help — Email the founders directly. We respond within a day.