AI for All: Powering APIs and Large Language Models with Lightning ⚡🤖

Olaoluwa Osuntokun and Michael Levin
July 6, 2023

Today, we’re excited to announce the release of a new set of developer tools to enable the Lightning and AI developer communities to build global, inclusive, and out-of-the-box cost effective LLM (Large Language Model) tooling which seamlessly incorporates Lightning and bitcoin. These tools are built on the L402 protocol, a Lightning native authentication mechanism, and Langchain, a popular library to simplify working with AI agents by adding external data to allow for more advanced functionality:

  • LangChainBitcoin: a suite of tools that enables Langchain agents to directly interact with Bitcoin and also the Lightning Network, including two main features:
    • LLM Agent BitcoinTools: a series of tools that leverages the newly released OpenAI GPT function calls to enable developers to create agents that can hold a Bitcoin balance (on-chain and Lightning), send/receive Bitcoin on Lightning, and also directly drive with a LND node.
    • L402 HTTP API Traversal: a Python wrapper for a LangChain LLM chain (APIChain) that enables agents to transparently access L402 payment-metered APIs.
  • Aperture: the latest release of Aperture, a drop-in reverse proxy server implementation of the L402 protocol which can transform any API into a pay per use resource gated via L402 functionality and sats on the Lightning Network.
    • Now with Lightning Node Connect Support: Aperture supports any Lightning Node running litd, a binary that bundles Lightning Labs’ services alongside LND, (e.g Voltage, Umbrel, Start9, Raspiblitz, BTCPayServer) through the Lightning Node Connect protocol, an end-to-end encrypted mechanism for connecting a Lightning node to a web browser.
    • Rich Dynamic API Endpoint Pricing: Aperture now supports the ability to optionally provide full HTTP request context to the proxied backend to enable dynamic per-call pricing. In contrast to the existing static pricing configuration, dynamic pricing allows a backend to implement custom endpoint pricing logic.
  • L402 bLIP: a Bitcoin Lightning Improvement Proposal (bLIP) specification for the L402 protocol standard to openly share the primitives for building these components with the community.
    • L402 is a protocol standard for payment-metered APIs explicitly designed to fit neatly into the existing HTTP 402 Payment Required status response code, which was optimistically included by the creators of the Web to enable native payments. A combination of Bitcoin, the Internet's native currency and the Lightning Network enables globally accessible rapid, scalable micropayments for paid APIs via L402.

The L402 standard directly supports the use case of charging for services and authenticating users in distributed networks. L402 combines the authentication functionality of macaroons, which are authentication mechanisms for distributed systems that don't require access to a central database, and the global permissionless payments of the Lightning Network to deliver the ability to authenticate and charge per use of internet-native services (e.g. APIs). At the time of creation of the internet, when the 402 error was included, those micropayments were not possible, but with the L402 protocol and bitcoin, these long-ignored status responses can be utilized to build the machine payable web of the future, powered by Lightning.

With the tools we're releasing today, developers will be able to build more accessible AI infrastructure for both humans and agents alike. And the time to build is now, with the AI4ALL hackathon starting this week and running for the whole month of July, we’re incredibly excited to release these tools to the community to see what can be built. 🚀

Why now for the Lightning + AI Convergence?

With the meteoric rise of LLM development over the past few months, we’ve heard from the AI developer community that the development of LLM models currently encounters a few challenges that the Lightning Network, and the L402 protocol specifically, can help solve.

First, the expense of training GPUs requires some upfront cost that developers look to recoup via the low hanging fruit of credit cards, which results in higher than necessary costs to the end user to incorporate chargeback and fraud fees. Further, this business model means that billions of people who don’t have access to traditional credit card or banking rails cannot easily get access to these new mediums for supercharging education, productivity, and creativity. Finally, because of these cost scaling issues, the best models end up behind closed source APIs, which significantly harms the accessibility and inclusivity of the latest and greatest LLM models.

GPUs (Graphics Processing Units) are the foundation upon which AI and LLM models are built and trained. Given the explosion of activity in the space, GPUs are more in demand than ever before. The soaring price of GPUs has had a direct effect on the infrastructure cost of training, deploying, and building on top of the latest LLM models. Nearly all of the popular LLM platforms require credit card information in order for users to access their most advanced and reliable models. A reliance on credit cards serves to increase costs for both platform providers and users alike. Due to rampant fraud and chargeback fees, platform providers are forced to further increase the cost of their platforms beyond what is dictated purely by unit economics. One direct effect of the link to traditional banking and credit card rails is that billions of people around the world are not able to interface with the new creative and educational medium provided by today’s LLMs.

Additionally, a new fast growing class of “organisms” (i.e. intelligent LLM or AI Agents) are unable to easily gain access to fiat systems of payment as they aren’t registered “entities” with any nation. However, these agents will certainly need to pay for resources whether from gated APIs or paid sources of data. Plus, they will need to be able to effectively evaluate pricing signals to determine the most efficient path to accomplish a task. These payments, evaluations, and decisions will lead to thousands of AI agents making countless micro-payments and micro-decisions a day. Given these factors, it makes sense the creators of AI agents will ultimately gravitate towards a globally available, permissionless, near-instantly settled internet native monetary system in Bitcoin and Lightning rather than the traditional fiat system which simply cannot support these types of payments or evaluations.

In looking at the latest wave of innovation enabled by AI creators, we find a distinct but related problem of rising deployment costs. The success of an AI application can actually become a barrier to further success due to the current billing paradigm: creators must expose their AI application to the world with their credit card and API key on the line. A popular application either must be ready to rack up exorbitant credit card bills, or be shut down (or rate limited) to protect the creator from their own success. In order for these applications to properly scale, creators need a way to offload all or part of their cost in a low cost, globally inclusive, and privacy preserving manner to the end user, which is enabled by Lightning and the L402 protocol.

Turning our attention to the popular LLM platforms, we find yet another accessibility issue: the most powerful models are gated behind closed source APIs with opaque terms of use. Thankfully, we’ve seen the rise of several performant fully open source models to ensure that these innovations will be available to all of humanity. However, running the most powerful models is still out of reach for many individuals. Democratizing access to the latest AI models requires a novel way for those without the most advanced hardware to incrementally pay for access to these groundbreaking models.

The L402 protocol, which was created in 2020 as we foresaw the rise of both the machine payable web, as well as the Lightning Native web, can address many of these issues. It can dramatically increase the accessibility of AI for both humans and AI agents alike. The L402 protocol can be used by any entity with an internet connected device, and some bitcoin or the ability to receive/acquire it. The protocol uses the Lightning Network to enable instant, high-volume payments in a privacy preserving manner. In order to help facilitate this new wave of innovation, we’ve updated the L402 protocol with a new bLIP specification, and have released some new easy to use tools for AI hackers, creators, and agents alike.

The L402 Protocol & Aperture

The L402 protocol is a protocol standard for the authentication of paid APIs (using the internet’s preferred currency: sats!). The L402 protocol finally presents a use for the internet’s forgotten HTTP error code: 402 Payment Required. You’ve probably seen error codes such as 200 OK, 404 NOT FOUND, and 500 INTERNAL SERVER ERROR. Alongside these error codes, the early authors of the HTTP protocol also inserted provision to enable internet-native payments. However, at the time, Bitcoin hadn’t yet been created, so a decentralized currency for the internet didn’t yet exist. The L402 protocol bridges the gap, by creating a protocol standard that enables programmatic micropayments for API, login, and digital resource access.

Aperture is the first L402-aware reverse proxy that enables seamless L402 integration for any existing API. Aperture serves as a bridge between an incoming request and a payment metered backend. Aperture handles the L402 protocol negotiation, pricing, authentication, and API key creation for the backend. As a result, any existing API backend can easily be upgraded to add L402 awareness.

The latest version of Aperture now enables dynamic pricing. This dynamic pricing feature means that an Aperture-aware API backend can dynamically inform the proxy what the new pricing should be for a given endpoint. In the context of LLMs, this means that a backend could modify the price as a function of the type of model, query length, context window, and other parameters directly related to the operational cost of a model. Aperture also now supports SQLite and Postgres database backends, which should be a very familiar construct for developers.

The combination of the L402 protocol and open source models enables a number of impactful innovations. As an example, a user can sell a prompt by gating access to an API capable of responding to queries against the prompt itself. Potential buyers can then ask their own local agent to evaluate the response given a set of criteria. If the agent approves of the response, then further responses can be purchased. This interaction effectively creates a new tit-for-tat evaluation model to ensure that purchased data and information meets quality guidelines.

Each new generation of models requires a wealth of data for training, but the ever-increasing trend towards data protectionism, visible in platforms like Twitter and Reddit, has not only curtailed this data access but also detrimentally affected the usability of these services for the human users. The introduction of the L402 protocol offers a solution. By standardizing the handling of HTTP 402 payment required responses, it provides an automated, dynamic way for agents to negotiate and traverse paid APIs, providing services a direct way to monetize and recuperate operational costs while rate-limiting spam.

In order to permit LLM applications to do more than respond to simple queries, a new class of intelligent hierarchical agents has emerged. In order to enable these agents to traverse APIs themselves, we’ve created a wrapper for LangChain agents to become L402 aware.

L402-Aware LangChain Agents

LangChain is the most popular library used to create AI agents capable of tasks like automatically debugging and fixing code, planning complex projects, and breaking down deliverables into easy steps. LangChain makes the more complicated parts of working with AI models easier by allowing you to bring external data to your LLM and by providing advanced functionality for your agents, which allows for your models to interact with its environment via decision making. In order to maximize the impact of these agents, they need a bridge to the real world along with the ability to pay for resources and APIs on the internet. In order to solve this problem, we’ve created a new wrapper around the existing APIChain abstraction that permits agents to ingest API docs for any API and interact with it with L402 awareness.

Our LangChanL402 wrapper can be dropped into any existing LangChain system that uses the APIChain abstraction to enable it to traverse L402 APIs.

from langchain.llms import OpenAI from lightning import LndNode from l402_api_chain import L402APIChain # Create a connection to an active lnd node. lnd_node = LndNode( cert_path='path/to/tls.cert', macaroon_path='path/to/admin.macaroon', host='localhost', port=10018 ) # You can also use this with an API Chain instance like so: llm = OpenAI(temperature=0) # Create the L402-aware API chain. The API_DOCS here describes the API docs to provide as context to the model to learn which calls need to be called. chain_new = L402APIChain.from_llm_and_api_docs( llm, API_DOCS, lightning_node=lnd_node, verbose=True, ) output = chain_new.run('LLM query here') print(output)

This functionality dramatically increases the set of actions that agents are capable of, as they are now able to traverse L402 APIs needed to pay for real world resources. As an example, an agent can deploy a containerized version of itself to the cloud via an L402 AWS API gateway, then purchase GPU hours to train a more powerful version of itself. The above code fragment can be used to traverse any L402-metered API given just a set of API docs and our L402 wrapper.

In order to limit the amount of bitcoin an agent can use on paid APIs, the specific macaroon above can be backed by an LND Account to create a limited off-chain spending account.

We've created an easy to run Jupyter notebook that demonstrates the new L402APIChain wrapper in more depth.

LangChainBitcoin: LLM Agent Bitcoin Tools and Function Calls

With the L402APIChain wrapper, agents are now able to programmatically access paid APIs, however without a way to properly budget, and potentially obtain more bitcoin, a richer set of functionality is needed. By hooking up Lightning and Bitcoin tools directly to the LLM agent, we give the agent the ability to hold, send and receive Bitcoin (on-chain or over Lightning). Leveraging the newly released OpenAI GPT function calling APIs we're able to create a set of Langchain tools that enables agents to directly interact with Bitcoin and the Lightning Network. At a high level, we create some functions implementing tools, then describe these tools to the agent. A description can include a strict schema for the input arguments (a: string, b: Invoice) and also some examples of responses.

Here's a code snippet that shows how to ask the agent to generate a new Bitcoin address:

lnd_node = LndNode( cert_path='tls.cert', macaroon_path='lnd.macaroon', host='localhost', port=10018 ) # Create a new set of tools backed by our lnd node. The agent will be able to # use these tools to anwer queries and iterate towards goals. lnd_tools = LndTools.from_lnd_node(lnd_node=lnd_node) llm_model = ChatOpenAI(model="gpt-3.5-turbo-0613") bitcoin_agent = initialize_agent( lnd_tools.get_tools(), llm_model, agent=AgentType.OPENAI_FUNCTIONS, verbose=True, ) print(bitcoin_agent.run("make a new bitcoin address to receive funds")) --- > Entering new chain... > Invoking: `new_address` with `{}` address: "sb1p0dypm3slr2umg3wnx07pk4kqn2xm5qlcmmetkkjw5t542sgj3jyq2j6mm7" You can use the following Bitcoin address to receive funds: sb1p0dypm3slr2umg3wnx07pk4kqn2xm5qlcmmetkkjw5t542sgj3jyq2j6mm7 > Finished chain. You can use the following Bitcoin address to receive funds: sb1p0dypm3slr2umg3wnx07pk4kqn2xm5qlcmmetkkjw5t542sgj3jyq2j6mm7

To see more detailed examples of the functionality provided by these bitcoin tools in combination with native OpenAI function calls, check out the above-referenced notebook, which can be run locally.

langchain bitcoin tools
Asking the agent to query for the current wallet balance.

Conclusion

We’re extremely excited about the new wave of innovation brought about with the latest wave of LLM models. With the L402 protocol and LangChainBitcoin, we’re able to ensure the open models can be accessed using an open payment system, connected to the world’s set of open APIs. In the end, open source will win, and we’re working to provide the tooling to enable greater accessibility to AI for humans and agents alike.

To get started using these developer tools to build your own AI and Lightning-powered products, check out the L402 bLIP, download Aperture, and read the Aperture developer documentation. If you need access to a Lightning node with LNC integration, you can spin one up in a variety of ways from running your own custom setup to using a node OS like Umbrel, Start9, or Raspiblitz to using a hosting provider like Voltage. As long as the node also runs litd, then you have access to Lightning Node Connect and can connect it directly to your Aperture instance. If you’re a developer eager to start building with these tools, check out the AI4ALL hackathon happening this month! We’re incredibly excited to see what the AI and Lightning developer communities build with these new tools. If you're interested in the intersection of Bitcoin, AI Agents, and the Lightning Network, we're hiring! Finally, if you haven't already, join our Slack community, contact us on Twitter, attend the lnd PR review club, contribute a PR, or subscribe to our newsletter!

About the authorOlaoluwa Osuntokun

Olaoluwa received his B.S and M.S in CS from UCSB. During his graduate studies he focused on the field of applied cryptography, specifically encrypted search. Before he became an active contributor to the Bitcoin open source ecosystem, he spent three consecutive summers as a Software Engineering Intern at Google. These days, his primary focus lies in designing and building private, scalable off-chain blockchain protocols, such as Lightning.

Olaoluwa received his B.S and M.S in CS from UCSB. During his graduate studies he focused on the field of applied cryptography, specifically encrypted search. Before he became an active contributor to the Bitcoin open source ecosystem, he spent three consecutive summers as a Software Engineering Intern at Google. These days, his primary focus lies in designing and building private, scalable off-chain blockchain protocols, such as Lightning.

About the authorMichael Levin

Michael received his Bachelor of Science in Economics from the Wharton School at the University of Pennsylvania with a double concentration in Management and Operations, Information, and Decisions along with a minor in Engineering Entrepreneurship. Before Lightning, Michael worked across a variety of teams and functions at Google including product, growth, and marketing. Michael loves food, travel, live music, skiing, and sports.

Michael received his Bachelor of Science in Economics from the Wharton School at the University of Pennsylvania with a double concentration in Management and Operations, Information, and Decisions along with a minor in Engineering Entrepreneurship. Before Lightning, Michael worked across a variety of teams and functions at Google including product, growth, and marketing. Michael loves food, travel, live music, skiing, and sports.