Work / Latvian Financial Analyst Agent
AI AgentsLatvian Financial Analyst Agent
An agentic AI system that answers plain-English questions about Latvian company accounts. It writes and runs its own SQL and Pandas analysis, checks data quality and reports the result in euros.
images/latvian-agent.svg01Problem
Company financial statements hold the answers to questions like “What was the average cash held in 2021?” or “How did total equity change between 2020 and 2021?” But getting those answers usually means knowing the schema, writing SQL, and handling messy values such as zeros, blanks and text-typed numbers.
I wanted an analyst you can simply ask. It should pick the right method, query the data and explain the result. It also needs to be safe to point at a real database.
02Data
The data is a local SQLite database of Latvian company financial statements in five linked tables:
- register: legal entity details, registration and location
- financial_statements: the master table linking each company to its reporting periods
- balance_sheets: assets, liabilities and equity
- income_statements: revenue, expenses and net income
- cash_flow_statements: operating, investing and financing cash flows
A JSON data dictionary describes every table and column. The agent reads it, so it reasons over real field meanings rather than guessing from column names.
03How it works
- LangGraph state machine: an
agentnode (GPT-5-mini, temperature 0) decides what to do next. Atoolsnode executes the decision, and the loop continues until the agent can answer. Rate-limit errors are retried up to five times. - MCP tool server: tools are exposed over the Model Context Protocol, keeping the agent and its capabilities separate. The tools are
sql_query,python_repl(Pandas),variance_analysis,data_quality_checkandvisualise_data. - Output: answers come back as markdown tables and a plain-language summary, through a CLI or an optional Streamlit UI.
04Guardrails
An LLM that writes SQL needs firm boundaries, so I built them into both the tools and the system prompt:
- Read-only by construction:
sql_queryrejects anything that isn't aSELECT. It also blocks INSERT, UPDATE, DELETE, DROP, ALTER, CREATE and REPLACE, even inside a SELECT. - Domain rules: the agent always filters out zero and NULL financials, casts values to REAL to avoid NaN, and reports in EUR.
- Skew-aware reporting: financial data is heavily skewed, so aggregations report both the average and the median.
- No invented numbers: the prompt tells the agent never to fill gaps with assumptions, and to run
data_quality_checkwhen a result looks suspicious.
if not stripped.upper().startswith("SELECT"):
return "Error: Only SELECT queries are permitted."
forbidden = ["INSERT", "UPDATE", "DELETE", "DROP", "ALTER", "CREATE", "REPLACE"]
if any(kw in upper_query for kw in forbidden):
return "Error: Query contains forbidden keywords."
05Engineering
- pytest coverage for the data loader, schema checks, variance analysis and data-quality tools.
- A Dockerfile, an entrypoint script and an EC2 setup script for running it as a service.
- Dependencies managed with
uvand a lockfile for reproducible installs.
06What I'd do next
The next step is an evaluation set: a list of questions with known answers, so accuracy can be measured rather than judged by eye. After that, I'd add query-cost limits and caching for repeated questions, and support for more models through the same tool interface.
Next project
Academic Literature Navigator →images/literature-navigator.svg