Agent Memory: SQL and SPARQL Answer Correctly 98% of the Time, but SPARQL Costs 7–145% More
An agent needs memory, and you can store it in tables (queried with SQL) or in a knowledge graph (queried with SPARQL). The author asked 15 questions about a single article to three open models and ran 405 trials per query language. SQL answered correctly 97.8% of the time, and SPARQL 98.5%. But SPARQL costs 7–145% more, because the models make more syntax errors and have to re-query the database more often. If your data already sits in tables with a documented schema, start with SQL.
AI-processed from Habr AI; edited by Hamidun News
When an agent needs memory so it doesn't make up answers, a developer faces a simple choice: store knowledge in ordinary tables and query them with SQL, or build a knowledge graph and query it with SPARQL (a graph query language similar to SQL, but working with relationships between objects). Usually the choice is made based on internet posts, without measurement. The author of a Habr analysis decided to measure. The result: both approaches answer correctly in roughly 98% of cases, but SPARQL costs 7–145% more depending on the model, because models make more mistakes in its syntax and make extra attempts.
Where the debate came from
The starting point was a study by Sequeda and co-authors. In it, GPT-4 answered questions about an enterprise SQL database correctly 16% of the time, and about a knowledge graph built on the same database 54% of the time. But the comparison was not fair: the graph had an ontology (a detailed description of what each type of object and relationship means), while the SQL schema had only the types and names of tables and columns. The author described both forms with equal detail and tested again.
How the experiment works
Data from a single article about PostgreSQL and ClickHouse was broken down by a neural network into entities, theses, measurements, quotes, and processes. This produced 10,154 quads (graph records of the form "subject, property, value, graph") across 37 graphs. These were then converted into 34 PostgreSQL tables: a graph node became a row, and a property became a column. Through an R2RML mapping, the tables can be assembled back into the original graph. The memory description in the system prompt is identical for both variants and runs 15–17 thousand tokens.
The agent is written in Java with langchain4j. It works in a loop of "model, tools, model" with no more than 16 model calls and 12 memory queries. Memory is connected through an MCP server, and the agent has three tools: sql_query, sparql_query, and submit_answer for the final answer with a citation. The database is read-only, the query timeout is 5 seconds, and responses are truncated to 200 rows.
There are 15 questions across seven types: facts, relationships, aggregates, chains of arguments, disputes, quotations, and unanswerable questions. Checking is done by rules, without an LLM judge. The models chosen have open weights: DeepSeek V4.1 Flash, Gemma 4 31B, and Qwen 3.8 27B. Each combination was repeated three times: 3 models, 3 conditions (SQL only, SPARQL only, both tools), 15 questions, 3 repetitions, for a total of 405 runs per database language. All runs took 10 minutes.
Results: equal accuracy, different bills
On the Russian database, SQL scored 97.8% correct answers, while SPARQL and the mode with a choice between the two tools scored 98.5%. On the English translation: 98.5%, 99.3%, and 97.8%. DeepSeek answered correctly all 270 times. Of the 13 errors by Gemma and Qwen, 12 came from two questions: the one about chains of arguments (P1) and the one about counting dialogues in a dispute (D3).
The cost difference is significant. Database errors per call are 2–3 times higher with SPARQL: 5.9% versus 2.8% on the Russian database, and 7.3% versus 2.5% on the English one. Every run gets more expensive: DeepSeek by 11–25%, Gemma by 7–10%, Qwen by 63–145%. But agents recover well on their own: of 134 runs where the database returned an error, 129 ended with a correct answer. The model reads the error text and rewrites the query.
A separate lesson concerns prompt caching. Per run, the agent calls the model 4–7 times and each time resends the same 15–17 thousand tokens. DeepInfra caches DeepSeek (97% of input tokens are served from cache) and does not cache Gemma. As a result, Gemma costs 25–27 times more for the same answers: $0.0234 per run versus $0.0009 for DeepSeek. Qwen costs $0.0077 per run on the Russian database. Median response time on the Russian database: 13 seconds for DeepSeek, 38 for Qwen, and 57 for Gemma.
What to do in practice
If the data already lives in a relational database and the schema is documented, SQL delivers the same accuracy for less. A graph is worth building where relationships matter and it isn't clear which table to open. The prompt-caching provider affects the bill more than the choice of query language. And since the agent fixes its own errors, pass it the database's error text instead of cutting the task short.
The limitations are honest: one article, 15 questions, one author. The models were called through OpenRouter rather than locally, because even a 27B model in 4-bit quantization can't run on a laptop in a reasonable time. The author adjusted the checking format and prompts during the work, for example by adding default SPARQL prefixes. So the figures should be treated as a guide, and the measurement method can be repeated on your own data. Prices in the article are as of October 27, 2026.
- Nov 21, 2026MLCommons Starts Measuring AI APIs Under Real Load: 7 Runs for Agents
- Nov 21, 2026For Three Days, a Hacked DeepSeek Answered Through OpenRouter: Refusals Fell from 83–98% to 1%
- Nov 21, 2026Alisa, ChatGPT and Perplexity Cite Only 34 of 309 Article Platforms
- Nov 21, 2026CoreWeave Benchmarks Vera Rubin: Up to 10x More Tokens per Megawatt Than GB200
Key Questions Answered
Where the debate came from?
The starting point was a study by Sequeda and co-authors. In it, GPT-4 answered questions about an enterprise SQL database correctly 16% of the time, and about a knowledge graph built on the same database 54% of the time. But the comparison was not fair: the graph had an ontology (a detailed description of what each type of object and relationship means), while the SQL schema had only the types and names of tables and columns. The author described both forms with equal detail and tested again.
How the experiment works?
Data from a single article about PostgreSQL and ClickHouse was broken down by a neural network into entities, theses, measurements, quotes, and processes. This produced 10,154 quads (graph records of the form "subject, property, value, graph") across 37 graphs. These were then converted into 34 PostgreSQL tables: a graph node became a row, and a property became a column. Through an R2RML mapping, the tables can be assembled back into the original graph.
What are the key details on Results: equal accuracy, different bills?
On the Russian database, SQL scored 97.8% correct answers, while SPARQL and the mode with a choice between the two tools scored 98.5%. On the English translation: 98.5%, 99.3%, and 97.8%. DeepSeek answered correctly all 270 times. Of the 13 errors by Gemma and Qwen, 12 came from two questions: the one about chains of arguments (P1) and the one about counting dialogues in a dispute (D3). The cost difference is significant.
Test Frontier LLMs & Autonomous Agents in One Workspace
Run Claude Opus 5.5, GPT-6, Gemini 3.1, and DeepSeek side-by-side in Nomad AI (nomadnet.ai) — unified model routing, deep research, and multimodal workflows under one subscription.
The AI world, distilled — once a week
Seven stories that actually mattered, hand-picked. No noise, no reposts, no press releases.
Done! Check your inbox for a confirmation.