Quaintitative

AI for Investing

The four agent patterns for investing

Four patterns, from the smallest to the whole thing. Each is the same four components - model, tools, instructions, memory - used a little harder.

Tool calling

One question, one tool, one answer. You write a function that returns the current price for a ticker, mark it as a tool, give it to an agent, and ask in natural language: "what is Apple's current price?" The agent works out it needs the price tool with Apple, the tool returns a number, and the agent writes a sentence around it. The model did not guess the price. It called a function that returned a specific value. That is the point of the whole thing. Turn on the verbose output and every step is visible, which for finance matters: for compliance, for debugging, and for deciding how much to trust it.

ReAct

Real questions are not one tool. "Compare Apple and NVIDIA. Which is trading closer to its 52-week high?" needs two prices, two highs, two percentages, and a comparison. ReAct, short for Reason plus Act, is the agent alternating between working out what it needs next and going to get it: think what do I need, act by calling a tool, observe the result, then back to think, until the question is answered.

The trace for that question: Apple's price first, then Apple's 52-week high, then NVIDIA's price, then NVIDIA's high, and finally the comparison, Apple at 89.4% of its high, NVIDIA at 89.9%, so NVIDIA is closer. Ask about three stocks and it adds steps without breaking. It handles the edges too: ask for the range of a ticker that does not exist and the tools return zeros, and the agent notices both are zero, concludes the ticker was not found, and says so rather than reporting a range of zero to zero. Every step is visible, which you need in finance, where you cannot run investment decisions through a black box. One caveat: model output is not deterministic, so run it twice and the wording, and even the number of steps, can differ. The pattern is what stays the same.

CodeAct

"Calculate the Sharpe ratio for these monthly returns." There is no Sharpe tool, and you cannot pre-build a tool for every calculation, because tomorrow's question is the Sortino ratio, or maximum drawdown, or a metric nobody has named. So let the agent write the code. That is CodeAct: the agent writes a short piece of Python, the system runs it, and the agent reads the result.

Why code and not words? Because language-based arithmetic is unreliable: a model might get a Sharpe calculation approximately right, or quietly drop a step. Code either runs or throws an error. There is no "approximately 4" in Python. The model brings the knowledge, the formula and what a decent ratio looks like, and Python brings the computation. The obvious risk is that the agent is writing and running code, so the basic control is a whitelist of what it may import, such as numpy and pandas and nothing that touches the filesystem or the network. Production adds sandboxing, time limits, and a log of every code block.

Tools for data, code for analysis.

Orchestration

Real applications need all three at once. A portfolio optimiser might hold six tools, each doing one job: fetch historical prices; optimise for maximum Sharpe; optimise for minimum volatility; optimise for a target return; convert weights into share counts for a given sum; compare two strategies. Plus code execution for anything custom, and memory for follow-ups.

Ask it: "I have $50,000 for five stocks. Compare max Sharpe against minimum volatility, show me how many shares to buy under each, and tell me which you would recommend for someone five years from retirement." The agent compares the strategies with one tool, computes the share allocation under each with another, and writes up two tables with expected return and volatility, and a recommendation: minimum volatility, because five years from retirement you cannot afford the drawdown the other portfolio could produce. Then the follow-up with memory on, "what if I removed NVDA?", and it reruns without repeating anything.

Now the whole structure is visible. The model takes in the request and chooses the tools. The real work is the optimiser, the market data, the share calculation. Around them sit the input checks, error handling and logging. The model then turns the numbers into a recommendation and handles the follow-up. That is how every serious finance agent is built, whatever framework is underneath.

Keep going

These patterns are proven in a sandbox. Taking them to real money is the production gap. Start at AI for investing for the whole picture, or get the full book, AI Agents for Investing, which builds each pattern as a runnable notebook. I am publishing more in the coming weeks. Subscribe to get it.

Subscribe for updates