APIs in the Age of AI Agents
From the publisher’s feed, collected in the ZeroLabs newsroom.
Why Every AI Builder Needs to Understand the BasicsImage created by the author using chatGPT.IntroductionSoftware systems require defined interfaces to exchange data and invoke functionality. An application programming interface (API) provides that interface. In AI systems, APIs connect language models, databases, and external tools.Agent-based systems rely on repeated interactions with such interfaces. An agent receives a task, decomposes it, retrieves context, invokes tools, and returns a result. Each of these steps may involve one or more API calls. In this setting, APIs define how components interact and how work is executed.If you are building agentic systems, orchestrating tools, or deploying LLM powered systems, understanding APIs is crucial as it is an essential part of the architecture.Why You Should CareAI Agents Are the DriverMore and more, as agentic workflows keep maturing and impacting industries, it is not just about larger models. It is more and more about orchestration of systems.A typical agent loop includes task evaluation, tool selection, context retrieval, and response generation. Each tool invocation is a call to a defined interface. When the tool is remote, the interface is usually an HTTP API. You can read more about the agentic loop here.The agent tool loop concept. Inside of an AI agent, there is an LLM inside of a loop that makes decisions based on the consequences of actions (tools) to achieve a task. Each iteration the context is updated with the status of the task and the result of the action. Image created by the author.At the core, an AI agent typically operates in a cycle when receives a task:1. Checks if the task is completed2. Breaks it into steps3. Calls tools4. Retrieves context5. Updates its reasoning6. Returns a resultConsider an agent that summarizes clinical notes, queries a vector database, computes a risk score, writes results to a database, and triggers a notification. If any of these components are external services, the agent must coordinate multiple API calls. The behavior of the system depends on latency, error handling, authentication, and response structure across those calls.Understanding APIs allows a developer to measure and control latency across chained requests, handle partial failures, restrict access to sensitive data, and limit unnecessary model invocations. These concerns determine whether a system operates reliably under load.Without API literacy, you can build prototypes. With API literacy, you can build production systems.The basics: What Is an APIAPI stands for Application Programming Interface. It is a structured contract that allows one software system to communicate with another, a client and a server. The client sends a request in a defined format. The server returns a response in a defined format.In HTTP-based APIs, the main elements are endpoint, method, headers, request body, and response.1. EndpointAn endpoint is a URL that identifies a resource or capability. For example, POST /v1/summarizeor GET /v1/patients/123.Example:POST /v1/summarizeGET /v1/patients/123POST /v1/risk-assessmentIn agent systems, endpoints often represent tools.2. HTTP MethodThe HTTP method indicates the action.GET retrieves information without changing state.POST sends data to be processed. Most AI inference calls use POST.PUT updates existing resources.DELETE removes resources.Agents usually rely heavily on POST because they send structured inputs to models or tools.3. HeadersHeaders contain metadata about the request.Common headers:- Content-Typedefines the format of the payload, usually application json.- Authorizationcarries credentials such as API keys or tokens.Example:Content-Type: application/jsonAuthorization: Bearer sk-123456Headers are critical in regulated systems where traceability and authentication matter.4. Request BodyThis is the actual data being sent. Example for a summarization model:{ "text": "Patient presents with bleeding on probing...", "max_tokens": 200, "temperature": 0.2}The body contains both task data and configuration parameters.5. ResponseThe response contains structured output. Well-designed APIs return predictable JSON objects so that agents can parse results without ambiguity.{ "summary": "Moderate periodontal risk with localized inflammation.", "tokens_used": 145}In this way agents can reliably parse the output.A complete request includes all of these elements. The server returns an HTTP status code and a response body. A status code of 200 indicates success. Other codes indicate client or server errors.Putting It All TogetherHere is what a complete request looks like conceptually:POST /v1/summarize HTTP/1.1Host: api.example.comContent-Type: application/jsonAuthorization: Bearer sk-123456{ "text": "Patient presents with bleeding on probing...", "max_tokens": 200, "temperature": 0.2}And the response:HTTP/1.1 200 OKContent-Type: application/json{ "summary": "Moderate periodontal risk with localized inflammation.", "tokens_used": 145}What happens when the service requested takes longer than a few seconds?Synchronous and Asynchronous requestsIn a synchronous request, the client waits for the server to respond. This model is simple and works for short operations such as small inference calls.In an asynchronous request, the client submits a job and continues execution. The result is retrieved later through polling, callbacks, or a message queue. This model is used for long-running tasks such as processing large documents or coordinating multiple steps.Agent systems often mix both approaches. Short tool calls may be synchronous. Long-running processes are typically asynchronous to avoid blocking the entire system.Communication Protocols: REST, WebSockets, and WebRTCMost APIs use HTTP with a request — response model, often described as REST. But AI agents do more than single request-response calls. They stream tokens, maintain sessions, coordinate voice conversations, and push updates in real time. Protocol choice matters.RESTREST stands for Representational State Transfer. It is an architectural style built on top of HTTP. REST APIs are stateless: each request contains all necessary information. In practice, REST means:- HTTP based endpoints- Stateless communication- Request and response model- Structured data exchange, usually JSONEach request is independent. The server does not store session state unless explicitly designed to.REST is ideal for:- Model inference calls- Structured tool invocation- Retrieving records from a database- Triggering background jobsIf your agent calls POST /v1/risk-assessment and waits for a result, REST is the right tool. REST is simple, scalable, and widely supported.WebSocketsWebSockets enable persistent, bidirectional communication between client and server. Unlike REST, instead of sending the request and receiving the response and terminating the transaction, the connection remains open. Both client and server can send messages at any time.This is ideal for:- Streaming LLM tokens- Real time chat interfaces- Progress updates for long running tasks- Live dashboardsInstead of waiting for the full model output, tokens stream incrementally. WebSockets improve user experience by reducing perceived latency. However, they require connection management, careful scaling strategies and load balancing considerations. They are powerful but add architectural complexity.WebRTCWebRTC stands for Web Real Time Communication. It is designed for peer to peer real time audio, video, and data exchange. WebRTC is optimized for low latency, adaptive bitrate and media streaming. As you can see, this protocol shines for- Voice based AI agents- AI powered telehealth- Real time conversational systems- Interactive tutoring platformsIf you are building a voice assistant that processes speech and responds instantly, WebRTC is often the correct choice. Note that it is not designed for typical JSON based inference calls.Choosing the Right ProtocolA simple guide to decide what protocol to use. Many systems use more than one protocol. A system may use REST for tool invocation, WebSockets for streaming responses, and WebRTC for audio interaction.Authentication and AuthorizationAPIs require a method to verify the identity of the caller and determine what actions are permitted for each caller.For example, for agents, this determines which agent is allowed to call which tool or which user can access which patient record or which backend service can invoke which model.In AI agent systems, authentication is part of architecture. Let us unpack the major methods.1. Bearer TokensThe Simplest Model of TrustBearer tokens are a common method. The client includes a token in the Authorization header. The server validates the token and grants access if it is valid. You often see this:Authorization: Bearer sk-123456What does Bearer mean? It literally means, whoever bears this token is allowed access. The token itself is the proof. There is no additional identity check in the request. If the token is valid, access is granted.Think of a backstage concert pass. If you hold the pass, security lets you in. They do not ask your name. The pass is enough. But if someone steals your pass, they can enter as you. That is the tradeoff.Bearer tokens are often popular because they are:SimpleStatelessEasy to validateEasy to scaleThis is why model providers and internal microservices commonly use them. This method of authentication is often chosen forserver to server communicationInternal toolsModel inference endpointsSystems without complex user rolesLimitationThey do not inherently define permissions or identity. They only validate possession. If you need fine grained control or user delegation, bearer tokens alone are not enough.2. API Keys in Custom HeadersA Variation of BearerAPI keys are similar. They are often sent in custom headers such as x-api-key. They are used in public or metered APIs. Example:x-api-key: abc123This is conceptually similar to bearer tokens. The main difference is convention. Using Authorization with Bearer follows standardized HTTP semantics and integrates better with middleware and security tooling.API keys are often used for:Public APIsRate limited servicesMetered usageThey share the same weakness as bearer tokens: If leaked, access is compromised.3. Basic AuthenticationUsername and Password Over HTTPBasic authentication sends a username and password with each request. It is rarely used in new systems.Basic authentication sends:Authorization: Basic base64(username:password)This is an older method and not used any more. It is like writing your username and password on a note, encoding it, and attaching it to every request. Even though it is usually protected by HTTPS, it is not ideal for modern distributed systems. However, it is still used in legacy systems or internal low risk environments. It is rarely recommended for AI agent architectures.4. OAuth2Delegated AuthorizationOAuth stands for Open Authorization. This method is an authorization framework that allows a user to grant limited access to an application without sharing credentials. It is not just authentication. It is an authorization framework.An authorization server issues tokens with defined scopes. These scopes specify what actions are allowed. This model is used in systems where users delegate access to applications.Why It ExistsImagine you want an AI app to access your Google Drive. You do not give the app your Google password. Instead you log into Google, Google asks if you approve access, Google gives the app a token and then the app uses that token to access only allowed resources.What Makes OAuth DifferentBearer tokens prove possession. OAuth defines permission scopes and delegation. Scopes define what is allowed. For example, imagine we haveread:patientswrite:notesinvoke:risk-modelIn this case, we have a user that wants to calculate the risk for a patient to develop certain condition:User → Client App → Authorization Server → Resource ServerIn an OAuth setting, it would look like thisUser Client App Auth Server API Server | | | | | Login | | | |------------->| | | | | Request token | | | |--------------->| | | | | Issue token | | | | | Call API with token | | |-------------------------------> | | | | Validate token | | | |--------------> | | | | | |In this way you can make sure that the right person is identified and authortized to use only the designated resources.5. JWTJSON Web Tokens ExplainedJWT stands for JSON Web Token. Why JSON? Because the token contains structured claims encoded as JSON. Why Web? Because it is designed for web based systems and distributed services. Why Token? Because it represents a verifiable credential.A JWT has three parts:HeaderPayloadSignatureThe payload may include the user ID, role, expiration and permissions.Example payload conceptually:{ "sub": "user123", "role": "clinician", "exp": 1735689600}The signature ensures the token has not been tampered with.Think of a digitally signed passport. It contains your information. It has a government signature. Anyone who trusts that government can verify it without calling the government every time. That is the key advantage.This is powerlful because it enables stateless authentication and horizontal scaling in microservice architectures.The server does not need to check a database every time. It verifies the signature and trusts the claims. Therefore, it is preferred for distributed AI systems, microservices, agentic architectures and role based access control systems.JWT is often used together with OAuth2. OAuth2 handles delegation. JWT carries the actual claims.6. Mutual TLSCertificate Based TrustMutual TLS uses certificates to authenticate both client and server. It is used in environments that require strong guarantees about the identity of both parties.It is like two embassies verifying each other’s official seals before exchanging documents. This method is commonly used for healthcare systems, financial systems, inter organization communication and highly regulated environments. It is strong but operationally complex.This Matters for AI Agents because not every agent should call every tool. Not every user should access every dataset and not every service should invoke every model.Bottom line, authentication defines policy. Authorization defines permissions. In agentic systems where different levels of autonomy are part of the system, this becomes crucial.How to Build a minimal APIA simple API can be implemented with a web framework such as FastAPI. The following example defines an endpoint that computes a risk score from input data and requires a bearer token for access.Install dependencies:pip install fastapi uvicornCreate app.pyThe application defines input and output schemas, a function to compute a score, and a route that validates the request and returns a structured response. Running the server exposes an OpenAPI specification and an interactive interface for testing.from fastapi import FastAPI, Header, HTTPExceptionfrom pydantic import BaseModelfrom typing import Optionalapp = FastAPI()API_KEY = "secret-key"class RiskRequest(BaseModel): age: int smoking: bool probing_depth: floatclass RiskResponse(BaseModel): risk_level: str score: floatdef simple_risk_model(age, smoking, probing_depth): score = probing_depth * 2 if smoking: score += 3 if age > 50: score += 1 return score@app.post("/v1/risk-assessment", response_model=RiskResponse)def assess_risk(request: RiskRequest, authorization: Optional[str] = Header(None)): if authorization != f"Bearer {API_KEY}": raise HTTPException(status_code=401, detail="Unauthorized") score = simple_risk_model( request.age, request.smoking, request.probing_depth ) if score > 8: level = "High" elif score > 4: level = "Moderate" else: level = "Low" return RiskResponse(risk_level=level, score=score)This type of endpoint can be invoked by an agent as a tool. The agent sends structured input and receives structured output.Run the server:uvicorn app:app --reloadVisit:http://127.0.0.1:8000/docsFastAPI generates interactive documentation using OpenAPI.API endpoints can be tested with HTTP clients such as Postman or Bruno before integrating them into an agent system.Screenshot of Postman. Image edited by the author.Design Principles for AI Agent APIsExplicit Schemas for deterministic outputsAgent systems depend on predictable interfaces. Input and output schemas should be explicit. Responses should be structured so that they can be parsed without additional interpretation.Prefer structured JSON over ambiguous text when tools must be parsed by agents.VersioningVersioning allows changes without breaking existing clients. Routes such as /v1 and /v2 indicate different versions of the API. Use versioned routes such as /v1 and /v2 to evolve safely as development continues or or complexity grows.IdempotencyIdempotency determines whether repeated requests produce the same result without side effects. Many inference endpoints are designed to be idempotent.ObservabilityObservability includes logging request identifiers, latency, and errors. These records are used to trace failures and measure performance.Separation of ConcernsSeparate authentication, business logic, inference, and logging layers. Clean architecture keeps agent systems maintainable as complexity grows.ConclusionAPIs define how components in an AI system interact. In agent-based systems, most actions involve one or more API calls. The structure of those calls determines how data is exchanged, how failures are handled, and how access is controlled.Understanding endpoints, protocols, authentication methods, and response formats allows a developer to design systems that operate predictably under load.ReferencesFastAPIOpenAPI InitiativePostmanBrunoOAuth 2.0RFC 9110: HTTP SemanticsIf you found this post interesting or relevant to your work, please consider leaving a clap or a comment. Thanks 😁APIs in the Age of AI Agents was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.