# Stop Giving Your AI Agents API Keys - Kim Maida, Keycard & Mason Egger, Temporal

## Executive summary

This talk details how to build secure and reliable AI agents by addressing two critical architectural challenges: identity management and state persistence. Kim Maida demonstrates replacing shared API keys with OAuth token exchange, ensuring every tool call is scoped to a specific resource and tied to both the agent's and the user's identity, creating a full audit trail. Mason Egger complements this by introducing Temporal, a durable execution framework that ensures agents can fail and recover seamlessly, preventing the loss of state, credentials, or expensive LLM calls during long-running tasks.

## Key takeaways

- Secure Agent Identity via Token Exchange: Instead of shared API keys, agents should use OAuth token exchange. This process ensures that every tool call receives a short-lived token scoped to that specific call and tied to the agent's identity and the user's identity (the 'subject token').
- Multi-Hop Token Delegation: Token exchange is a multi-hop process: The agent exchanges the user's subject token for a token to access the MCP server (1st hop). The MCP server then uses that token to initiate a second exchange to get a token for the downstream resource (e.g., Ledger API).
- Durable Execution for Agents: Temporal provides durable execution, allowing agents (which are complex loops/distributed systems) to fail and recover without losing state, re-calling LLMs, or re-executing previous activities. The application state is maintained internally.
- Maintaining Confidentiality in Durable Systems: Sensitive data (like JWT tokens and credentials) must be kept out of the Temporal event history. This can be achieved using custom interceptors (like Keycard implemented) or Temporal's native payload encoder.

## Technical details

- OAuth 2.0 Token Exchange: The process involves an Authorization Server (IdP) issuing a token after user consent and defining scopes. The agent uses this subject token to request access to a resource via a dedicated OAuth client, which validates the request against defined policies.
- Agent Architecture Components: The agent loop involves an LLM, an MCP client, and an MCP server. The MCP server acts as a gateway, receiving the agent's proposed tool calls and initiating the necessary token exchanges to access downstream resources (e.g., Ledger API).
- Temporal Workflow Structure: Temporal applications are built using workflows, which are defined by parent and child function calls (activities). These activities are designed to be fault-tolerant, allowing the system to retry or rebuild state upon failure.

## Practical implications

- Build engineers can design highly secure agents by implementing token exchange, ensuring that access is always scoped and auditable, rather than relying on static, shared credentials.
- The combination of Keycard and Temporal allows for the creation of mission-critical, stateful agents that can withstand network failures, service outages, and compute restarts without data loss or manual intervention.
- Implementing durable execution is crucial for any agent loop that involves expensive, multi-step processes like LLM calls or external API interactions.

## Topics

Agent Architecture, OAuth 2.0, Identity and Access Management (IAM), Durable Execution, Distributed Systems, LLM Agents, Keycard, Temporal, Keycloak

Source: https://www.youtube.com/watch?v=YqJ4asgj1R8
