|
| 1 | +--- |
| 2 | +title: Inference & Settlement |
| 3 | +--- |
| 4 | + |
| 5 | +Previously, we got our "passport" to the LazAI network. We learned how to create a digital identity and register a piece of data on the blockchain. We are now official citizens of this new digital country. |
| 6 | + |
| 7 | +But what do we _do_ here? The most exciting part of the LazAI network is its bustling marketplace of AI services. What if your agent needs to think with a super-powerful AI brain that you can't run on your own computer? How do you securely "rent" time on someone else's machine and pay them for it? |
| 8 | + |
| 9 | +## The Pay-As-You-Go AI Marketplace |
| 10 | + |
| 11 | +Imagine you have an AI Agent but you want it to use a massive, cutting-edge AI model. Running this model requires expensive, powerful hardware that you don't have. |
| 12 | + |
| 13 | +In a centralized world, you'd subscribe to a single company's service. In the LazAI network, you can access a global, decentralized marketplace of "inference nodes"—people and organizations all over the world who are renting out their AI processing power. |
| 14 | + |
| 15 | +**Our goal:** We will take a simple `Agent` and make it run its "thinking" process (inference) on a remote machine in the LazAI network. We'll see how payment is handled automatically and securely, without needing to trust the node operator directly. |
| 16 | + |
| 17 | +## The "Digital Check": How Settlement Works |
| 18 | + |
| 19 | +How can a node operator trust that you'll pay them _after_ they've done the work? And how can you be sure you're only paying for the work you requested? |
| 20 | + |
| 21 | +The solution is a "digital check" that you sign _before_ you even send your request. In Alith, these are called **settlement headers**. |
| 22 | + |
| 23 | +When you want to ask a remote AI model a question, here's what happens: |
| 24 | + |
| 25 | +1. **You Write a Digital Check:** Your `LazAI Client` creates a special set of data (the headers) that says, "I, wallet `0x123...`, promise to pay node `0xABC...` for this one request." |
| 26 | +2. **You Sign It:** You digitally sign this "check" with your wallet's private key. This is a cryptographic guarantee that it came from you. |
| 27 | +3. **You Attach It to Your Request:** Your AI's question and this signed check are sent together to the remote node. |
| 28 | +4. **The Node Verifies the Check:** Before doing any work, the node operator can quickly verify your signature and check the blockchain to see if you have enough funds deposited to cover the cost. |
| 29 | +5. **Work is Done & Payment is Claimed:** Once they see the check is valid, they run the AI model and send you the answer. Later, they can submit your signed "check" to the blockchain to claim their payment. |
| 30 | + |
| 31 | +This creates a fair, trustless, and pay-as-you-go system. |
| 32 | + |
| 33 | +## Using a Remote AI Model: A Step-by-Step Guide |
| 34 | + |
| 35 | +Let's modify our `Agent` to use a remote AI model. We'll be referencing the logic in the [`lazai_inference_settlement.py`](https://github.com/0xLazAI/alith/blob/main/sdks/python/examples/lazai_inference_settlement.py) example file. |
| 36 | + |
| 37 | +**1. One-Time Setup: Join the Club and Add Funds** |
| 38 | + |
| 39 | +Before you can shop in the marketplace, you need to register as a user and deposit a small amount of funds to cover service fees. This is like opening a bank account in the new country. |
| 40 | + |
| 41 | +_This is a one-time operation._ Once you've done it, you're ready to make requests. |
| 42 | + |
| 43 | +```python |
| 44 | +from alith.lazai import LazAIClient |
| 45 | + |
| 46 | +# This is the "address" of the service provider group we want to join |
| 47 | +LAZAI_IDAO_ADDRESS = "0x34d9E02F9bB4E4C8836e38DF4320D4a79106F194" |
| 48 | +client = LazAIClient() |
| 49 | + |
| 50 | +# This block is only run once to set up your account |
| 51 | +try: |
| 52 | + client.get_user(client.wallet.address) |
| 53 | + print("User already registered!") |
| 54 | +except Exception: |
| 55 | + print("Registering user and depositing funds for services...") |
| 56 | + client.add_user(10000000) # Register on the network |
| 57 | + client.deposit_inference(LAZAI_IDAO_ADDRESS, 5000000) # Deposit funds |
| 58 | +``` |
| 59 | + |
| 60 | +This code first checks if you're already a registered user. If not, it registers you and deposits some funds into a special account for paying for inference services. |
| 61 | + |
| 62 | +**2. Find a Service Provider (Inference Node)** |
| 63 | + |
| 64 | +Now, let's find a shop to buy from. We'll ask our `client` for the web address (URL) of a registered inference node. |
| 65 | + |
| 66 | +```python |
| 67 | +# Get the URL of a node that provides the service |
| 68 | +url = client.get_inference_node(LAZAI_IDAO_ADDRESS)[1] |
| 69 | + |
| 70 | +print(f"Found an inference node at: {url}") |
| 71 | +``` |
| 72 | + |
| 73 | +This fetches a list of available providers from the blockchain and gives us the URL for one of them. |
| 74 | + |
| 75 | +**3. Get Your Signed "Digital Check" (Settlement Headers)** |
| 76 | + |
| 77 | +This is the most important step. We ask our `client` to generate the signed settlement headers for our request. |
| 78 | + |
| 79 | +```python |
| 80 | +# Create the signed "digital check" for our request |
| 81 | +headers = client.get_request_headers(LAZAI_IDAO_ADDRESS) |
| 82 | + |
| 83 | +print("Generated settlement headers:", headers) |
| 84 | +``` |
| 85 | + |
| 86 | +**Example Output:** |
| 87 | + |
| 88 | +``` |
| 89 | +Generated settlement headers: {'X-LazAI-User': '0x...', 'X-LazAI-Node': '0x34d9E...', 'X-LazAI-Nonce': 16, 'X-LazAI-Signature': '0x...'} |
| 90 | +``` |
| 91 | + |
| 92 | +This dictionary contains your identity (`X-LazAI-User`), the provider's address (`X-LazAI-Node`), a unique number for this request (`X-LazAI-Nonce`), and your unforgeable digital signature (`X-LazAI-Signature`). |
| 93 | + |
| 94 | +**4. Create an Agent Pointing to the Remote Node** |
| 95 | + |
| 96 | +Now, we create our `Agent`, but instead of letting it use a local model, we tell it to send its requests to the remote node's URL and to include our "digital check" with every request. |
| 97 | + |
| 98 | +```python |
| 99 | +from alith import Agent |
| 100 | + |
| 101 | +agent = Agent( |
| 102 | + model="Qwen-2.5", # The name of the model on the remote server |
| 103 | + base_url=f"{url}/v1", # The remote server's API address |
| 104 | + extra_headers=headers, # Attach our signed "digital check" |
| 105 | +) |
| 106 | +``` |
| 107 | + |
| 108 | +The `base_url` tells the agent _where_ to send its thoughts, and the `extra_headers` ensures it gets paid for. |
| 109 | + |
| 110 | +**5. Talk to the Remote Agent!** |
| 111 | + |
| 112 | +Now, you can use the agent just like before. But this time, the hard work is being done on a powerful remote machine! |
| 113 | + |
| 114 | +```python |
| 115 | +response = agent.prompt("What is Alith?") |
| 116 | +print(response) |
| 117 | +``` |
| 118 | + |
| 119 | +When you run this line, your agent sends the question "What is Alith?" _plus_ your signed settlement headers to the remote node. The node verifies your payment guarantee, runs the `Qwen-2.5` model, and streams the answer back to you. |
| 120 | + |
| 121 | +### How It Works Under the Hood |
| 122 | + |
| 123 | +The process seems simple from your side, but it involves a beautifully coordinated dance between your script, the remote node, and the blockchain. |
| 124 | + |
| 125 | +1. **You call `agent.prompt()`:** Your script wants an answer. |
| 126 | +2. **Agent Prepares Request:** The `Agent` creates a standard web request for the AI model. |
| 127 | +3. **Client Attaches Headers:** It automatically includes the `extra_headers` you provided. |
| 128 | +4. **Request Sent:** The complete package (prompt + headers) is sent over the internet to the remote inference node. |
| 129 | +5. **Node Verifies Signature:** The _first thing_ the remote node does is check your `X-LazAI-Signature`. It uses a public function to confirm that the request really came from your wallet address and hasn't been tampered with. It also checks the blockchain to ensure you have enough funds deposited. |
| 130 | +6. **Node Does the Work:** If the signature is valid, the node performs the computationally expensive AI inference. |
| 131 | +7. **Response Returned:** The AI's answer is sent back to your `Agent`. |
| 132 | +8. **Settlement Occurs:** The node now holds your signed "check" (the headers) as proof of work. It can submit this proof to the blockchain at any time to have the funds transferred from your deposit to its own account. |
| 133 | + |
| 134 | +Here is a diagram of the flow: |
| 135 | + |
| 136 | +```mermaid |
| 137 | +sequenceDiagram |
| 138 | + participant You as Alith Agent |
| 139 | + participant RemoteNode as Remote Inference Node |
| 140 | + participant LazChain as LazChain |
| 141 | +
|
| 142 | + You->>RemoteNode: Sends prompt + signed settlement headers |
| 143 | + RemoteNode->>LazChain: "Is this signature from this user valid?" |
| 144 | + LazChain-->>RemoteNode: "Yes, and they have funds." |
| 145 | + RemoteNode->>RemoteNode: Runs the AI computation |
| 146 | + RemoteNode-->>You: Returns the final AI-generated answer |
| 147 | +``` |
| 148 | + |
| 149 | +The code in [`lazai_inference_server.py`](https://github.com/0xLazAI/alith/blob/main/sdks/python/examples/lazai_inference_server.py) shows the other side of this transaction. It's a server that listens for requests, and its first step is to check for and validate these exact `X-LazAI-*` headers before it proceeds with any AI computation. |
0 commit comments