Build an analyst that writes a daily report
This recipe builds a business analyst: it reads your operating database only and writes a report on a schedule, conclusion first, then the numbers, then anomalies. It is layer ④ of the business operations hub, and reads the pages and sync_runs tables built in Sync a web back office into a database.
Prerequisites
- The database
DBfrom the previous recipe, synced at least once; reuse itsapifunction and environment variables. - The model is
deepseek-flash: a report runs every day and an inexpensive model is enough; see public pricing.
1. Write the system prompt
Save the following as analyst.md. The data dictionary, the definitions and the rules are where the report’s quality comes from: units, what “today” means and how failures are counted must be written down, not left to the model.
You are the business analyst. You only read the bound operating database, querying it with codemode (praxis.database.table.rest.retrieve).
Data dictionary:- pages: one row per source record. path (unique key), title, url, snippet, synced_at (time of the latest sync, UTC).- sync_runs: one row per sync. run_id (unique), started_at, finished_at, status (succeeded / failed), rows (rows in this run), error.
Definitions:- "Today's data" is the rows written by the latest successful sync: synced_at equals the started_at of the latest succeeded row in sync_runs.- Whether syncing is healthy: any failed row in sync_runs in the last 24 hours, and the time of the latest succeeded row. Times in sync_runs are UTC; convert them to Beijing time in the report.
Rules:- Read only; never write to any table.- When the data is not enough to answer, say what is missing; do not guess or invent.- Report format: one sentence of conclusion, then the numbers (total rows, latest successful sync time, failures in the last 24 hours), then anomalies; write "no anomalies" when there are none.2. Create the agent, bound read-only to the database
A service agent’s bindings limit what its codemode can reach. access: "read_only" lets it read only: a write returns 403 BINDING_READ_ONLY.
AGENT=$(api /agent/v1/agents --data "$(jq -n --rawfile sys analyst.md --arg db "$DB" '{ name:"Business analyst", kind:"service", model:"deepseek-flash", system:$sys, bindings:[{type:"database", id:$db, access:"read_only", instructions:"Operating data: pages and sync_runs"}]}')")AGENT_ID=$(jq -er .id <<< "$AGENT"); AGENT_VERSION=$(jq -er .version <<< "$AGENT")3. Write the report on a schedule
A deployment is a fixed set of session parameters with an optional schedule: when due, it creates a new session and sends the messages in initial_events. environment_id is required; use node22.
DEPLOYMENT=$(api /agent/v1/deployments --data "$(jq -n --arg a "$AGENT_ID" --argjson v "$AGENT_VERSION" '{ name:"Daily report", agent:{type:"agent", id:$a, version:$v}, environment_id:"node22", initial_events:[{type:"user.message", content:[{type:"text", text:"Write today's business report."}]}], schedule:{type:"cron", expression:"30 8 * * *", timezone:"Asia/Shanghai"}}')" | jq -er .id)To see it now instead of tomorrow, run it once and read the last reply of that session:
SESSION=$(api /agent/v1/deployments/$DEPLOYMENT/run -X POST | jq -er .session_id)until [ "$(api /agent/v1/sessions/$SESSION | jq -r .status)" = idle ]; do sleep 15; doneapi "/agent/v1/sessions/$SESSION/events?limit=100" \ | jq -r '[.data[] | select(.type == "agent.message") | .content[] | select(.type == "text") | .text] | last'Expected: a report whose first sentence is the conclusion, whose numbers agree with sync_runs, and whose last part lists anomalies or says there are none. Check the first report by hand, item by item.
4. After changing the prompt, update the deployment
A deployment stays on the agent version it was created with. Changing the prompt makes a new agent version, but the deployment keeps running the old one until you point it at the new version:
NEW=$(api /agent/v1/agents/$AGENT_ID --data "$(jq -n --rawfile sys analyst.md --argjson v "$AGENT_VERSION" '{version:$v, system:$sys}')" | jq -er .version)api /agent/v1/deployments/$DEPLOYMENT --data "$(jq -n --arg a "$AGENT_ID" --argjson v "$NEW" '{agent:{type:"agent", id:$a, version:$v}}')" | jq '.agent'Expected: the printed agent.version is the new version.
Common mistakes
| Symptom | Cause and fix |
|---|---|
| The report says “I counted it this way, correct me if wrong” | A definition in the prompt is ambiguous. The first prompt in the run below said “synced_at after the latest successful sync”; the model saw it should be “equal to” and made its own assumption. With “equal to the started_at of the latest succeeded row” it no longer does |
| Times do not match | The tables are in UTC. Ask in the prompt for Beijing time in the report |
| The report did not change after a prompt change | The deployment still points at the old version; update it as in step 4 |
Creating the deployment returns environment_id is required or light is retired | Pass environment_id: "node22" |
| The agent says it cannot find the tables | The bound database id is wrong or missing; check the agent’s bindings |
Run record
2026-11-25, Develop, model deepseek-flash, on the data from the previous recipe’s run (19 rows, 4 run records with 1 failure). The prompt used was the Chinese version of the one above.
| Item | Result |
|---|---|
| Scheduled run | a test deployment firing every minute was due at 05:33:00Z and created a session at 05:33:26Z; the agent read both tables and wrote the report; the deployment was then paused |
| First report | conclusion, numbers and anomalies all present, including that the HTTP 404 failure at 13:28 was covered by the successful sync at 13:30; it made an assumption about “today’s data” (see common mistakes) |
| After the prompt change | the agent moved to version 2 and the deployment to version 2 at 08:30 daily; a manual run counted 19 rows by the new definition, in Beijing time, with no assumption |
| Records | agent agent_96f87ed9-0d39-4402-8c47-b3c94df4f93d v2, deployment depl_7117194ee4e24828abbe50d20b8ec16d, sessions sesn_fc6bea2d0d194df39d79f09e1da31ce5, sesn_48df27aa311d428abd2d1863aa88dea3 |
Next steps
When the analyst finds a problem, have it file an action request that runs after your approval: see Carry out an action after approval and read it back.