Skip to content

LNURL AI API brainstorming

Christopher David edited this page Jul 27, 2023 · 3 revisions

AI Endpoint Protocol

Step 1: Getting AI Models and Prices

A user wants to hit an AI endpoint, so they hit api.whatever.com/.well-known/lnai and receive the following response:

{
  "models": [
    {
      "model": "gpt3",
      "url": "<url to hit to interact with it>",
      "per_token_in": 0.0,
      "per_token_out": 0.003,
      "per_request": 0.0
    },
    // Add more models and prices as needed
  ],
  "lnurl": "<lnurl for payment>"
}

Step 2: Initiating the LNURL Protocol

The user initiates the LNURL protocol, and the service replies with the following payerData:

{
  // ...standard lnurl payerdata fields.
  "auth_token": {"mandatory": true}
}

Step 3: Calculating Expected Usage

The user can calculate their usage based on the model they plan on using. Doing this in advance is important so the payment system doesn't induce latency. However, it need only be done for a few minutes in advance because lightning is fast and settlement is final.

Step 4: Making Advance Payment

The user uses LNURL to pay, in advance, for their expected usage. The payment should be enough to cover the future usage before the next payment. An accurate estimation engine, that uses streams of requests and a geomentric mean and standard deviation should be suitable to predict the next 10 minutes of usage with a 99.999% certainty. That gets paid for in advance. A standard engine for this can be built and included with the protocol examples.

Step 5: Hitting the AI Model

Finally, the user makes a POST request to the URL of their choice with a BEARER token from the LNURL service payment, and the SAME api parameters they are used to. Since BEARER tokens are standard in AI, and no major changes have been made to the flow, all that's necessary for the service provider to implement is "inserting the auth_token along with token limits into a db"... something they are already doing today.

Step 6: Handling Errors

The remote ai service responds an error without performing work if its estimated tokens don't match what has been paid on that particular token.

Step 7: Requesting a Sweep

The end-user can request a sweep to a new auth_token at any time by using the following endpoint:

api.whatever.com/.well-known/lnai/sweep?from=old_token&to=new_token

Replace old_token with the current auth_token, and new_token with the desired new auth_token.

Clone this wiki locally