r/SQL • u/abcd__dcba • Jun 10 '26
MySQL How would you store execution logs for a production Text-to-SQL agent?
I'm currently a 6th semester student and have been interning at a small product-based company 2 months.
The project I've been working on is a Text-to-SQL agent that queries the company's internal databases. The agent is built using LangGraph and currently consists of around 8 nodes (intentanalysis, table selector, SQL generation, validation, execution, response generation, etc.).
The next task I've been given is to design a MySQL schema for storing agent logs and execution history.
The application is multi-tenant, so every request is associated with a tenant and a user. My initial thought was to store fields like tenant_id, user_email, timestamps, etc. as columns and then store the complete agent output in a JSON column.
A simplified version of the metadata currently generated by the agent looks like this:
```json
{
"generated_sql": "SELECT p.id, p.name FROM company_product p ...",
"selected_tables": [
"company_product",
"company_product_category"
],
"execution_time_ms": 16.67,
"retry_count": 0,
"intent": "safe"
}
```
In addition to this metadata, I can also store intermediate outputs from individual LangGraph nodes if needed.
The requirements are not fully defined yet, but I can see a few potential use cases:
* Debugging incorrect SQL generation
* Investigating failed executions
* Tracking latency and performance
* Auditing what queries were generated and executed
* Future analytics (success rate, common failures, usage by tenant, etc.)
One concern is that the LangGraph workflow will likely evolve over time. Today it has 8 nodes, but that could change in the future, so I'd prefer a design that doesn't require schema changes every time the graph changes.
I'm considering a few approaches:
Store tenant/user metadata in columns and dump the entire agent response into a JSON column.
Store frequently queried fields (generated_sql, execution_time_ms, status, etc.) as columns while also storing the full JSON payload.
Create separate tables such as:
* agent_runs
* agent_node_logs
* execution_traces
For those who have built production LLM agents, LangGraph systems, or observability/logging infrastructure:
* How would you design the schema?
* Which fields would you normalize into columns versus keep in JSON?
* Would you store node-level execution data separately?
* Any lessons learned or pitfalls to avoid?
I don't have much experience designing schemas for observability/audit systems, so I'm trying to understand what scales well before implementing something that becomes difficult to maintain.
I'd appreciate examples of schemas or patterns you've used in production
6
u/alinroc SQL Server DBA Jun 10 '26
What will the logs be used for after they’re captured, and why does that have to be in a relational database?
2
u/Aggressive_Ad_5454 Jun 10 '26
You’re definitely on the right track. Take a step back and ask some questions.
One purpose might be figuring out which queries are slow. To support that use case you’ll want the query elapsed time, maybe in milliseconds, in a column of the log file.
Another purpose might be forensic: catching miscreant users stealing data they’re not supposed to see. For that you may want columns for data volume (number of rows) delivered, and maybe for the user’s privileges at the time of the logged operation. As well as time, and maybe the IP address from whence the user accessed the subsystem. Plan for IPv4 and IPv6.
A third purpose might be product management: analyzing how this agent is used in practice to look for opportunities to improve it.
You get the idea, ask your co-workers what they guess the logs will be used for. Try to imagine the queries you’ll need to get the log data for those purposes.
You won’t get all this perfect, but that’s OK.
1
u/Prestigious_Bench_96 Jun 10 '26
As a directional note, you'll often want to ultimately end up with a agentic flow that lets the agent return to previous steps (reselect tables based on query results, etc) - not sure of the langgraph details there - but I think in general if you have a request level table, a node level table, extract out core structured fields to the top level (tokens, duration, node type/tool calls; error/success) you'll be fine. Having a FK pointer to the raw trace/JSON blob table also gives you some good potential separataion.
TLDR; I think you're on the right track with 1, 2, and 3 - store that metadata with common fields, split that + raw traces across some lightly normalized tables that represent your common patterns (overall requset usage, who is doing what) - analysis of the details of request - and then raw logs for debugging/future structured data extraction.
1
u/chocolateAbuser Jun 10 '26
there's most probably a law in your country that says something about how long user data should be kept; apart from that the matter is also how much time could it pass before people will be aware that something's gone wrong and look at the logs? i would say at least a week, make it two, but if then data is used for optimization that's a task will happen pretty rarely, so we're talking like 6 months or more
it's unclear to me what you mean by the 8 nodes things, do they all work on the same request at the same time? if not then this shouldn't be an issue, just put a machine name column (or equivalent) and you can analyze if one is behaving differently from the others (or failing)
explicit columns are usually the ones that needs indexing/sorting and have to be part of a versioned model because that data has to be present (there are a lot of cases to analyze here and too few informations, as an example imagine the system can be used both by humans and from a service, you would likely keep the user in a way that it's possible to determine that, or for example for security since this processing probably has a cost you could think of keeping which ip (even if internal) the request is coming from)
as other pointed out the choice of mysql for this is kinda meh, in the sense that for example elastic is made for faster writing, and above all text search; mysql has a fulltext index, but it doesn't have as many options as lucene/elastic
there are many other things to think about, but we really need more data, it wold be really not that small of a project describing all that gets into this
and especially the important part would be how this is would be used, how much data do you anticipate to process and keep, how many people will be using it, and so on
1
u/hermoum75 Jun 10 '26
Having built similar agentic observability layers in production, your third option (hybrid approach with
agent_runsandagent_node_logs) is definitely the way to go.A flat JSON dump will quickly become a nightmare to query for latency analytics or tenant-wide audits as your scale grows.
Here is a proven pattern that works perfectly in MySQL without breaking when your LangGraph topology changes:
agent_runs(Top-level table): Extract core operational columns for quick indexing:id(UUID),tenant_id,user_id,status(success/failed),total_latency_ms, andcreated_at. You can store the final system response or prompt tokens here.agent_node_logs(Child table): Link this toagent_runsvia anagent_run_idForeign Key. Columns:id,node_name(e.g., 'table_selector'),status,latency_ms, and anode_payloadJSON column.By wrapping each node's inputs/outputs inside that JSON column in the child table, your graph can grow from 8 to 20 nodes tomorrow, or introduce conditional loops, and your MySQL schema won't care. You still get clean, indexable metrics on tenant usage and top-level failures, while keeping the flexibility of a document store for deep debugging.
One major pitfall to avoid since you are on MySQL: log tables grow fast. Make sure to include a clear retention/purging strategy from day one based on
created_at(e.g., partitioning or a cron job deleting logs older than 30 days), otherwise your indexes will eventually kill performance.1
u/Ifuqaround Jun 10 '26
Why are you building this shit for them as an intern?
sigh
I'm not building production agents for some small product company lol.
1
u/FarRub2855 Jun 11 '26
As someone on the business side your instinct to tag everything by tenant ID is spot on. Just make sure its easy to query which clients are hitting the most errors, because the accounts team will definately want to know who is struggling before a renewal call.
1
u/datasmithing_holly Jun 12 '26
Have you considered the open source version of MLflow? Does lots of the tracing and has loads of OTEL integrations
1
u/CasteliaLyon Jun 12 '26
Consider using existing oss llm observability frameworks like opik, they do most of the heavy lifting when it comes to logging. It even comes with a wrapper for langgraph
1
u/[deleted] Jun 10 '26
[removed] — view removed comment