AI prompts and ask()
Two tools. The prompt builder puts your data in front of a model in the fewest tokens that still read well. ask() takes the arithmetic away from the model entirely.
The prompt builder
Use it when the model has to read the data: finding records, summarising, explaining. It writes a one-line instruction, the data, and your question.
prompt = jdogo.format_query_prompt("Which orders shipped late?", orders)const prompt = formatQueryPrompt("Which orders shipped late?", orders);String prompt = JDogoAI.formatQueryPrompt("Which orders shipped late?", orders);Data is a CSV table of 3 rows: the first line (after '@') names the columns, then one row per line. Data (3 rows): @id,name,role,active 1001,Ada Lovelace,admin,true 1002,Grace Hopper,editor,true 1003,Alan Turing,viewer,false Question: Which users are editors?
The builder chooses the layout per payload. It writes ids in full rather than as steps, keeps any value your question quotes exactly as written, and uses record lines (@:) for wide, sparse tables. All three SDKs produce byte-identical prompts.
ask(): the model writes the query, you compute the answer
Models are unreliable at counting and adding up rows, in any format. ask() sends a short schema card instead of the rows. The model replies with a one-line query, and the SDK evaluates it over your data locally.
from jdogo import ask
result = ask("What is the total value of open orders?", orders, llm=call_model)
result.answer # 48210.5
result.route # "compute" or "rows"
result.expression # 'sum(total) where status = "open"'
result.fallback_used # True if ask() had to send the rowsimport { ask } from "jdogo";
const r = await ask("What is the total value of open orders?", orders, callModel);
r.answer; r.route; r.expression; r.fallbackUsed; r.fallbackReason;JDogoAI.AskResult r = JDogoAI.ask("What is the total value of open orders?", orders, prompt -> callModel(prompt));Your llm function takes the prompt text and returns the model's reply as a string (or an object with the content and token usage).
What the model sees
Table: 2000 rows Columns: - id (number): 1001..2999 [e.g. 1001, 1008, 1015] - department (string): ["Engineering", "Finance", "Legal", "Marketing", "Sales"] - active (boolean): [true, false]
A few hundred tokens, whatever the table size.
How it routes
| Model replies | ask() does |
|---|---|
| A valid query | Evaluates it; route = "compute" |
NEED_ROWS | Sends the rows with the prompt builder; reason need_rows |
| Something unparseable | Sends the rows; reason unparseable |
| A query that fails, such as an unknown column or a tie | Sends the rows; reason execution_error |
| A group-by or ranking, by default | Sends the rows; reason group_by_disabled |
Pass fallback_enabled=False (fallbackEnabled: false) to get the failure back instead of a second model call.
Why it's safe
Model text is never executed. Queries are parsed by a small hand-written parser into a typed tree and evaluated by a tree walker. No eval, no SQL, no shell. Expressions are capped at 2,000 characters and 32 levels of nesting.
Measured results
- Compute mode: 150 of 150 counting and totals questions right across three models, against 74 of 150 when the same models read the rows.
- Reading rows: models answer about equally well from JSON, TOON and JDogo. JDogo's win there is cost: the same answers for about half the prompt tokens.
Group-by, rankings and percentages
The evaluator supports group-by, top N, rank, percent, ratio and median in all three SDKs. They stay off in ask()'s default instruction until a live test validates them. To use them now:
ask(question, data, llm=call_model, allow_group_by=True)
await ask(question, data, callModel, { allowGroupBy: true })JDogoAI.ask(question, data, llm, true, true) // fallbackEnabled, allowGroupBy
The full query language is on Compute expressions.
Is JDogo worth it for this data?
A model needs a short instruction to read JDogo, so for a row or two plain JSON can be cheaper. optimize() measures both and returns the cheaper text, together with a receipt that shows the numbers behind the choice:
import jdogo text, receipt = jdogo.optimize(rows) print(receipt["chosen"], receipt["reason"], receipt["savedTokens"])
import { optimize } from "jdogo";
const { text, receipt } = optimize(rows);
console.log(receipt.chosen, receipt.reason, receipt.savedTokens);OptimizedPayload result = JDogoOptimizer.create().optimize(rows); String text = result.content(); Map<String, Object> receipt = result.receipt();
- Net, not gross. JDogo is charged its text plus the instruction for the features it uses, and is chosen only when that total is smaller than JSON.
savedTokensis the saving after the instruction. - Never lossy. A format is only offered if decoding it gives back the data. The result is JDogo or compact JSON, and every JDogo reader accepts both.
- Reasons are the same in all three SDKs:
fewest tokens,instruction costs more than it saves,json has fewer tokens, orno other format round-trips. - Tokens are measured with o200k (or cl100k for GPT-4) in Java and in Python with
tiktokeninstalled; the receipt'stokenizerfield says which. TypeScript estimates them (within about 15%) unless you pass{ countTokens }. - The playground's Encode tool shows the same calculation for the text it produces.
Tool results and agents
Return tool output as JDogo to keep agent context small:
from jdogo import jdogo_tool
@jdogo_tool
def list_orders(customer_id: str):
return db.orders(customer_id) # returned to the model as JDogoimport { createJDogoTool } from "jdogo";
export const listOrders = createJDogoTool({
name: "listOrders",
description: "Orders for one customer",
execute: async ({ customerId }) => db.orders(customerId),
});Agents in Claude Desktop, Cursor and other MCP clients can use all of this through the JDogo MCP server, including exact answers with jdogo_compute.
For local models, jdogo gbnf writes a llama.cpp grammar that forces output into valid JDogo rows.