Build custom AI agents with the Claude Code SDK, it provides automatic prompt caching and performance optimizations, rich tool ecosystems, advanced permissions and production essentials such as error handling, session management and monitoring. Key Advantage: You get Claude Code’s battle-tested tool ecosystem (file ops, bash, web search, etc.) plus the ability to add your own tools and system prompts.
The Python SDK provides two primary interfaces:
- The
ClaudeSDKClientclass (Recommended): Best for streaming responses, multi-turn conversations, and interactive applications: - The
queryfunction: For simple, one-shot queries:
from claude_code_sdk import query, ClaudeCodeOptions
async for message in query(
prompt="Analyze system performance",
options=ClaudeCodeOptions(system_prompt="You are a performance engineer")
):
if type(message).__name__ == "ResultMessage":
print(message.result)
For example, i want to build a Finance agent, here is the codes
Basic Finance Agent
// TypeScript SDK
const agent = new ClaudeCodeAgent({
systemPrompt: `You are a senior financial analyst with expertise in:
- Portfolio management and risk assessment
- Financial statement analysis
- Market research and valuation
- Regulatory compliance (SEC, FINRA)
Always provide data-driven insights with proper risk disclaimers.`
});
Specialized Finance Agents
Investment Research Agent:
claude --append-system-prompt "You are a CFA charterholder specializing in equity
research. Analyze companies using DCF, comparable analysis, and technical
indicators. Always cite data sources."
Risk Management Agent:
# Python SDK
agent = ClaudeCode(
system_prompt="""You are a quantitative risk analyst. Focus on:
- VaR calculations and stress testing
- Portfolio optimization
- Regulatory capital requirements
- Market risk metrics
Use statistical models and provide confidence intervals."""
)
Trading Strategy Agent:
const tradingAgent = new ClaudeCodeAgent({
systemPrompt: `You are an algorithmic trading strategist. Analyze:
- Technical patterns and momentum indicators
- Market microstructure
- Backtesting methodologies
- Risk-adjusted returns
Never provide investment advice - only analytical insights.`
});
And I can feed domain-specific documents to your finance agent in several ways:
1. Direct File Access
// Agent can read your finance documents
const agent = new ClaudeCodeAgent({
systemPrompt: `You are a finance analyst. Reference these documents:
- /data/company_financials.pdf
- /models/valuation_models.xlsx
- /docs/trading_manual.md
Always cite which document you're referencing.`
});
// Agent uses Read tool to access files as needed
await agent.run("Analyze Q3 earnings using our standard model");
2. MCP Servers for Data Sources
Claude Code supports MCP (Model Context Protocol) servers to connect external
data:
// .claude/settings.json
{
"mcpServers": {
"finance-data": {
"command": "npx",
"args": ["@company/finance-mcp-server"],
"env": {
"API_KEY": "your_key"
}
}
}
}
3. Custom Tools in SDK
const financeAgent = new ClaudeCodeAgent({
tools: [
{
name: "query_financial_database",
description: "Query internal financial models and data",
handler: async (params) => {
// Connect to your finance database/models
return await queryFinanceDB(params.query);
}
}
]
});
4. Pre-loaded Context
# Load documents into agent context
with open('finance_manual.md', 'r') as f:
manual = f.read()
agent = ClaudeCode(
system_prompt=f"""You are a finance analyst. Use this manual as reference:
{manual}
Follow all procedures outlined in the manual."""
)
The agent can then access, search, and reference your specific finance documents,
models, and data sources during analysis.
It’s required to use the API key, login console here.