โ† Back|AI-AGENTSโ€บSection 1/16
0 of 16 completed

Using APIs inside agents

Intermediateโฑ 10 min read๐Ÿ“… Updated: 2026-02-17

๐Ÿ”Œ 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 AloneAgent + 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:


TypeProtocolFormatExample
**REST**HTTPJSONMost web APIs
**GraphQL**HTTPJSONGitHub, Shopify
**WebSocket**WSJSON/BinaryReal-time data
**gRPC**HTTP/2Protocol BuffersMicroservices

REST API Basics (most common):


code
GET    /api/flights?from=MAA&to=BLR    โ†’ Fetch data
POST   /api/bookings                    โ†’ Create data
PUT    /api/bookings/123                โ†’ Update data
DELETE /api/bookings/123                โ†’ Delete data

HTTP Status Codes agents should handle:

CodeMeaningAgent Action
200Success โœ…Process response
400Bad requestFix parameters, retry
401UnauthorizedRefresh auth token
404Not foundTry alternative
429Rate limitedWait and retry
500Server errorRetry 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)

json
{
  "name": "search_flights",
  "description": "Search available flights between cities",
  "parameters": {
    "from_city": "string (IATA code)",
    "to_city": "string (IATA code)",
    "date": "string (YYYY-MM-DD)",
    "passengers": "integer"
  }
}

Step 2: User gives request

"Book me a flight to Bangalore tomorrow"


Step 3: LLM decides which function to call

json
{
  "function": "search_flights",
  "arguments": {
    "from_city": "MAA",
    "to_city": "BLR",
    "date": "2026-02-18",
    "passengers": 1
  }
}

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

๐Ÿ—๏ธ Architecture Diagram
```
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚           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:


MethodHow It WorksSecurityExample
**API Key**Key in header/queryBasicOpenWeather
**Bearer Token**JWT token in headerGoodMost APIs
**OAuth 2.0**Token exchange flowStrongGoogle, GitHub
**Basic Auth**Username:passwordWeakLegacy APIs

API Key Example:

code
GET /api/weather?city=Chennai
Headers: {
  "X-API-Key": "your-secret-key-here"
}

Bearer Token Example:

code
GET /api/user/profile
Headers: {
  "Authorization": "Bearer eyJhbGciOi..."
}

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

โœ… Example

Building a Travel Planning Agent:

code
Agent has access to 5 APIs:

1. ๐ŸŒค๏ธ Weather API
   GET /weather?city=Goa&date=2026-03-15
   โ†’ {"temp": 33, "condition": "sunny", "humidity": 75}

2. โœˆ๏ธ Flight API  
   GET /flights?from=MAA&to=GOI&date=2026-03-15
   โ†’ [{"airline": "IndiGo", "price": 3500, "time": "08:30"}]

3. ๐Ÿจ Hotel API
   GET /hotels?city=Goa&checkin=2026-03-15&nights=3
   โ†’ [{"name": "Beach Resort", "price": 5000, "rating": 4.5}]

4. ๐Ÿฝ๏ธ Restaurant API
   GET /restaurants?city=Goa&cuisine=seafood
   โ†’ [{"name": "Fisherman's Wharf", "rating": 4.8}]

5. ๐Ÿ“ง Email API
   POST /send-email {to, subject, body}
   โ†’ {"status": "sent"}

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:


code
function callAPI(endpoint, params, maxRetries = 3) {
  for (attempt = 1; attempt <= maxRetries; attempt++) {
    try {
      response = http.get(endpoint, params)
      
      if (response.status === 200) return response.data
      if (response.status === 429) {
        wait(attempt * 2 seconds)  // exponential backoff
        continue
      }
      if (response.status === 401) {
        refreshToken()
        continue
      }
      if (response.status >= 500) {
        wait(attempt * 1 second)
        continue
      }
      
      throw Error("Unexpected status: " + response.status)
    } catch (networkError) {
      if (attempt === maxRetries) throw networkError
      wait(attempt * 2 seconds)
    }
  }
}

Error Recovery Matrix:

ErrorRetryable?Strategy
TimeoutYesRetry 3x with backoff
Rate limit (429)YesWait and retry
Auth error (401)OnceRefresh token, retry
Bad request (400)NoFix params or skip
Server error (500)YesRetry 3x
Not found (404)NoUse alternative

๐Ÿ”„ API Rate Limiting & Optimization

Rate limits handle panradhu critical!


Common Rate Limits:

APILimitPeriod
OpenAI500 RPMPer minute
Google Maps50 QPSPer second
Twitter300 reqPer 15 min
GitHub5000 reqPer hour

Optimization Strategies:


  1. Caching ๐Ÿ’พ
code
cache = {}
if query in cache and cache_age < 1_hour:
    return cache[query]  // No API call needed!
else:
    result = call_api(query)
    cache[query] = result
    return result

  1. Batching ๐Ÿ“ฆ
  • 10 individual API calls โ†’ 1 batch API call
  • Many APIs support batch endpoints

  1. Parallel Requests โšก
  • Independent API calls simultaneously execute pannunga
  • Don't wait for weather API to finish before calling flight API

  1. Request Deduplication ๐Ÿ”„
  • Same query within 5 minutes? Return cached result

Impact:

StrategyAPI Calls Saved
Caching40-60%
Batching50-80%
Parallel0% (but faster!)
Dedup10-30%

๐Ÿงช Try It โ€“ Build an API-Using Agent

๐Ÿ“‹ Copy-Paste Prompt
```
You are an AI Agent with these API tools:

TOOLS:
1. get_weather(city, date) โ†’ {temp, condition, humidity}
2. search_news(topic, limit) โ†’ [{title, summary, url}]
3. send_email(to, subject, body) โ†’ {status}
4. translate(text, from_lang, to_lang) โ†’ {translated_text}

USER REQUEST: "Check tomorrow's weather in Mumbai, find 
top 3 AI news articles, translate the headlines to Tamil, 
and email everything to boss@company.com"

Execute step by step:
1. Show which API you'll call and why
2. Show the API call with parameters
3. Show simulated response
4. Show how you combine results
5. Show the final email content

Think about: Can any calls be made in parallel?
```

Practice API orchestration skills! ๐Ÿ”ง

โš ๏ธ API Security Checklist

โš ๏ธ Warning

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

๐Ÿ’ก Tip

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:

  1. Flight Search API: GET /api/flights?from=MAA&to=BLR&date=2026-02-25
  2. Hotel Search API: GET /api/hotels?city=BLR&checkin=2026-02-25
  3. Weather API: GET /api/weather?city=BLR

Step 2: Create Tool Definitions (4 mins)

Each API-ku tool specification:

json
{
  "name": "search_flights",
  "description": "Search available flights",
  "parameters": {
    "from_city": "string (IATA code)",
    "to_city": "string (IATA code)",
    "date": "string (YYYY-MM-DD)",
    "passengers": "integer"
  }
}

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:

code
User: "Show me flights to Bangalore and weather there"
Agent calls: search_flights() + weather_api()
Results combine aagum
Output: "Found 5 flights, weather is sunny"

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

โ“ API na enna simple la?
Application Programming Interface โ€“ oru software innooru software kitta talk panna use pannum method. Restaurant la menu card maari โ€“ available options show pannum, neenga order pannunga.
โ“ Agent ku API edhuku?
API illama agent just think and talk mattum dhaan pannum. API irundha real actions edukka mudiyum โ€“ email send, data fetch, booking, payment, etc.
โ“ Free APIs irukka?
Niraiya irukku! OpenWeather, News API, GitHub API, JSONPlaceholder โ€“ free tiers available. RapidAPI la 1000+ free APIs browse pannalaam.
โ“ API key secure aa store panna epdi?
Environment variables use pannunga (.env file). Never hardcode in source code. Production la use secrets manager (AWS Secrets, HashiCorp Vault).
๐Ÿง Knowledge Check
Quiz 1 of 1

Test your API knowledge:

0 of 1 answered