Case Study/AI & Agents/Architecture, engineering, maintenance
Bilgim Omnichannel AI Ecosystem
- Author
- Aryan
- Client
- Own product
- Published
- 2026
- Length
- 8 min · 1620 words
Abstract
One Bun and Elysia core behind five clients, with a single UID and credit wallet across all of them.
Key outcomes
- Clients
- 5 on one core
- Identity
- Unified UID
- Models
- 35+ pooled
The thread is the unit of value
Most AI tools live in a browser tab. Close the tab and the thread is gone; open the phone and the same person arrives as a stranger with a second account and a second balance. For a product whose work is long research, generation and tool-calling, that is the wrong unit of measurement. The thread is the unit of value, not the session, and a session belongs to whatever surface happened to open it.
Bilgim is built on the opposite assumption. Context, memory and credits belong to a person, and every client is a door into the same state. That one sentence determines most of the architecture below. If there is more than one door, there can only be one room behind it, and everything that is shared between clients has to be designed as shared from the start rather than assembled later.
One core, five clients
The repository is a Turbo monorepo holding a single API and every client that talks to it.
apps/apiis the core: Bun as the runtime, Elysia for HTTP and WebSocket, Mastra as the agent orchestrator. All logic, streaming and generation live here.apps/webis the Next.js 15 workspace, the full desktop cockpit.apps/botis a grammY Telegram interface, for the moments when the operator is not at a desk.apps/mobileis an Expo client, deliberately narrow rather than a port of the web app.apps/adminis a Vite panel for operating the platform: jobs, usage, model health.
The alternative was a shared library vendored into each client, and it was rejected early for a specific reason. A vendored library makes the API surface optional. The mobile build can pin an old version, the bot can ship its own validation, the admin panel can reach into the database directly, and the system drifts into three subtly different behaviours that all look correct in isolation. Keeping the logic behind a process boundary means a model call, a quota check or a thread mutation is written once and reached over REST, WebSocket or the bot’s own command surface. The cost is a network hop and a serialisation contract. The return is that there is exactly one implementation of every rule, and a fix in the orchestrator is a fix everywhere.
The identity link is the hard part
The interesting engineering in an omnichannel product is that Telegram Login, Google OAuth and phone OTP all arrive as unrelated credentials for the same human being, and reconciling those credentials into one identity is the whole job the account model exists to do.
The account model separates an identity from an account. Each provider produces an identity record: the provider, the provider’s user id, and verified claims. The account is keyed on a single universal UID, and that UID is the thing credits, threads and usage attach to. Login is therefore a resolution of identity to a UID, and the resolution has to be safe when it runs twice concurrently.
The merge step runs on every login. It validates the provider token, looks for an existing link on that identifier, and attaches the new identity to the matching UID if one exists, otherwise creating a UID and linking the first identity to it. The link write is the part that has to be atomic. Two logins from different providers arriving in the same second, both finding no existing account, must not create two UIDs for one person. That is enforced at the database boundary with a unique index on the provider identifier and a compare-and-set on the account’s identity set, not with a read-then-write in application code, because the application-level version has a window between the read and the write that only appears under load.
Phone OTP adds a second problem. A phone number is a mutable identifier, not a stable key, because numbers are reassigned. It can authenticate a login and it can link to an account, but it cannot be the account.
Credits belong to the UID across every surface
Wallet design is the same decision restated in money. Credits are held against the UID. A video generation started in the web workspace and a tool call made from Telegram draw on the same balance, and the mobile dashboard reflects the spend without a reconciliation job between platforms.
Per-surface balances would have been easier to build and wrong to operate. They force a reconciliation step, they turn “how much does this person cost” into an aggregate instead of a lookup, and they let one person run out of credit in one client while holding plenty in another. Holding the balance centrally and debiting it in the same transaction that admits a job also removes the double-spend race, where two clients submit work at once and both pass an affordability check against the same pre-debit balance.
The asymmetry is that every client now depends on one ledger. That is a deliberate concentration. One place to reason about money beats four places that have to be kept in agreement, and the admin panel reads the same source of truth rather than an export of it.
Long jobs over a status bus
Generation is asynchronous by nature. Video renders and multi-step agent runs outlast any reasonable request timeout, and a request that holds a connection open for a render is a connection that cannot be used for anything else.
Rather than have clients poll, the API publishes job state on a WebSocket bus. A client subscribes once, receives progress, and then receives the terminal result. Polling was the obvious first design and it is worse in two specific ways. It scales with the number of waiting clients multiplied by the frequency of their optimism, and it makes the interface dishonest, because a client that has not heard anything renders the last thing it knew. On the bus, a job that is still running looks like it is still running, and a job that failed reports the failure rather than a timeout that got mistaken for one.
The status bus is also where the surfaces converge. The same event stream that updates the web workspace drives the Telegram bot’s progress message, which is why the Telegram client never needed its own job-tracking implementation.
Contracts instead of conventions
Four shared packages carry the things that must not drift:
@repo/configvalidates the environment with Zod and holds model pricing and system constants.@repo/databaseowns the Mongo schemas and the repository patterns.@repo/typescarries the API request and response shapes and the WebSocket event contracts.@repo/uicarries the design system.
The value is that type errors surface at the boundary during a build, which turns what would be a runtime mismatch between a mobile build and an API three versions ahead into a compile-time failure. Model pricing living in @repo/config is the same idea applied to money: the price of a model is a configuration fact, and it should change in one place instead of in each client’s request builder.
The environment story follows from the same principle. The monorepo uses one root environment file and a single install command, and each application reads its specific subset through @repo/config. That is what makes a five-application repository tolerable to run locally: one install, one configuration file, one Turbo pipeline for lint and build.
The pooled model layer
The platform exposes more than one model family behind a single interface, and the orchestration layer treats the model as a parameter rather than as a piece of architecture. Mastra holds the agent definitions and the tool set, and the tool set is what makes an agent useful: browser control, external API calls and database reads sit alongside plain generation.
Keeping provider selection and pricing in shared configuration is what makes the pool operable. A model becomes unavailable or changes price, and the change is a configuration edit that every client respects, rather than a deploy of four surfaces. The same layer is where streaming is normalised, so the web workspace, the bot and the mobile client all consume one event shape regardless of which provider answered.
What the shape bought
Adding a surface is additive. The web app, the bot and the mobile client were each built against an API that already worked, which is why the same features landed on all three without a second implementation, and why a fix in the orchestrator fixes it in all of them.
The narrow mobile client is the clearest example. Because the state lives on the server, the phone does not need to be a port of the desktop application. It needs to render threads, spend and job state, and it can leave the heavy editing surface to the workspace. That was only possible because the mobile client never owned the state in the first place.
What I would do differently
Two things stand out.
Deferred database work on the roadmap is a warning sign. Persistent agent memory is listed as a later phase, which means the first version did not settle the shape of long-term memory before the clients were built against it. Memory is not a feature that can be bolted onto an agent afterwards without touching how threads, retrieval and context assembly work, so the clients inherited an interface that will have to change.
The second is the identity merge. Building it as the login path coupled it to every provider integration. The resolution step should have been a standalone component with its own collection and its own tests from the first commit, so that adding a fourth provider would be a plugin rather than a change inside the authentication flow.
References
- [Author]Aryan (Uo1428)
Built with