# Foundry Agent Webapps: Cosmos or Azure SQL for Backend State, and When?

> Source: <https://dev.to/geekindata/foundry-agent-webapps-cosmos-or-azure-sql-for-backend-state-and-when-kj1>
> Published: 2026-11-29 16:34:12+00:00

**Recommendation:** When moving an agent webapp's app-owned state to Azure SQL, freeze the API contract, migrate only your own collections, and never re-home Foundry platform threads or memory.
More in the playbook "Designing agent systems": <https://wpnews.pro/playbooks/agent-architecture>

I used AI tools to help draft and edit this post. I checked the facts and the wording, and the original lives at [geekindata.com](https://www.geekindata.com/blog/47-foundry-webapp-cosmos-vs-sql). The diagrams are on that page.

**Key takeaways**

Community threads keep asking why Microsoft RAG chatbot samples push Cosmos for chat history when a small Azure SQL database would also store messages. Fair question. Document stores map cleanly to message bags, Learn walkthroughs ship fast, and Cosmos shows up in the diagrams people copy.

That question is **context**, not the whole thesis for a Microsoft Foundry agent webapp. Published agent shells often leave durable product history with your backend. The records your backend writes for the product are your tables. Agents, threads, files, knowledge/Search, and platform memory stay on Foundry. The real decision is which engine backs the **app-owned** plane behind one product API.

**Named model: API stays; store moves**

*Figure 1. Named model: API stays; store moves. Freeze the wire contract. Move only app-owned collections. Leave Foundry platform stores alone.*

Most Cosmos-versus-SQL arguments fail because they mix two planes.

**App-owned backend state** is what your agent webapp API persists for the product: collections A to D (records keyed by id, an index, and an append-only log). That plane is yours to put on Cosmos, Azure SQL, or something else. Callers care about routes and JSON, not which Azure SKU sits under the repository.

**Foundry platform stores** are what the service owns: agent definitions and tools, threads and runs, files and knowledge (including Search grounding), and platform memory. Those stay on Foundry. Shipping a Cosmos-to-SQL cutover for the shell's app-owned collections does not mean you re-home platform threads into your App Service database.

Figure 2 draws the boundary. The engine debate applies to the left column only.

*Figure 2. Two store planes. Cosmos versus Azure SQL is a decision about app-owned backend state. Conflating the planes is how forum debates talk past each other.*

Ask the Reddit question out loud, then narrow it.

Cosmos answers (1) often. Message documents are natural, partitioning tutorials are ready, and the architecture diagram looks like the Learn walkthrough. Teams then treat that default as a platform law.

Microsoft Foundry changes the second half. When a published agent experience leaves multi-turn product history with your backend, the shell API becomes the product history plane whether you like Cosmos or not. Community deploy notes already describe persisting threads and messages in Cosmos **or** Postgres/SQL behind the webapp API. Ownership is not optional. The engine still is.

So keep the sample default as **context**. Use it to explain why Cosmos appears in diagrams. Do not use it as the ADR for every app-owned table.

Figure 3 is the decision card for the app-owned plane.

*Figure 3. When Cosmos, when Azure SQL. Document bags and sample velocity favor Cosmos. Relational product state and a frozen HTTP cutover favor Azure SQL + EF Core.*

**Prefer Cosmos when**

**Prefer Azure SQL (+ EF Core) when**

**Never**

Defaults are not destiny. Requirements are.

A cutover like this can start when app-owned state sits on Cosmos and a team wants relational ops and one wiring path. The decision is not "Cosmos is wrong for every chat bag." It is "these collections want relational ops and one wiring path."

Ship the cutover as **API stays; store moves**:

A temporary bridge can help during migrate. A permanent two-store product is how wiring drifts return. Leave Foundry platform stores alone the entire time.

**Treating sample Cosmos as a Foundry mandate.**

Learn diagrams are teaching aids. Your product state contract is an ADR.

**Moving Foundry platform stores with the shell database.**

Agents, threads, files, knowledge, and platform memory are not more collections on your migration checklist.

**Changing JSON shapes during the engine swap.**

Then every caller bug looks like a database bug. Freeze the wire format first.

**Env drift: Cosmos in one environment, SQL in another.**

Environment-only failures follow. One repository wiring path after cutover.

**SQL firewall blocking EF before listen.**

Local startup never reaches "API is up" if migrations cannot connect. Open the firewall path before you chase SPA timeouts.

**Dual-writing forever.**

A temporary bridge is fine. A permanent two-store product reopens the drift you just closed.

**Use as-is**

**Adapt before you use**

Do not rip Cosmos out of a pure message-bag prototype that has no relational pressure just to match this note. Do not "fix chat history" by migrating Foundry threads into your SQL database. Do not leave Cosmos credentials in prod config after the app-data path is gone.

Sources (community grounding): [Why does MS recommend Cosmos for RAG chat history vs Azure SQL?](https://www.reddit.com/r/AZURE/comments/1d3dx54/azure_rag_architecture/), [Foundry published app / store behavior and backend-owned multi-turn state](https://www.reddit.com/r/AZURE/comments/1q8nw1h/azure_ai_foundry_new_agents_project_endpoint_vs/), [Deploy Foundry agents: persist threads in Cosmos or Postgres/SQL behind your API](https://www.reddit.com/r/AZURE/comments/1p67vdu/deploy_foundry_agents/), [Anyone here using Cosmos DB?](https://www.reddit.com/r/AZURE/comments/1sl05mj/anyone_here_using_cosmosdb/).

Sample defaults explain Cosmos in the diagram. Your agent webapp still decides where product state lives. Keep two planes honest. Keep one product API stable. Move only what you own.
