# How It Works

Start by connecting your wallet — no signup, no email, just sign a message to prove ownership. ZDrive derives a unique encryption key from your wallet signature; this key never leaves your browser and is never stored anywhere, even by ZDrive. You're now authenticated for the session.

Type a prompt or upload a file. Your browser immediately encrypts everything with AES-256-GCM using your derived key. The encrypted blob and metadata (which model, which tier) go to the ZDrive worker. The worker never sees plaintext — only ciphertext plus metadata. It verifies your wallet signature, checks your credit balance, routes your request to an available inference provider, and streams back the encrypted response. Your browser decrypts the response locally. You see the result; the network saw only encrypted noise.

If you want to store your encrypted data permanently, the worker signs the ciphertext as an ANS-104 DataItem and uploads it to Arweave via Irys node2. The network confirms the upload and returns an Arweave transaction ID. That ciphertext is now immutable and globally retrievable — but only you can decrypt it because only you have the encryption key.

For inference, ZDrive routes your request through a fallback chain of TEE-backed providers. If the primary model is overloaded or unavailable, the worker silently retries the next option in the chain. You get a response header (`X-Zdrive-Model-Used`) telling you which model processed your query. Credits are consumed after successful inference, not before.

## End-to-end flow

```mermaid
sequenceDiagram
    participant B as Browser
    participant W as CF Worker
    participant KV as KV Store
    participant BC as Base Chain
    participant T as Chutes TEE
    participant I as Irys / Arweave

    B->>B: Derive AES key from wallet signature
    B->>W: POST /v1/chat/completions {wallet, sig, session, messages}
    W->>W: Verify wallet signature (ERC-1271)
    W->>BC: Read credit balance
    W->>KV: Check rate limit
    alt Paid tier (credits > 0)
        W->>W: Use selected model
    else Free tier
        W->>W: Override to Qwen3-32B
    end
    W->>T: Forward inference (SSE stream)
    T-->>W: Stream response chunks
    W-->>B: Stream response (X-Zdrive-Model-Used header)
    B->>B: Decrypt response locally
    W->>BC: consumeCredit() async
    opt Vault upload
        B->>B: AES-256-GCM encrypt file
        B->>W: POST /vault/upload {ciphertext, x-vault-tags}
        W->>I: Signed ANS-104 DataItem
        I-->>W: {id: arweaveTxId}
        W-->>B: {id: arweaveTxId}
    end
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.zdrive.io/how-it-works.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
