A native iOS app that talks to a server I built and run myself, which routes messages to a language model, streams the answer back word by word, remembers what was already said, and now lets the model call tools that run on the phone or on the server. The interesting part isn't the app, it's everything that happens in between.
The foundation for a system I'm still actively building on, not a finished chatbot.
On the surface this is simple: type a message on an iPhone, get a reply from an AI model. But I didn't want the app talking directly to an AI company's servers. Instead, the message travels to a server I own and run, which decides which model should answer, sends the request along, and streams the response back into the app as it's being written, not all at once at the end.
That middle piece, my own server sitting between the app and the AI, is the actual project. I'm calling it my AI Harness because it's infrastructure I plan to keep extending: more capable routing, tools, memory, and eventually more complex mobile AI workflows all get built on top of this same foundation.
Click through each step below to see where the message is at that moment and why that step exists.
The easy version of this project would have the iPhone app talk straight to an AI provider. I didn't build it that way, on purpose. A phone app is something anyone can pull apart, so it should never hold private keys or make permanent decisions about which AI model to use. Instead, all of that lives on my server, in one place I control.
The credentials needed to call an AI provider live only on my server. The app never sees them, so there's nothing sensitive to extract even if someone took the app apart.
Which AI model answers, what happens if one fails, how a future feature like memory or tools would work, all of that can change on the server without touching or resubmitting the iPhone app.
The app is the interface. The server is the control layer underneath it.
My server is a small Python program. I built it to be asynchronous, meaning it doesn't sit and wait around while an AI model is thinking. It can start one request, immediately move on to handle something else, and come back when the model has something to say. That matters because AI responses can take a few seconds, and a server that just freezes during that time would be a bad building block for anything more advanced later.
Three pieces work together here, each doing one job:
In plain terms: Starlette decides what my server does with a request. Granian is the part that's actually listening for that request in the first place. Splitting those two things apart is a standard pattern in real backend systems, and it's the first time I built something that way instead of running one script that did everything itself.
An AI model doesn't produce its whole answer instantly, it generates it piece by piece. My server doesn't wait for the whole thing to finish before sending anything back. As soon as the model produces a small chunk of text, the server forwards that chunk straight to the app, which is why the reply appears to type itself onto the screen instead of showing up all at once.
Under the hood, this uses a technique called Server-Sent Events: my server keeps the connection to the app open and pushes small pieces of text down it one at a time, instead of closing the connection after a single reply the way a normal web request would.
Send the message, then wait, with nothing on screen, until the entire answer has been generated somewhere else and finally arrives all at once.
Words start appearing within a second. The full answer takes the same amount of time to finish generating, but it never feels like the app is stuck doing nothing.
The parsing turned out trickier than the concept. SSE events are separated by blank lines, but Swift's built-in line reader silently skips empty lines, which means it throws away exactly the delimiter the protocol depends on. I ended up reading the stream byte by byte and building lines myself so I could actually see where one event ended and the next began.
My server isn't locked to one single AI provider. It keeps a list of providers it's allowed to use, starting with Groq, and picks one for each request. If that provider is slow, unavailable, or returns an error, the server can try the next one on the list instead of just failing outright. The app never has to know or care which one actually generated the answer.
This stopped being theoretical fast. I started with Groq alone, and once I was actually testing regularly, I ran straight into its rate limit. Instead of just waiting it out, I added OpenRouter as a second provider the server could fall back to. That one annoyance is basically why the fallback logic exists in its current form, not because I planned for it up front, but because I hit the exact failure it's meant to handle.
If Groq is rate-limited or down, the server falls back to OpenRouter rather than assuming the first provider always works. Only if nothing responds does it tell the app so honestly, instead of pretending it succeeded.
Because the app only ever talks to my server, not to a specific AI company, I was able to add OpenRouter as a second provider without changing a single line of the iPhone app.
My server has its own address on the internet and only accepts encrypted connections, so no one sitting in between can read what's being sent. While building and testing this, I had to set up that encryption myself, which meant the app and the server had to be introduced to each other and agree to trust one another before any conversation could happen.
That taught me something I didn't expect going in: "just turn on HTTPS" isn't a single switch, it's an actual trust relationship between a device, a certificate, and a server, and every piece of that chain has to be set up correctly for it to work at all.
I didn't want to have to manually start this server every time I wanted to use the app. So instead of running it as a script in a terminal window, I set it up as a proper background service on the cloud machine it lives on. That means it starts automatically whenever the machine boots, and if it ever crashes, it restarts itself instead of just staying down.
That's a small change on paper, but it's the difference between "a script I run when I remember to" and "a service that's actually available." It's the same expectation any real product would need to meet.
The connection now worked, but every message was still a one-off. That is where Chapter 02 starts.
Everything up to this point could send a prompt and stream back an answer, but every request was independent. The model had no idea what had been asked five seconds earlier. If I wanted the app to feel like an actual conversation instead of a series of one-off questions, I had to solve a different problem: how do you build context around a model that has none of its own?
Without context
With context
Language models don't automatically know what happened earlier in a conversation. Every time I wanted a coherent back-and-forth, I had to explicitly send the prior turns back to the model along with the new message. The question stopped being "how do I call an LLM" and became "how do I reconstruct a conversation for a model that forgets it the instant it answers."
The app still just sends a message. All of the new work happens on the server: it identifies which app is asking, looks up what that app's conversation has said so far, hands the model the full history instead of a single line, and saves the new exchange once it's done. The app never has to manage any of this itself.
Watch what actually gets sent to the model as a conversation grows. Each turn you add here gets appended to the request payload, the same way it happens on the server.
Nothing sent yet. Click the button to start the conversation.
The model never "remembered" the first message on its own. The trick is that the full history gets sent back to it every single time, and that history has to live somewhere between requests since the model itself keeps none of it.
My server isn't only ever going to talk to one app. That means it can't just pull "the most recent conversation" out of the database. Every stored turn is tagged with an app identifier (the iOS app's bundle ID), and the server only loads turns that match the app making the current request before it builds the context to send the model.
It's a small field doing an important job. Without it, one shared table of stored turns would turn into one shared, tangled conversation. With it, the same backend and the same database can hold as many separate conversations as there are apps talking to it, and each one only ever sees its own history.
These are real screenshots from the simulator, not mockups. Both runs ask the same follow-up question, "what did I just say," after a first message, using two completely different models behind the same backend.

Before: no conversation yet

After: "You just said, 'howdy?'"

First message, answered by qwen3.8-27b

Follow-up sent to the same conversation
Because the app never talks to a provider directly, swapping which model answered a given conversation doesn't change anything about how that conversation's memory is stored or retrieved. The context layer sits below the model, not inside it.
Memory only covers what has already been said. It cannot help when the model needs something nobody has told it yet, like where I am right now. That is where Chapter 03 starts.
Take a simple question: "What's the weather at my location?" The model is stuck. It does not know where my phone is, and it cannot see today's weather.
The fix is called tool calling (also known as function calling). I describe a few functions to the model. When it needs one, it asks for it by name, my code runs it, and the model uses the result to finish its answer. The model never runs anything itself. It only asks.
The answer needs two facts that live in two different places. My location is on the iPhone. The weather is on a weather service. And the order matters: the model needs the coordinates before it can ask about the weather. Click through to follow the whole trip.
The key design choice is deciding where each tool lives. I used two terms for it. A resident tool lives on my server, so the server can run it directly. A non-resident tool lives somewhere else, here on the phone, so the server has to pass the request along and wait for the answer.
Model → Server → iPhone → Server → Model
Model → Server → Weather API → Model
One agent, two execution environments. The model coordinates both as if they were one workflow.
Each side has a Toolbox, a simple lookup table from a tool's name to the code that runs it. Every tool also has a schema, a small JSON file that tells the model what the tool is called, what it does, and what inputs it takes. The model reads the schema. My code supplies the actual behavior. That is why the model never touches the GPS. It can only ask for get_location, and the app decides how that works.
The payoff is that the networking code does not need special handling for each new tool. It looks up the name, reads the arguments, and runs the match. Adding a tool later means adding one entry.
In Chapter 01 the stream only delivered words. Now the same open connection can also deliver a request: "please run this function." The Swift parser I wrote for Chapter 01 learned a new event type, tool_calls. When one arrives, the app does not show it on screen. It runs the tool. Toggle between the two below.
For the userThe app appends these words to the chat bubble as they arrive.
For the appThe app decodes this, looks up get_location in its Toolbox, and runs it. Nothing appears in the chat.
Illustrative payloads, simplified for this page. They are examples, not recorded output.
When the model asks for the phone's location, its answer is only half finished. The phone runs the tool, but then the result has to land in the right conversation. The server solves this with a turnID, a number that marks the paused turn. It builds directly on the SQLite memory from Chapter 02.
The app sends the question and the list of tools it can run.
Only the turn number and the tool's result. No history, no tool list.
Illustrative payloads. The turn number is an example, not a recorded value.
To store this I added tools and calls tables to the database. When a result arrives, the server finds the turn, rebuilds the conversation from SQLite, and hands it all back to the model to continue.
I tested the backend on its own first, then connected the app. That way, when something broke, I knew which half to look at.
Real simulator run, frontend tested with qwen3.5:9b
The app's whole job is showing a conversation and reacting to typing. Every decision about credentials, which model answers, and what happens if something fails belongs on the server. Keeping that boundary clean made both halves easier to reason about.
A reply isn't one thing that arrives, it's a sequence of small pieces arriving over time. The app has to be built to update itself as new pieces show up, not to wait for a single finished answer.
Encryption, the actual web server, my application code, and the way requests get routed all solve separate problems that stack on top of each other. Understanding where one layer's job ends and the next one's begins made debugging far less mysterious.
A tool call is only a request. The model names a function, and my code chooses whether and how to run it, on the phone or on the server. Keeping that line clear is what made it safe to give the model more abilities.
Each chapter added one ability to the same system: it connected, then remembered, and now it acts. Getting it to run on my laptop and keeping it reliably available turned out to be two very different skills.
I connected the app, gave it memory, and now it can use tools on both the phone and the server. The foundation is in place, so each new ability builds on the same system instead of starting a new app. Next I want to give the agent more tools to choose from, harder tasks, and richer input than text.
The system streams answers, remembers conversations, and runs multi-step tool workflows across an iPhone and my own server. It began as a university tutorial series that I implemented and extended. Everything below is what I am building toward next.
The system spans a native SwiftUI client, a Python backend, SQLite persistence, model routing, and SSE streaming, and tool calling across both sides. I wrote the client, the server, the conversation storage, and the routing logic myself, with the AI providers handling only the actual language generation.