Using APIs inside agents
๐ Introduction โ APIs Give Agents Superpowers!
AI Agent brain irukku (LLM), but hands irukkaadhu โ real world la enna pannum?
APIs are those hands! ๐คฒ
Without APIs: Agent just text generate pannum
With APIs: Agent email send pannum, data fetch pannum, payments process pannum, bookings confirm pannum!
| Agent Alone | Agent + APIs |
|---|---|
| "Here are flight options" | Actually books the flight โ๏ธ |
| "You should send an email" | Sends the email ๐ง |
| "Current weather might be..." | Gets real-time weather ๐ค๏ธ |
| "You could check your calendar" | Reads your actual calendar ๐ |
APIs transform agents from advisors to executors! ๐ช
๐ API Fundamentals for Agents
API Types agents commonly use:
| Type | Protocol | Format | Example |
|---|---|---|---|
| **REST** | HTTP | JSON | Most web APIs |
| **GraphQL** | HTTP | JSON | GitHub, Shopify |
| **WebSocket** | WS | JSON/Binary | Real-time data |
| **gRPC** | HTTP/2 | Protocol Buffers | Microservices |
REST API Basics (most common):
HTTP Status Codes agents should handle:
| Code | Meaning | Agent Action |
|---|---|---|
| 200 | Success โ | Process response |
| 400 | Bad request | Fix parameters, retry |
| 401 | Unauthorized | Refresh auth token |
| 404 | Not found | Try alternative |
| 429 | Rate limited | Wait and retry |
| 500 | Server error | Retry with backoff |
๐ง How Agents Use APIs โ Function Calling
Modern LLMs function calling feature irukku โ agent automatically correct API call pannum!
How it works:
Step 1: Define available tools (APIs)
Step 2: User gives request
"Book me a flight to Bangalore tomorrow"
Step 3: LLM decides which function to call
Step 4: Agent executes the API call
Step 5: Response goes back to LLM for interpretation
This is how ALL modern AI agents work! ๐ง
๐๏ธ API Integration Architecture
```
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ USER REQUEST โ
โ "What's the weather in Chennai?" โ
โโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ๐ง LLM REASONING โ
โ "I need to call weather API" โ
โ โ Function: get_weather("Chennai") โ
โโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ๐ง API EXECUTION LAYER โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ 1. Validate parameters โ โ
โ โ 2. Add authentication โ โ
โ โ 3. Make HTTP request โ โ
โ โ 4. Handle errors/retries โ โ
โ โ 5. Parse response โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ๐ EXTERNAL API โ
โ api.openweathermap.org/weather โ
โ Response: {"temp": 32, "desc": "sunny"}โ
โโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ๐ง LLM INTERPRETATION โ
โ "Chennai la ippo 32ยฐC, sunny day! โ๏ธ" โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
```๐ Authentication Methods
APIs access panna authentication venum:
| Method | How It Works | Security | Example |
|---|---|---|---|
| **API Key** | Key in header/query | Basic | OpenWeather |
| **Bearer Token** | JWT token in header | Good | Most APIs |
| **OAuth 2.0** | Token exchange flow | Strong | Google, GitHub |
| **Basic Auth** | Username:password | Weak | Legacy APIs |
API Key Example:
Bearer Token Example:
Security Best Practices:
- ๐ Never hardcode API keys in source code
- ๐ Use environment variables (.env files)
- ๐ Rotate keys periodically
- ๐ก๏ธ Use minimum required permissions
- ๐ Monitor API key usage for anomalies
๐ฌ Real Example โ Travel Agent APIs
Building a Travel Planning Agent:
User: "Plan a Goa trip for March 15, 3 nights"
Agent calls 4 APIs in parallel, then sends summary via Email API!
All automated, all in under 30 seconds! โก
โ ๏ธ Error Handling for APIs
APIs WILL fail. Plan for it!
Error Handling Strategy:
Error Recovery Matrix:
| Error | Retryable? | Strategy |
|---|---|---|
| Timeout | Yes | Retry 3x with backoff |
| Rate limit (429) | Yes | Wait and retry |
| Auth error (401) | Once | Refresh token, retry |
| Bad request (400) | No | Fix params or skip |
| Server error (500) | Yes | Retry 3x |
| Not found (404) | No | Use alternative |
๐ Popular APIs for Agents
Must-know APIs for building agents:
| Category | API | Free Tier | Use Case |
|---|---|---|---|
| ๐ค๏ธ **Weather** | OpenWeatherMap | 1000 calls/day | Weather data |
| ๐ง **Email** | SendGrid | 100 emails/day | Send emails |
| ๐ **Search** | SerpAPI | 100 searches/mo | Web search |
| ๐ **Calendar** | Google Calendar | Free | Schedule management |
| ๐ฌ **Messaging** | Twilio | Trial credits | SMS/WhatsApp |
| ๐ **Data** | Alpha Vantage | 25 calls/day | Stock data |
| ๐บ๏ธ **Maps** | Google Maps | $200/mo free | Location services |
| ๐ฐ **News** | NewsAPI | 100 requests/day | News articles |
| ๐ **Code** | GitHub API | 5000 req/hr | Repository management |
| ๐ง **AI** | OpenAI API | Pay-per-use | LLM capabilities |
Pro tip: Start with free tiers. Upgrade only when you need more! ๐ฐ
๐ API Rate Limiting & Optimization
Rate limits handle panradhu critical!
Common Rate Limits:
| API | Limit | Period |
|---|---|---|
| OpenAI | 500 RPM | Per minute |
| Google Maps | 50 QPS | Per second |
| 300 req | Per 15 min | |
| GitHub | 5000 req | Per hour |
Optimization Strategies:
- Caching ๐พ
- Batching ๐ฆ
- 10 individual API calls โ 1 batch API call
- Many APIs support batch endpoints
- Parallel Requests โก
- Independent API calls simultaneously execute pannunga
- Don't wait for weather API to finish before calling flight API
- Request Deduplication ๐
- Same query within 5 minutes? Return cached result
Impact:
| Strategy | API Calls Saved |
|---|---|
| Caching | 40-60% |
| Batching | 50-80% |
| Parallel | 0% (but faster!) |
| Dedup | 10-30% |
๐งช Try It โ Build an API-Using Agent
โ ๏ธ API Security Checklist
Before integrating any API:
- ๐ API keys in .env โ Never in source code
- ๐ HTTPS only โ No HTTP API calls
- ๐ก๏ธ Input validation โ Sanitize before sending to API
- ๐ Scope limitation โ Request minimum permissions
- ๐ฐ Spending limits โ Set budget alerts on paid APIs
- ๐ Usage monitoring โ Track calls, detect anomalies
- ๐ Key rotation โ Change keys every 90 days
- ๐๏ธ Data cleanup โ Don't store unnecessary API responses
One leaked API key = potential thousands in charges! ๐ธ
๐ก API Integration Best Practices
1. Wrapper functions โ Don't call APIs directly, wrap them
2. Retry logic โ Built-in for every API call
3. Timeout handling โ Max 30s per API call
4. Response validation โ Check response format before using
5. Fallback APIs โ Primary fails? Use backup provider
6. Documentation โ Document every API your agent uses
7. Version pinning โ Use specific API versions, not "latest"
8. Mock testing โ Test without hitting real APIs
๐ Summary
Key Takeaways:
โ APIs give agents real-world action capability
โ Function calling: LLM decides which API to call and with what params
โ Authentication: API Key, Bearer Token, OAuth 2.0
โ Error handling: Retry, backoff, fallback, human escalation
โ Optimization: Cache, batch, parallel, deduplicate
โ Security: .env files, HTTPS, input validation, spending limits
โ Start with free tier APIs, upgrade when needed
Next article la Multi-Agent Architecture paapom โ enterprise-grade agent systems! ๐ข
๐ ๐ฎ Mini Challenge
Challenge: Build Agent-API Integration
Agent-ku external API integrate panni hands-on:
Scenario: Travel booking agent โ flights + hotels search pannum
Step 1: Define APIs (3 mins)
Identify 3 APIs:
- Flight Search API: GET /api/flights?from=MAA&to=BLR&date=2026-02-25
- Hotel Search API: GET /api/hotels?city=BLR&checkin=2026-02-25
- Weather API: GET /api/weather?city=BLR
Step 2: Create Tool Definitions (4 mins)
Each API-ku tool specification:
Step 3: Function Calling Setup (3 mins)
LLM-na tools define panni, LLM itself decide panna which tool call pannum
Example: User "Book flight to Bangalore next week"
- LLM: "I need to call search_flights"
- Params: {from_city: "MAA", to_city: "BLR", date: "2026-03-02", passengers: 1}
Step 4: Error Handling (3 mins)
Each API-ku error cases:
- API timeout? โ Retry
- Auth fail? โ Refresh token
- Rate limit? โ Wait and retry
- Invalid params? โ Inform LLM, refine
Step 5: Test (2 mins)
Test workflow:
API integration complete! ๐
๐ผ Interview Questions
Q1: AI Agent-ku APIs edhuku important?
A: APIs = agent-oda hands! Without APIs, agent just text generate pannum. APIs irundha real actions execute pannum โ booking, payment, data fetch, etc. Agent brain becomes executor!
Q2: Function calling vs direct API calls โ enna difference?
A: Function calling: LLM decides which function call pannanum, automatically! No manual code needed. Agent directly API hit pannum vs. LLM saying "call this function with these params" โ abstraction layer!
Q3: API error handling agent perspective?
A:
- Timeout: Retry with exponential backoff
- Auth fail: Refresh token, retry
- Rate limit: Queue, wait, retry
- Invalid params: Request better params from LLM
- 5xx errors: Retry later
Every error-ku recovery strategy mannumanum!
Q4: Free APIs agents practice-la use panna mudiyuma?
A: Definitely! OpenWeather, NewsAPI, GitHub, JSONPlaceholder, CoinGecko โ free tiers available. Perfect for learning! Production: premium APIs select panna-onum, reliability ensure!
Q5: API key security best practice?
A:
- Never hardcode .py files-la
- Environment variables use (.env)
- Production: secrets manager (AWS Secrets, Vault)
- Rotate keys regularly
- Monitor API usage (detect abuse)
- Scope API keys (specific permissions only)
Security = non-negotiable! ๐
โ Frequently Asked Questions
Test your API knowledge: