llm-over-dns

System Architecture Guide

This document describes the high-level architecture, component design, and data flows of the LLM over DNS server.


🗺️ System Overview

LLM over DNS is an asynchronous, stateless UDP DNS server written in Rust. It functions as a gateway that intercepts incoming standard DNS TXT record queries, forwards the prompt to an LLM provider (AnyRouter or OpenRouter), chunks the response, and returns standard DNS records to the client.

graph TD
    Client["DNS Client (e.g., dig, nslookup)"] -->|1. TXT UDP Query| Server["LLM over DNS Server (Rust + Tokio)"]
    Server -->|2. Validate & Parse| Handler["DnsHandler"]
    Handler -->|3. Forward Prompt| ClientAPI["LlmClient"]
    ClientAPI -->|4. HTTP JSON Request| Gateway["LLM Gateway (AnyRouter / OpenRouter)"]
    Gateway -->|5. Forward Inference| LLM["LLM (Gemini, Llama)"]
    LLM -->|6. Answer Text| Gateway
    Gateway -->|7. HTTP Response| ClientAPI
    ClientAPI -->|8. Clean String| Handler
    Handler -->|9. Split 255-char Chunks| Chunker["Chunker"]
    Chunker -->|10. Build TXT records| Handler
    Handler -->|11. Send DNS Packet| Client

🏗️ Core Components

The codebase is modularized into isolated components designed for testability and thread-safe parallel processing:

1. Main Server Entry (main.rs & server.rs)

2. DNS Handler (dns_handler.rs)

3. LLM Client (llm_client.rs)

4. Chunker (chunker.rs)


🔄 Core Data Flow

Let’s trace a typical request:

  1. UDP Listener: A UDP packet arrives on port 5454. The server spawns a new async Tokio task to process it.
  2. Extraction: DnsHandler extracts the query name. E.g. what-is-rust.example.com is parsed into the query prompt "what is rust".
  3. API Dispatch: LlmClient makes a POST request to AnyRouter.
  4. Fallback Handling: If the primary model returns a rate limit or gateway error (e.g. 429 or 502), the client instantly triggers a backup model query.
  5. Response Split: The string response is passed to the Chunker.
  6. Packet Response: The handler populates the Answer section of the DNS Message with the array of chunked TXT records and sends it back to the client.

🔒 Security & Performance Considerations

Input Sanitization

Gateway Rate Limits

Performance Profile


🛠️ Testing & Quality Gates