Cookbook: Financial Analyst
Build an agent that can fetch real-time stock data, analyze trends, and generate PDF reports.
Overview#
This recipe demonstrates how to chain multiple tools together: an API fetcher, a data analyzer, and a file generator.
Input
"Analyze AAPL and MSFT performance over the last month and summarize the key differences."
➔
Output
"Here is the comparison table and a downloadable PDF report..."
Ingredients#
- Akios SDK for the agent loop.
- Yahoo Finance API (or mock) for data.
- PDFKit for report generation.
- Zod for schema validation.
1
Create the Stock Tool
tools/stock.ts
import { Tool } from '@akios/sdk'
import { z } from 'zod'
export const stockTool = new Tool({
name: 'get_stock_history',
description: 'Fetch historical daily prices for a ticker symbol.',
schema: z.object({
symbol: z.string().describe('The stock ticker (e.g. AAPL)'),
days: z.number().default(30)
}),
execute: async ({ symbol, days }) => {
// In production, call a real API like AlphaVantage or Yahoo
// Here we mock it for the demo
const mockData = Array.from({ length: days }).map((_, i) => ({
date: new Date(Date.now() - i * 86400000).toISOString().split('T')[0],
price: 150 + Math.random() * 20
}))
return JSON.stringify({ symbol, history: mockData })
}
})2
Create the Analysis Tool
tools/analyze.ts
import { Tool } from '@akios/sdk'
import { z } from 'zod'
export const analysisTool = new Tool({
name: 'calculate_volatility',
description: 'Calculate the standard deviation of a price array.',
schema: z.object({
prices: z.array(z.number())
}),
execute: async ({ prices }) => {
const mean = prices.reduce((a, b) => a + b) / prices.length
const variance = prices.reduce((a, b) => a + Math.pow(b - mean, 2), 0) / prices.length
return JSON.stringify({ volatility: Math.sqrt(variance) })
}
})3
Assemble the Agent
agent.ts
import { Agent } from '@akios/sdk'
import { stockTool, analysisTool } from './tools'
const analyst = new Agent({
name: 'WallStreetBot',
model: 'gpt-4o',
systemPrompt: `You are a financial analyst.
1. Fetch stock data for the requested companies.
2. Calculate volatility for each.
3. Provide a recommendation based on risk (high volatility = high risk).`,
tools: [stockTool, analysisTool]
})
// Run it
const result = await analyst.run("Compare risk for AAPL vs TSLA")
console.log(result.output)Going Further
Add a `generate_chart` tool using a library like `chart.js` or `matplotlib` (if using python backend) to return image URLs that the agent can display.