Credit System

Search documents
How to Build an Agent with Auth and Payments - LangGraph.js
LangChainยท 2025-06-30 17:28
Core Functionality & Architecture - The application provides a credit system for charging users based on LLM usage, allowing them to purchase Stripe subscriptions for more credits [1][2] - The codebase consists of five key areas: authentication, payments, credit infrastructure, chat agent, and user interface [3][4][5] - Authentication is implemented using Superbase, with JWT tokens protecting the Langraph agent via middleware [3][4][8] - Payments infrastructure is handled by Stripe, enabling users to buy subscriptions and receive credits [4][12][13] - Credit infrastructure, stored in Superbase, includes utility functions for adding, refreshing, and removing credits [4][14][15] Technical Implementation - The project is a monorepo with an 'agents' application (Langraph agent, middleware) and a 'web' application (UI, Stripe, Superbase, credits) [5][6] - Langraph middleware verifies Superbase JWT tokens to grant users permissions to interact with the graph [4][8] - Stripe integration includes utility functions for creating sessions, getting subscriptions, and managing user credits [12] - A webhook route processes Stripe events (subscription creation, updates, deletion) to update user data in Superbase [13] - The application uses providers to manage the state of authentication and credits on the client side [10][14] Data Flow & Security - Superbase JWT tokens are passed through the application to the Langraph middleware for authentication [4][18][19] - The thread and stream providers are key components for passing the JWT token to the Langraph client [18][19] - User credits are updated optimistically on the UI and finalized in the Superbase database [15][16] Resources & Documentation - The repository includes a credit system file outlining key files for the credit and authentication systems [20] - A detailed readme provides step-by-step instructions for setting up and running the application [21]