Fetching Live Stock Prices for LLMs
Connect your AI to real-time financial data using Dwizi tools.
Dwizi Team
Editorial
Fetching Live Stock Prices for LLMs
If you ask GPT-4 "What is the price of Apple stock?", it will likely give you a very polite, very useless answer:
"I'm sorry, but my training data only goes up to 2023. As of my last update, AAPL was around..."
This is the "Frozen in Time" problem. Training a Large Language Model takes months and costs millions of dollars. By the time the model is released, its knowledge of the world is already obsolete.
For history or coding questions, this is fine. The French Revolution hasn't changed much since 2023. But for finance? Yesterday's price is ancient history. Today's price is the only thing that matters.
The Solution: Real-Time Retrieval
We don't need to retrain the model every second. We just need to give it a pair of glasses.
We can build a tool called get_stock_price. When the user asks about a stock, the AI pauses, uses this tool to "look up" the current price on the internet, and then uses that fresh data to formulate its answer.
The Implementation
We will use a standard financial API. In this example, we'll pretend to use a generic provider, but the code works for Alpha Vantage, Yahoo Finance, or IEX Cloud.
/**
* Fetches the current stock price for a given symbol.
*
* Description for LLM: "Get the real-time price of a stock using its ticker symbol (e.g., AAPL)."
* Notice how simple the description is. We don't need to explain HTTP or JSON to the LLM.
* We just explain the *capability*.
*/
type Input = {
ticker: string;
};
export default async function getStockPrice({ ticker }: Input) {
// 1. The Key
// Financial data isn't free. We need an API key.
// Using Deno.env.get() keeps this key safe in Dwizi's vault.
const apiKey = Deno.env.get("FINANCE_API_KEY");
if (!apiKey) throw new Error("Missing FINANCE_API_KEY");
// 2. The Fetch
// We make a real HTTP request to the outside world.
const url = `https://api.financial-data-provider.com/v1/quote?symbol=${ticker}&apikey=${apiKey}`;
const response = await fetch(url);
// 3. Error Handling
// What if the ticker doesn't exist? What if the API is down?
// We catch these errors and return them as data.
// This allows the LLM to explain the problem to the user: "I couldn't find a stock named 'BLAH'."
if (!response.ok) {
return { error: `Failed to fetch data for ${ticker}` };
}
const data = await response.json();
// 4. Data Curation (Token Economy)
// APIs often return huge blobs of JSON with 50 fields.
// The LLM doesn't need "volume_weighted_average_price_15min".
// Passing too much data wastes "context tokens" (which costs money and speed).
// We extract ONLY what is needed.
return {
symbol: data.GlobalQuote["01. symbol"],
price: parseFloat(data.GlobalQuote["05. price"]),
changePercent: data.GlobalQuote["10. change percent"],
timestamp: new Date().toISOString()
};
}
The "Context Window" Economy
Look at step 4 in the code above. Why do we manually pick fields instead of just returning data?
LLMs have a limited "context window"—the amount of text they can read at once. If you dump a 50kb JSON file into the chat, you might push important instructions out of the window.
A good Dwizi tool is a curator. It goes out to the messy, noisy internet, finds the exact nugget of truth you need, and brings back only that nugget. It keeps your agent fast, cheap, and focused.
The Execution Story
User: "How are tech stocks doing today? Check Apple and Microsoft."
Agent Action: The agent realizes it needs two prices. It might call the tool twice in parallel (Dwizi supports this!).
Call 1: get_stock_price({ ticker: "AAPL" }) -> Returns $185.00
Call 2: get_stock_price({ ticker: "MSFT" }) -> Returns $420.00
Agent Response: "Tech stocks are looking strong. Apple is trading at $185.00, and Microsoft is at $420.00."
The user is happy. The model looks smart. And the "Frozen in Time" problem is solved.
Subscribe to Dwizi Blog
Get stories on the future of work, autonomous agents, and the infrastructure that powers them. No fluff.
We respect your inbox. Unsubscribe at any time.
Read Next
Project Management (Linear/Jira)
Stop manually creating tickets. Let the conversation become the ticket.
Currency Conversion (Determinism)
A simple tool that proves a big point: Why we need 'Islands of Truth' in a sea of hallucination.
The Junior Dev (GitHub)
Automating the first 15 minutes of every bug report. How to build an agent that triages issues.