• 2 min read

K2 Chat

A chat app that answers questions from your own documents, on Cloudflare Workers with a queue-driven AWS ingestion pipeline.

Cloudflare WorkersAWS CDKStep FunctionsPineconeGeminiNext.js

K2 Chat system architecture

What it is

You upload PDFs, text files or Word documents, and then ask questions about them. Answers come back with citations that link to the passages they used. If your documents don’t cover the question, the model can call a web search tool instead.

Each account only ever sees its own documents, conversations, search results and cache entries.

How it works

The Next.js frontend runs on Cloudflare Pages and talks to a Hono API on Cloudflare Workers. AWS handles auth (Cognito), records (DynamoDB), file storage (S3) and the background work. Gemini writes the answers, with Cloudflare Workers AI as a fallback, and Pinecone stores the vectors.

An upload goes straight from the browser to S3. S3 notifies SQS, and a Lambda consumer checks that the document record belongs to the uploader before it picks a path by file size:

File sizePath
up to 10 MiBSQS → Lambda (extract, chunk, embed) → Pinecone
10 to 100 MiBSQS → Lambda dispatch → Step Functions → Glue (Spark) → Pinecone
over 100 MiBrejected by the upload API

When you ask a question, the API embeds it, pulls matching chunks, and drops any that don’t belong to your account or to a ready version of the document. Retrieved chunks go through a small reranker that I trained in PyTorch and re-implemented as a plain TypeScript forward pass, so the Worker needs no ML runtime. All infrastructure is defined in AWS CDK (Python).

Results

I ran a bounded synthetic test through the large-file path (Step Functions → Glue → Pinecone):

InputVectors ready with matching document and version metadata
100,000 synthetic tokens224 / 224

That run checks ingestion and indexing. It does not measure throughput, answer quality or cost. Two bugs turned up during it (request batching and nested Workers AI embedding output) and I fixed both.

I also timed the Spark chunking job locally on 200 synthetic files (about 29 MB) at different partition counts, three runs each:

PartitionsAverage
11.870 s
41.434 s

Past 4 partitions it got slower again (1.634 s at 8, 1.776 s at 16) because scheduling overhead outweighed the parallel work. That matched the decision to cap Glue at 2 workers.

The first version

K2 Chat started in January 2026 as a self-hosted platform (public repo). A FastAPI gateway talked to a gRPC inference service that ran small Hugging Face models on a DigitalOcean droplet: SmolLM2 and Gemma 3 270M for text, SmolVLM for images, Whisper tiny for audio. Supabase handled auth, Postgres with row-level security, and file storage. The backend sat behind a Cloudflare Tunnel, so the server had no open inbound ports.

Jaeger trace of a multimodal request crossing the gateway and inference service

I traced requests with OpenTelemetry and Jaeger and used flame graphs to keep token streaming non-blocking. The small droplet kept running out of memory with several models loaded, so I switched to loading one model at a time and unloading the rest. Running models myself on a tiny server was the main limit, which is why the second version moved inference to hosted APIs and put the effort into the document pipeline.

Back to top