Large language models (LLMs) can be used to create a broad spectrum of automations, often personified as “AI agents.” These automations can translate prompts into sequences of programmatic actions to interact with other systems.
These agents are built using new frameworks that blend prompting and function calling. This post explores how to integrate the Stripe agent toolkit into these new frameworks and some common use cases. Stripe can enhance your agents’ functionality by enabling access to financial services and tools to allow your agents to help you earn and spend funds, facilitate common support operations, and bill for usage with metered billing.
What is an agentic workflow?
An “agentic workflow” combines large language models and function calling to achieve an objective. For example, consider searching for and purchasing a flight. A user may query, “Book a flight from New York to San Francisco on 4/24 for under $500.” To achieve this, we need to be able to:
- Turn the query into variables –
origin
,destination
,departure_time
, andbudget
. - Search and filter a flight database provided those variables.
- Present the user with the options and enable them to select.
- Purchase the flight.
Frameworks including Vercel’s AI SDK, LangChain, and CrewAI make it easier to build multi-agent workflows, breaking down each task and assigning to specialized agents. “Tools” can be provided to an agent – in this case to search online or to execute a purchase. These tools are code snippets that the LLM provider can “ask” the agent framework to execute.
Integrate function calling
With Stripe’s agent toolkit, you can provide your agents access to the Stripe API. It natively supports Vercel’s AI SDK, LangChain, and CrewAI, and works with any LLM provider that supports function calling. It’s built on top of Stripe’s Node.js and Python SDKs.
For example, imagine we want to build a “business partner” agent that can help facilitate tasks like invoicing users. We can instantiate a new StripeAgentToolkit
with our secret key and pass its tools to the agent.
import {StripeAgentToolkit} from '@stripe/agent-toolkit/ai-sdk'; import {openai} from '@ai-sdk/openai'; import {generateText} from 'ai'; const toolkit = new StripeAgentToolkit({ secretKey: "sk_test_123", configuration: { actions: { // ... enable specific Stripe functionality }, }, }); await generateText({ model: openai('gpt-4o'), tools: { ...toolkit.getTools(), }, maxSteps: 5, prompt: 'Send <<email address>> an invoice for $100', });
The toolkit can be used alongside any other set of tools allowing for complex, multi-step operations. For example, you can combine Stripe and Slack together to create powerful automations.
from langchain.agents import AgentExecutor, create_structured_chat_agent from langchain_community.agent_toolkits import SlackToolkit from stripe_agent_toolkit.langchain.toolkit import StripeAgentToolkit stripe_agent_toolkit = StripeAgentToolkit( secret_key=os.getenv("STRIPE_SECRET_KEY"), configuration={ "actions": { "payment_links": { "create": True, }, "products": { "create": True, }, "prices": { "create": True, }, } }, ) slack_toolkit = SlackToolkit() tools = stripe_agent_toolkit.get_tools + slack_toolkit.get_tools() agent = create_structured_agent(llm, tools, prompt) agent_executor = AgentExecutor(agent=agent, tools=tools) agent_executor.invoke({ "input": "Create a new payment link for $100 and post it to #my-channel" })
This SDK is an early exploration of integrating Stripe into agentic workflows. As the behavior of agents is non-deterministic, we recommend exploring the SDK in test mode and running evaluations to assess the performance of your application. Additionally, we recommend using restricted API keys to scope access to the functionality your agent requires.
Use financial services
Agentic workflows need not have exclusively virtual outcomes. Imagine a travel agent that can book flights for your company. Using LLMs and function calling we can assemble a set of agents that can search for flights online, return options, and ultimately identify a booking URL. With Stripe, you can embed financial services and enable the automation of the purchase flow as well.
Using Stripe Issuing, you can generate single-use virtual cards that agents can use for business purchases. This enables your agents to spend funds. The Issuing APIs allow you to approve or decline authorizations programmatically, ensuring your purchase intent matches the authorization. Spending controls allow you to set budgets and limit spending for your agents.
For example, consider the intermediate step that returns a list of possible flights, and the agent presents them to the human user.
Airline 1, New York -> SFO, $250, #ABC
Airline 2, New York -> SFO, $300, #DEF
In response to a selection, a card can be generated restricted to that amount
stripe.issuing.Card.create( cardholder="ich_123", currency="usd", type="virtual", spending_controls={ spending_limits=[{ amount="30000", interval="all_time" }] } )
You can monitor card usage with Stripe’s real-time authorization handling, allowing your system to approve or decline agent purchases, matching against the user intent. The card can also be deactivated after the purchase has been confirmed.
Paired together, this takes the best of each integration pattern – the search and assessment capabilities of LLMs and function calling with the programmatic constraints, controls, and determinism of APIs. Users expect that the proposed purchase is the purchase that is occurring. Presenting the options and confirming the selection to the user is valuable, but more critically is having the backing controls and monitoring to validate that intent matches action.
Measure usage with metered billing
Conducting agentic workflows have material cost – typically measured by token use or time. With usage-based billing, you can charge based on a customer’s usage of your product. The toolkit provides middleware to easily track prompt and completion token counts and send billing events for that customer.
import {StripeAgentToolkit} from '@stripe/agent-toolkit/ai-sdk'; import {anthropic} from '@ai-sdk/anthropic'; import { generateText, experimental_wrapLanguageModel as wrapLanguageModel, } from 'ai'; const stripeAgentToolkit = new StripeAgentToolkit({ secretKey: 'sk_test...', configuration: { // ... } }); const model = wrapLanguageModel({ model: anthropic('claude-3-5-sonnet-20240620'), middleware: stripeAgentToolkit.middleware({ billing: { customer: 'cus_123', meters: { input: 'input_tokens', output: 'output_tokens', }, }, }), }); const result = await generateText({ model: model, prompt: 'Tell me a joke!' });
We have a quickstart guide to help you quickly launch a chatbot with usage-based billing at https://docs.stripe.com/agents/quickstart.
Testing and reliability
Because agent behavior is non-deterministic, we recommend starting with the SDK in test mode and running evaluations to assess your application’s performance. Additionally, use restricted API keys to limit access to the functionality your agent requires.
The toolkit can be configured to use a subset of Stripe functionality, and we recommend selecting the tools required for the task you’re giving to your agent. For example, if you are building an agent to manage an inventory of your products, you can configure the toolkit to only include functions related to products and prices.
const toolkit = new StripeAgentToolkit({ secretKey: "sk_test_123", configuration: { actions: { products: { create: true, }, prices: { create: true }, }, } })
Further, we’ve minimized the surface area of our SDK to only focus on a subset of the Stripe API and we will expand supportability. Over time we’ll also provide richer configuration options to help you manage the available functionality and data of the SDK.
This is motivated by two reasons. First, as the number of available tools increases, the likelihood of selecting the right set of tools decreases. Secondly, although restricted access keys limit access at the authentication level, making the API unavailable removes any attempt whatsoever, helping guide the agent to the preferred outcome and avoiding tool failure mid-task.
For similar reasons, we’ve also reduced the request and response bodies of Stripe API requests that are returned to the LLM – for example, when creating a Payment Link, the response is abbreviated to only the ID and the URL – although the raw API response contains far more fields. By reducing the request and response shape to only the necessity, the LLM has better “focus” on what values to key on. This is even more important in multi-step flows where multiple function calls have to occur in sequence and depend on the values of previous calls.
Conclusion
With the Stripe agent toolkit you can now easily integrate Stripe into the most popular agent frameworks. This enables you to automate common workflows that depend on Stripe and also helps unlock new use-cases by providing agents access to financial services on tools. In addition, usage-based billing can quickly integrate in these agent frameworks to bill your customers. Take a look at agent toolkit documentation to explore more of its capabilities and how Stripe is supporting agent businesses.