A comprehensive, beginner-friendly guide to AI tool calling patterns with runnable examples in JavaScript/TypeScript and Python.
- Basic Single Tool Calling: How to call one tool at a time
- Parallel Tool Calling: Execute multiple tools simultaneously for efficiency
- Sequential Chaining: Chain tool calls where outputs feed into subsequent calls
- Real-World Applications: Practical examples combining multiple patterns
ai-tool-calling-examples/
βββ javascript/ # JavaScript/TypeScript examples
β βββ 01-basic-single-tool/
β βββ 02-parallel-tools/
β βββ 03-sequential-chaining/
β βββ 04-real-world-examples/
β
βββ python/ # Python examples
βββ 01-basic-single-tool/
βββ 02-parallel-tools/
βββ 03-sequential-chaining/
βββ 04-real-world-examples/
Perfect for learning the patterns without needing an API key!
JavaScript/TypeScript:
cd javascript
npm install
node 01-basic-single-tool/simple-tool-call.jsPython:
cd python
pip install -r requirements.txt
python 01-basic-single-tool/simple_tool_call.pyTo use actual AI models for tool calling:
-
Copy the environment template:
cp .env.example .env
-
Add your API key to
.env:# For OpenAI (get key from https://platform.openai.com/api-keys) OPENAI_API_KEY=sk-... AI_PROVIDER=openai # OR for Anthropic (get key from https://console.anthropic.com/settings/keys) ANTHROPIC_API_KEY=sk-ant-... AI_PROVIDER=anthropic
-
Run the real AI examples:
JavaScript:
cd javascript npm install node 01-basic-single-tool/with-real-ai.js node 02-parallel-tools/with-real-ai.jsPython:
cd python pip install -r requirements.txt python 01-basic-single-tool/with_real_ai.py
Use local open-source models for FREE - no API key needed!
Quick start with Ollama:
# 1. Install Ollama
curl -fsSL https://ollama.com/install.sh | sh
# 2. Pull a model
ollama pull llama3.2:latest
# 3. Configure .env
AI_PROVIDER=openai
OPENAI_BASE_URL=http://localhost:11434/v1
OPENAI_API_KEY=ollama
OPENAI_MODEL=llama3.2:latest
# 4. Run examples!
cd javascript
node 01-basic-single-tool/with-real-ai.jsπ Full guide: See OPENAI_COMPATIBLE.md for detailed setup with Ollama, LM Studio, Azure OpenAI, and more.
Learn the fundamentals of tool calling with a simple calculator example.
- Use Case: Performing basic arithmetic operations
- Pattern: One tool, one call
- Best For: Understanding tool definition and execution
Call multiple independent tools simultaneously for better performance.
- Use Case: Getting weather and time from different sources
- Pattern: Multiple independent tool calls
- Best For: Optimizing performance when tools don't depend on each other
Chain multiple tool calls where each depends on the previous result.
- Use Case: User lookup β preferences β personalized recommendations
- Pattern: Output of one tool feeds into the next
- Best For: Complex workflows with dependencies
Complete applications combining multiple patterns.
- Web Research Agent: Search, fetch, and summarize web content
- Data Processing Pipeline: ETL operations with multiple steps
- Content Analyzer: Multi-step content analysis and reporting
- Start with
01-basic-single-toolto understand fundamentals - Move to
02-parallel-toolsto learn optimization techniques - Progress to
03-sequential-chainingfor complex workflows - Explore
04-real-world-examplesfor practical applications
Learn the patterns without API keys using simulated AI responses:
simple-tool-call.js/py- Basic conceptsconcurrent-calls.js/py- Parallel patternschained-tools.js/py- Sequential patterns
See how it works with actual AI providers:
with-real-ai.js/py- OpenAI or Anthropic integration- Real tool calling with GPT-4 or Claude
- Production-ready code examples
The patterns work with:
- β OpenAI Function Calling (GPT-4, GPT-3.5)
- β Anthropic Tool Use (Claude 3.5 Sonnet)
- β
OpenAI-Compatible APIs:
- Azure OpenAI
- Ollama (Local - Llama, Mistral, etc.)
- LM Studio (Local with GUI)
- LocalAI, vLLM, and more
- β LangChain Tools
- β Custom implementations
π‘ Want to use local AI models? See OPENAI_COMPATIBLE.md for setup with Ollama, LM Studio, and others!
Tools are functions the AI can call to perform specific actions:
{
name: "calculator",
description: "Performs arithmetic operations",
parameters: {
operation: "add | subtract | multiply | divide",
a: "first number",
b: "second number"
}
}- AI receives user request
- AI determines which tools to call
- Tools execute and return results
- AI processes results and responds to user
- Single: Simple, straightforward tasks
- Parallel: Multiple independent operations
- Sequential: Complex workflows with dependencies
- Combined: Real-world applications requiring flexibility
Feel free to add more examples or improve existing ones!
MIT License - feel free to use these examples in your projects.