Middleware
The Settlement & Billing Middleware is a component that a node operator can "plug in" to their service node. This middleware sits between the user and the AI model, performing two critical jobs, authentication and billing.
We learned how to run the "factories" that provide the computational power for our AI. These nodes are the workhorses of the network. But running powerful computers costs real money for electricity and hardware. So, how do the people running these nodes get paid for their services?
This is where the Settlement & Billing Middleware comes in. Think of it as the automated accounting system for the entire AI network.
The Problem: A Free-for-All Isn't Sustainable
Imagine a highway system where there are no toll booths. Everyone can drive as much as they want for free. At first, it sounds great! But soon, the roads would be overcrowded, and there would be no money to pay for maintenance, repairs, or building new roads. The system would collapse.
Our AI network is like that highway. If using powerful AI Service Nodes is free, there is no incentive for people to provide the expensive computing resources needed to run them. We need a fair and automated way to charge for usage, like a smart toll booth that only charges for the distance you've driven.
The Solution: A Smart Utility Meter for AI
The Settlement & Billing Middleware
is our smart toll booth. It's a component that a node operator can "plug in" to their service node. This middleware sits between the user and the AI model, performing two critical jobs:
- Authentication: Before letting a request through, it checks the user's identity and confirms they have enough funds on the LazChain to pay for the service.
- Billing: After the AI model has done its work, the middleware measures the resources used (like the number of text "tokens" processed), calculates the cost, and automatically initiates the payment from the user to the node operator on the blockchain.
This middleware is the key to creating a decentralized marketplace for AI compute.
How a Node Operator Enables Billing
Unlike the other components we've seen, you don't typically use this middleware in your Agent
code. Instead, the person running the AI Service Node enables it when they start their server.
In the Node section, we saw this command to start a server:
To turn on the billing system, the node operator simply adds the --settlement
flag.
That's it! By adding that single argument, the server is now running our "smart toll booth," ready to authenticate users and charge for AI processing.
Under the Hood: The Life of a Billed Request
When a user sends a request to a node with settlement enabled, a fascinating dance happens between the server and the blockchain.
- User Signs the Request: The user, using their wallet, creates a cryptographic signature that essentially says, "I, User A, approve this action and agree to pay for it." They send this signature along with their request.
- Middleware Intercepts: The
TokenBillingMiddleware
catches the incoming request before it reaches the AI model. - Authentication & Validation: The middleware checks the user's signature to prove their identity. It then makes a quick call to the LazChain to verify that the user's account is valid and has sufficient funds.
- AI Processing: If everything checks out, the request is passed along to the AI model, which generates a response.
- Cost Calculation: The middleware intercepts the outgoing response. It looks inside the response data to find out how much work was done (e.g.,
"total_tokens": 512
). - Settlement: It calculates the final cost (e.g., 512 tokens * price per token) and submits this information to the LazChain to finalize the payment from the user to the node operator.
- Final Response: The original response from the AI model is sent back to the user.
Here is a diagram of that flow:
Diving into the Code
Let's see how the code enables this. In the alith/inference/server.py
file, the run
function checks for the --settlement
flag we used.
This shows the "plug-in" nature of the middleware. If settlement
is true, we simply add the TokenBillingMiddleware
to our server application (app
).
Now, let's look at a simplified version of the middleware itself from alith/inference/settlement.py
. Middleware classes have a special dispatch
method that gets to inspect both the request
and the response
.
This code follows our diagram perfectly. It lets the main application do its job by calling await call_next(request)
, and then it does its own accounting work afterward.
The final piece of the puzzle is the calculate_billing
function.
This function does the simple math and then, most importantly, calls a method on the client
object. This client
is our bridge to the blockchain.