03. Core Components
API Integration for Agents
API integration is how agent tools reach real systems — CRM, Git, Slack, payments, search. Wrap APIs behind schemas, auth, retries, and clean observations so the LLM never handles raw secrets or brittle HTTP details.
What you will learn
- Explain how APIs connect to agent tools
- Design layered adapters (schema → client → secrets)
- Apply auth, timeout, retry, and idempotency patterns
- Shape API responses into model-friendly observations
- Answer interview questions with short, solid answers
Definition
API integration for agents means connecting external services to the tool runtime so an agent can read and write real systems. The LLM should see tool names and structured observations — not API keys, sprawling JSON, or vendor SDK quirks.
Good integrations are boring: least-privilege auth, typed adapters, predictable errors, and logs that correlate tool_call_id with upstream request ids.
Introduction
This lesson (#6 in Core Components) follows Tool Calling. You know how the model proposes actions; now you wire those actions to production APIs.
Next: Feedback, Reflection and Goal Management — using API outcomes to steer the loop.
Why API integration matters
- Agents without APIs can only talk; with APIs they can operate
- Most production failures are integration failures, not model IQ
- Security lives at the API boundary (tokens, scopes, audit)
- Clean observations improve the next reasoning step
Integration layers
Separate concerns so prompt changes never touch secrets, and vendor SDK upgrades do not rewrite tool schemas.
- Orchestrator - decides when tools run
- Tool schemas - LLM-facing contracts
- Adapters - map args, shape observations, enforce idempotency keys
- API clients - HTTP/SDK, rate limits, retries
- Secrets - vault, OAuth tokens, service accounts
Auth patterns agents use
- Service account / API key - server-side only, never in prompts
- OAuth on behalf of user - store refresh tokens securely; respect scopes
- Signed requests / mTLS - for high-trust internal APIs
- Per-tenant credentials - isolate customers in multi-tenant agents
- Short-lived tokens - prefer over long-lived keys when possible
Reliability and errors
Agents retry loops. Flaky or non-idempotent APIs create duplicate tickets, double charges, or storms.
- Timeouts on connect and read
- Retry only idempotent or safely keyed operations
- Backoff on 429 / 5xx; surface rate-limit hints to the agent
- Map vendor errors to stable codes the model can handle
- Paginate explicitly - do not dump unbounded lists into context
- Trace tool_call_id ↔ upstream request id
Designing observations
Return what the agent needs to decide next — not the entire vendor payload.
- Prefer small JSON with status, key fields, and next_page tokens
- Redact secrets and PII from observations
- Include human-readable error hints without stack traces
- For writes, return resource ids and verification links
Common integration styles
- Request/response tools - agent pulls data or pushes updates
- Webhook + queue - external events wake a proactive agent
- MCP / connector catalogs - standardize tool discovery
- GraphQL/REST/gRPC - pick by service; keep adapter stable
- Human approval step - before irreversible API writes
Program / implementation notes
Build one adapter per domain (tickets, repo, billing). Share a client factory for auth and retries. Contract-test adapters with recorded fixtures.
- Never log raw Authorization headers
- Feature-flag new write tools in staging first
- Add dry_run where vendors allow
- Quota per agent run separate from global API quota
- Next: Feedback, Reflection and Goal Management
Advantages and limitations
- Advantage: real-world actions and grounded data
- Advantage: reusable adapters across agents
- Advantage: auditable side effects
- Limitation: vendor rate limits and outages become agent outages
- Limitation: OAuth and secret rotation add ops load
- Limitation: over-wide APIs increase blast radius of mistakes
Key points
- APIs power tools; tools are what the LLM should see.
- Layer schemas, adapters, clients, and secrets separately.
- Least-privilege auth; never put credentials in prompts.
- Timeouts, retries, and idempotency are mandatory for agent loops.
- Shape small, redacted observations for the next model turn.
- Correlate tool calls with upstream request ids for debugging.
FAQs
Should the LLM call HTTP APIs directly?
No. Let the model call typed tools; your adapters perform HTTP with auth, validation, and logging.
How is this different from normal backend integration?
Same networking basics, plus agent-specific needs: tool schemas, observation shaping, tighter allowlists, and retry behavior driven by an LLM loop.
Where do webhooks fit?
They push events into a queue that can start or resume an agent — useful for proactive workflows.
What is MCP in this context?
A protocol/pattern for exposing tools and resources to AI apps in a standard way — still backed by your APIs and auth.
Interview questions
- How do agents integrate with external APIs?
Answer Through tool adapters that validate arguments, attach credentials, call the API, and return structured observations to the agent loop.
- Why not expose raw REST endpoints to the model?
Answer Because of secrets, oversized payloads, unsafe methods, and brittle vendor details — narrow tools reduce risk and improve reliability.
- What auth patterns are common?
Answer Server-side API keys/service accounts, user OAuth with scoped tokens, and short-lived credentials from a vault — never in the prompt.
- How do you handle retries safely?
Answer Retry transient failures with backoff only when operations are idempotent or protected by idempotency keys; otherwise fail and let the agent replan.
- What should a tool observation contain?
Answer A compact, redacted summary of status and key fields the model needs next — not the full upstream response.
- How do you debug a failed agent API call?
Answer Trace tool_call_id to the upstream request id, inspect status/error mapping, auth scope, and whether the observation was truncated or misleading.
- Webhooks vs polling for agents?
Answer Webhooks/events are better for proactive agents; polling is simpler but costlier and slower to react.
Related topics
Practice task
Complete each numbered practice step in a notebook or doc before opening the next lesson.
- Draw agent -> tool schema -> adapter -> API client -> secrets for a ticket system.
- List scopes you would grant a support agent (and what you would deny).
- Design the observation JSON for create_ticket success and failure.
- Write a retry policy for GET vs POST charge endpoints.
- Answer interview questions 1, 2, and 4 in two sentences each.