JDogo
Open playground

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)
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 rows

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 repliesask() does
A valid queryEvaluates it; route = "compute"
NEED_ROWSSends the rows with the prompt builder; reason need_rows
Something unparseableSends the rows; reason unparseable
A query that fails, such as an unknown column or a tieSends the rows; reason execution_error
A group-by or ranking, by defaultSends 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

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)

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"])

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 JDogo

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.