读见

别再把 API 密钥直接交给智能体:用令牌交换和持久执行做报销工作流

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

约 32 分钟原文英文已翻译

视频由 YouTube 提供

推荐理由Keycard 与 Temporal 的报销演示把代理身份、短时授权和中断恢复接成工作流,帮助工程团队分别处理谁能行动与失败后如何继续的问题。

看前速览

3 个要点

Keycard 和 Temporal 用报销助手演示短时令牌、用户授权与审计,以及工作进程中断后的恢复。

  1. 每次工具调用申请独立的短时凭证

    智能体先为某个 MCP 工具申请访问权限。MCP 服务器再把这份凭证换成可以访问下游账本 API 的令牌。

  2. 分别记录用户、智能体和审批人

    智能体可以用自己的身份行动,也可以在用户授权范围内代办。审计记录能区分报销是谁提交的、谁批准或驳回。

  3. 恢复工作流时从已有结果继续

    结算进程重启后,Temporal 复用已记录的结果。拦截器在活动执行时获取凭证,另可通过负载编码保护保存下来的数据。

完整摘要

Kim Maida 从共享 API 密钥的报销助手讲起。这种设计无法确认谁提交了报销,也无法确认谁做出审批。她给智能体单独注册身份,让它把用户令牌逐跳换成范围更小的短时凭证。随后,Mason Egger 在结算中途停止 Temporal 工作进程,再将其重启。工作流从已有结果继续,已完成的调用没有重跑,事件历史里也没有保存 JWT。

章节

文字稿

查看原文

简要描述Brief Description

别再给你的 AI 智能体提供 API 密钥——Kim Maida(Keycard)与 Mason Egger(Temporal)Stop Giving Your AI Agents API Keys - Kim Maida, Keycard & Mason Egger, Temporal

目录Table of Contents

  • 简介
    Intro
  • 向智能体提供 API 密钥的问题
    The problem with giving agents API keys
  • 代币兑换详解
    Token exchange, explained
  • 跟踪提示在智能体循环中的流转
    Following a prompt through the agent loop
  • 每次工具调用都使用单独的令牌
    A separate token for every tool call
  • 工作坊设置:一名费用柜台代理蕴
    Workshop setup: an expense desk agent
  • 之前:所有人共用 API 密钥
    Before: everyone shares API keys
  • 赋予代理自身的身份
    Giving the agent its own identity
  • 将令牌交换接入代码中
    Wiring token exchange into the code
  • 以自身身份行事的智能体
    The agent acting as itself
  • 读取审计日志
    Reading the audit log
  • 添加非冒充的人工用户
    Adding human users without impersonation
  • 审批限额及各项审批的批准人
    Approval limits and who approved what
  • 追踪委派链路
    Tracing the delegation chain
  • Temporal:面向智能体的持久化执行
    Temporal: durable execution for agents
  • 在运行过程中终止一个 worker 并恢复运行
    Killing a worker mid-run, and recovering
  • 为什么持久性对智能体至关重要
    Why durability matters for agents
  • 不要将机密信息写入事件历史记录
    Keeping secrets out of the event history

简介Intro

Kim Maida:我叫 Kim Maida。我在 Keycard 工作。Keycard 为智能体提供身份与访问权限管理,并在运行时赋予智能体访问权限。哎呀。Kim Maida: My name is Kim Maida. I work at Keycard and Keycard provides agent access identity and access at runtime for agents. Oh my.

Mason Egger:大家好,我叫 Mason Egger。我是 Temporal 的解决方案架构师。Temporal 为软件应用提供持久化执行能力,涵盖智能体及其以上的场景。所以,不仅是智能体,基本上是整个技术栈。所以。Mason Egger: Hi everyone. My name is Mason Egger. I'm a solutions architect at Temporal and Temporal provides durable execution to your software applications up into and including agents. So, not just agents, but pretty much the entire the entire stack. So.

Kim Maida:本次研讨会所需的一切资料,都在屏幕上显示的链接里。你其实不需要提前注册 Keycard,因为注册流程会包含在课程内容中。不过,如果你想在研讨会结束后自己体验 Keycard,并创建一个供个人使用的账户,可以访问底部提供的链接,输入注册代码进行注册。好。Kim Maida: Everything you're going to need for the workshop is at the link that's on the screen right now. You actually do not need to sign up for Keycard ahead of time because the sign up will be included like within the curriculum, but in order to get an account for your own personal use, if you want to play with Keycard on your own after the workshop, you can go to the link that is provided at the bottom and enter that registration code in order to sign up for a personal account. Okay.

向代理提供 API 密钥的问题The problem with giving agents API keys

我们稍后会回到这张幻灯片,不过我想先谈一谈代理访问。如今我们经常做的一件事,就是向代理提供 API 密钥,让它们能够访问资源,对吧?但这种做法存在几个问题。首先,凭据无法关联到用户身份或代理身份。代理可以不受限制地使用该 API 密钥所拥有的全部权限。最后,我们不能总是在每个环节都加入人工审核,因为人也会犯错,而且许多代理也是自主运行的。So, we'll come back to this slide, but I am going to start out by talking a little bit about agent access. So, one of the things that we do a lot of these days is give API keys to agents to give them access to resources, right? And there's several problems with doing things this way. One being credentials aren't attributed to a user identity or an agent identity. The agent has unrestricted access to any and all permissions in that API key. And finally, we can't just sort of stick human in the loop everywhere because humans make mistakes, too, and many agents also run autonomously.

代币交换详解Token exchange, explained

那么,我们解决这个 Keycard 问题的方法是使用一种名为令牌交换的开放标准。这项标准实际上自 2020 年起就已存在,它扩展了 OAuth 2。接下来我会向你展示我们今天要做什么。要为代理及其背后的用户提供身份信息,我们首先需要知道用户是谁。授权服务器会验证身份并签发令牌。Google、Okta、Auth0 等身份提供商都提供授权服务器。如果我们想在代理执行路径中建立身份链,就需要先让用户登录。登录后,授权服务器会征求用户的同意,允许将其对资源的部分访问权限委托出去;这部分权限由一组称为作用域(scope)的权限组成。这样一来,访问权限就进行了第一轮收窄,对吧?So, the way we solve this Keycard is by using an open standard called token exchange. This standard has actually been around since 2020 and it extends OAuth 2 and I am going to show you what we're going to be doing today. So, in order to give identity to an agent and the user behind the agent, we need to know who the user is as first order of business. So, an authorization server verifies identities and issues tokens. Identity providers like Google, Okta, Auth0, etc. provide authorization servers. And if we want to get an identity chain in the agent execution path, then we need the user to log in first. After logging in, the authorization server prompts user for their consent to delegate some portion of their access to resources with a subset of their permissions called scopes. Now, this is kind of the first narrowing of access, right?

我们只是将用户总访问权限中的一部分委派给代理。授权服务器会签发一个令牌,用于识别用户并包含其访问级别。We're only delegating some of the user's total access to the agent. And the authorization server issues a token that identifies the user and includes their level of access.

跟踪提示在代理循环中的运行过程Following a prompt through the agent loop

那么,接下来看看代理执行流程中实际会发生什么:现在有一位经过身份验证、身份已知的用户,想要使用 LLM 与资源交互。代理是一个会调用 LLM 的控制循环。在本次研讨会中,这个代理是费用台代理,我们将围绕它展开。它会调用一个费用 MCP 服务器,该服务器位于代理与账本 API 之间。它为代理提供一组定义好的工具,同时也提供了与下游资源的连接。MCP 客户端会接收代理提出的工具调用,并将其分发到对应的 MCP 服务器。运行时是一个与代理循环一起运行并执行调用的进程。因此,在今天的示例中,费用台应用就是运行时。我们将使用一个在这个自定义费用台应用中实现的 LangChain 代理。So, now if we go into what actually happens in the agent execution path, we have an authenticated user now with a known identity who wants to use an LLM to interact with resources. Now, an agent is a control loop that calls an LLM and in this workshop, that agent is an expense desk agent. That's what we're going to be working with. It's going to call in an expense MCP server which sits between the agent and a ledger API. This provides the agent with a defined set of tools and it also provides the connection to that downstream resource. Now, an MCP client takes the agent's proposed tool calls and it dispatches them to its MCP server. The runtime is a process that runs with the agent loop and executes calls. So, in our case today, the Expense Desk application is the runtime. We're going to use a LangChain agent that's implemented in this custom Expense Desk app.

因此,如果我们沿着这条执行路径追踪一个提示词,用户会将提示词提交给运行时,运行时调用模型并将其发送给 LLM。模型会提出工具调用,随后 MCP 客户端将这些调用分发给其 MCP 服务器,由服务器执行工具,然后调用资源 API。API 会向 MCP 服务器返回响应。MCP 客户端将响应传递给运行时,运行时再调用模型;这个循环会不断重复,直到模型满意并向用户返回响应。现在,如果你还记得,我们有一个经过身份验证的用户,其令牌标识了用户的身份。这个令牌就成了我们所说的主体令牌,因为它标识了代理将代表其行事的主体。因此,为了支持令牌交换,我们需要一个能够执行代码的 OAuth 客户端。So, if we follow a prompt through this execution path, the user submits the prompt to the runtime, which calls the model and sends it to the LLM. The model proposes tool calls, which are then dispatched by the MCP client to its MCP server, which executes the tools, and then calls the resource API. The API returns a response to the MCP server. The MCP client delivers the response to the runtime, which calls the model, and this loop repeats until the model is satisfied and returns the response to the user. Now, if you remember, we have an authenticated user with a token identifying who they are. And this token becomes what we call a subject token because it identifies the subject on whose behalf the agent is going to act. So, in order to support token exchange, we need an OAuth client that's capable of executing code.

因此,它可能是 MCP 客户端与第三方 MCP 服务器之间的网关,也可能是自定义智能体应用,或者是封装现成编码智能体的 CLI。就今天这个例子而言,Expense Desk 智能体是一个自定义智能体应用,它实现了令牌交换,以便访问 Expense MCP 服务器。这个 MCP 服务器还支持针对下一个下游资源(即 ledger API)的令牌交换。该 API 连接到实际存储费用数据的数据库。So, that might be a gateway between the MCP client and maybe a third-party MCP server, or a custom agent app, or a CLI wrapper around an off-the-shelf coding agent. In this case today, the Expense Desk agent is a custom agent app that implements token exchange for access to the Expense MCP server. This MCP server also supports token exchange for the next downstream resource, which is the ledger API. This connects to the database where the expenses are actually going to be stored.

每次工具调用都使用一个单独的令牌A separate token for every tool call

现在,提示词和主体令牌会发送到 Expense Desk 智能体应用。智能体循环开始运行,模型提出一次工具调用。Expense Desk 智能体使用 Keycard 的安全令牌服务进行身份验证。这是一个特殊的授权服务器。Expense Desk 智能体可以使用其客户端凭据或工作负载身份进行身份验证。它还会发送用户的主体令牌,其中声明了用户的身份和授权级别。此时,Expense Desk OAuth 客户端会创建一个令牌交换请求,请求授权访问 MCP 服务器以执行该工具调用,但仅限这一次特定的工具调用。现在,我们基本上有了三条信息:请求访问的智能体的身份,以及智能体代表其行事的用户的身份。Now, the prompt and the subject token are sent to the Expense Desk agent app. The agent loop runs, and the model proposes a tool call. The Expense Desk agent authenticates with Keycard's security token service. Now, this is a special authorization server. And the Expense Desk agent can authenticate using its client credentials or a workload identity. It also sends the user's subject token that declares the user's identity and their level of authorization. Now the expense desk OAuth client creates a token exchange request asking permission to access the MCP server for that tool call, but only that one specific tool call. So now we basically have three pieces of information. We have the identity of the agent that's requesting access. We have the identity of the user on whose behalf the agent is acting.

我们已经获得用户授予代理的访问权限。现在,我们需要决定是否签发所请求的令牌。我们可以通过策略来做出判断,策略会根据请求访问的资源、代理的身份以及代理代表谁行事进行评估。如果委托链和所请求的访问权限都符合策略,那么 Keycard 就会签发一个访问令牌,该令牌只能用于 MCP 服务器。该令牌通常会在几分钟内过期,因此绝不应存储它。令牌会发送给 Expense Desk MCP 客户端,后者使用该令牌作为承载凭证,向 Expense MCP 服务器发起工具调用。MCP 服务器会验证该令牌,然后用它来授权工具调用。这些工具需要访问 Ledger API 资源。And we have the level of access that the user granted to the agent. So now we need to decide if the requested token should be issued or not. And we can do this with policy, which is evaluated against what access is requested from what resource, who the agent is, and who the agent is acting for. Now if the delegation chain and the requested access all fall within the policy, then Keycard issues an access token that can be only be used at the MCP server. Now this issued token usually expires within minutes and you should never store it. The token is sent to the expense desk MCP client, which makes a tool call to the expense MCP server using that token as a bearer credential. The MCP server validates that token and then uses it to authorize a tool call. The tools need access to the ledger API resource.

因此,在那个环节,MCP 服务器会发起第二跳:它会向 Keycard 发送自己的令牌交换请求。这个交换令牌的请求来自费用台,目的是获取一个令牌,以访问作为下游资源的 ledger API。接着,Keycard 会根据策略评估请求,然后为该资源签发凭证,其权限范围仅限于完成这次特定工具调用所需的访问权限。MCP 服务器收到这个令牌后,就会用它调用 ledger API。API 会验证令牌,然后授予所请求的相应访问权限。MCP 服务器绝不会存储这个令牌,并会在调用完成后立即丢弃它。API 响应随后会向上返回,智能体循环会重复运行,直到模型向用户返回答案。概括来说,这就是今天你会看到我们用 Keycard 实现的流程。So at that point the MCP server then initiates a second hop. This is where it sends its own token exchange request to Keycard. And the request to exchange that token is received from the expense desk for a token to access the ledger API, which is the downstream resource. Now Keycard evaluates request against policy and then mints the credential for the resource scoped only for the access that's needed for that specific tool call. The MCP server receives this token and then it uses it to call the ledger API. The API validates the token and then grants the appropriate requested access. Now, the MCP server never stores the token and it discards it as soon as the call is done. The API response then flows back up and the agentic loop repeats until the model returns the answer to the user. So, at a high level, this is what you're going to see us implement today with Keycard.

我再把这张幻灯片调出来。And I'm going to pull up this slide again.

研讨会设置:费用台助理Workshop setup: an expense desk agent

所以,再说一遍,我基本上会进行演示,或者我和 Mason 会一步一步在屏幕上演示所有内容。呃,你可以跟着一起操作;如果你更愿意只看,也可以。呃,本次工作坊会使用一个预先配置好的共享 Keycard 组织。因此,如果你参与进来,你就会和房间里的其他人处于同一个组织中,也就能看到其他人在做什么。好,打开之前提供的链接后,你会看到这个页面。这就是在一个沙盒环境中运行的工作坊。我们现在就启动它。这需要一两分钟来完成所有设置。简单来说,我们会从一个无需登录的费用管理代理开始。So, again, I'm going to basically demo or Mason and I are going to demo everything step-by-step on screen. Uh you are free to follow along or if you would prefer to just watch, you can also do that. Uh the workshop is going to use a pre-provisioned shared Keycard organization. So, if you do participate, you are going to be in the same organization as everybody else in the room. So, you'll be able to see what everybody else is doing. Okay, so if you go to the link that is that was provided, you're going to see this. This is the uh workshop running in essentially a sandbox. So, we're going to go ahead and start this. This is going to take a few min a minute or two to uh get everything all set up. But, basically, kind of the premise here is we're going to start with an expense desk agent that does not require a login.

它在任何时候都不知道是谁在操作,而且所有使用该代理的人都在使用 API 密钥。所以,我这里有几位同事会帮忙填充演示环境。嗯,如果你也想跟着一起操作,之后需要在 Keycard 中获得管理员权限。因此,在我们进行 workshop 的过程中,等到讲到这部分时,他们会留意用户列表里是否有人出现,并在看到你之后为你授予管理员权限。It doesn't know who is acting at any point and everybody who uses the agent is using an API key. So, I have some folks uh here who are going to be helping to populate the demo, essentially. Um if you want to follow along as well, you would are going to eventually need uh admin access in Keycard. So, as we go through the workshop, when we hit that part, uh they'll be watching to see people show up uh in the user list, and they will be granting you admin access as that happens.

之前:所有人共用 API 密钥Before: everyone shares API keys

那么,当你开始研讨会时,你会看到这样的界面。简单来说,这是一个我们不知道任何人身份的应用。所有人都在使用同一个 MCP 服务器,但各自运行自己的智能体。他们都在使用 API 密钥。可能用的是同一个,也可能用的是不同的。我们呢,也没有什么办法知道这一点。不过,如果我们看看报销台,就会看到一个很典型的报销流程。你提交一笔报销,然后它要么获批,要么被拒,之后再付款,不过实际上不会真的付款,所以你不会收到钱。基本上,报销台里的所有操作都是通过智能体完成的。我们首先要做的是提交一笔报销。现在,我要告诉智能体,我想为研讨会的零食提交一笔 75 美元的报销。So, when you begin the workshop, this is what you're going to see. So, basically, this is the application where we don't know anybody's identity. Everybody is using the same MCP server, but they're all running their own agents. They are all using an API key. They might be using the same one, they might be using different ones. We, you know, don't really have a way of knowing that. But, if we look at the expense desk, it's we kind of see your classical uh expense. You file an expense, and then it either gets approved or rejected, um and then paid out, which is not actually going to happen, so you're not going to receive money. Um But, basically, everything in this expense desk operates through the agent. So, the first thing we are going to do is we're going to submit an expense. So, I'm going to tell the agent I want to submit a $75 expense for snacks for the workshop.

它提交了这笔报销,然后状态就变成了待处理。现在,如果我们往下看这里的活动记录,就能看到,呃,这笔报销是通过 API 密钥授予的访问权限创建的,对吧?所以,我们没有任何用户身份信息,也不知道代理的身份;代理本身目前也还没有身份。接下来,我要刷新一下,然后批准或拒绝其他人的报销。我们来看这一笔。如果我点击“批准”,实际上,呃,这只是会作为一条提示发送给代理。它接着就批准了这笔报销,现在我们看到,创建者是这个 API 密钥。所以,我不知道是谁创建了这笔报销,对吧?大家都只是在使用 API 密钥。我批准了它,但我们也没有关于是谁批准的记录,因为再说一遍,我们手上只有一个 API 密钥。It submits that expense, and then its status is pending. Now, if we go down to the activity here, we can see that uh this expense was created using access granted by an API key. Right? So, we don't have any user identity, we don't know the agent's identity, the agent doesn't really have an identity yet on its own. And what I'm going to do is I'm going to refresh this. And I'm going to approve or deny somebody else's expense. So, let's see this one. If I hit approve, it's basically uh just going to send that as a prompt to the agent. It went ahead and approved that expense, and now we have the created by was created by this API key. So, I don't know who created that expense, right? Everybody's just using API keys. I approved it, but we also don't have any record of who approved it, either, because again, all we have is an API key.

为智能体赋予独立身份Giving the agent its own identity

那么,接下来我们要做的,就是为代理赋予一个身份,这样它就能代表自己采取行动。嗯,我们现在要做的第一件事,就是加入那个共享的 Keycard 组织。如果你正在跟着操作,点击说明中的链接,就会看到使用 GitHub 登录的提示,你可以通过 GitHub 注册并加入 Keycard。登录后,你应该会在左上角看到这个工作坊的 AI NYC,这表明你加入了正确的组织。让我把这些内容挪一挪,好让大家看得更清楚。好,如果你正在跟着操作,加入时很可能会看到几个授权提示。这就是我之前在演示中提到的内容:你将自己的部分访问权限委托给代理,让它代表你使用。So, the next thing that we want to do is actually give the agent an identity, so that it can act on its own behalf. Uh so, the first thing we are going to do now is we are going to join that shared Keycard organization. So, if you click the link, if you're following along, if you click the link in the instructions, you will be able to you will get a prompt to log in with GitHub, and you can sign up with GitHub to join Keycard. When you get in, you should say see this workshop AI NYC in the upper left corner, indicating that you're in the right uh organization, and let me move this around so we can see things better. Okay. So, if you're following along, when you join, you're likely to see a couple of consent prompts. These are what I kind of talked about earlier in the presentation, where you are delegating some subset of your access to the agent to use on your behalf.

那么,这就是 Keycard 控制台。首先我想谈谈应用程序。应用程序和资源都是注册项。因此,某个对象既可以是应用程序,也可以是资源。基本上,这意味着应用程序可以发起令牌请求。它们会请求令牌交换,然后再通过交换来访问其他资源。所以,我们首先要做的是为我们的代理创建一个应用程序注册项。这里我会把它命名为“agent app”。由于我们都在同一个组织中,我们需要给事物命名,这样才能找到自己创建的应用程序和资源。所以,我会使用我的 GitHub 用户名。每个应用程序的标识符都必须是唯一的。So, this is the Keycard console. The first thing I want to talk about is applications. Now, applications and resources are registrations. So, something can be both an application and a resource. And basically, what this means is applications are things that are going to be able to make requests for tokens. They're going to ask for token exchanges in order to then exchange on down the line to access other resources. So, our The first thing we want to do is we want to create an application registration for our agent. So, what we are going to do here is I'm going to call this agent app. And then in order because we're all in the same uh organization here, we want to name things so that we can find our own applications and resources that we created. So, I'm going to use the my GitHub handle. Every app's identifier needs to be unique.

所以,我们在这里要做类似的事情。我们将使用隐式同意。然后需要获取重定向 URL。这个 URL 可以从我们的沙盒镜像中获取。我们进入这里看看。我不知道为什么那个窗口无法置于前台。打开终端。我们要运行这个命令。它会提供专属于你的沙盒实例的重定向 URL。所以,我先把它复制下来。我要……哎,不需要把这个添加为重定向 URL。登录事件发生后,用户就会被重定向回这里。然后我点击“创建应用程序”。现在我们有了一个注册项,表明我们的 Expense Desk Agent 可以请求令牌,对吧?不过,我们的 Expense Desk Agent 也可以接收令牌,因为用户登录时,它会从用户那里接收主体令牌。So, we are going to do something similar here. We're going to use implicit consent. And then we need to grab our redirect URL. Now, this is something that we can get out of our uh sandbox image. So, if we go into this. I don't know why that one won't come to the front. Go to the terminal. We are going to run this. This gives you the redirect URL that is specific to your instance of the sandbox. So, I'm just going to copy that. I'm going to Oops, don't need to add this as a redirect URL. So, after there's a login event, this is where the user is going to be redirected back to. And I'm going to say create application. So, now we have a registration that says our expense desk agent can request tokens, right? So, but our expense desk agent can also receive tokens because it's going to receive the subject token from the user when the user logs in.

为了做到这一点,我们基本上需要再进行一次注册,并声明这个代理应用也可以充当资源,这意味着它是令牌的目标接收方。因此,身份验证令牌就是这个代理将要接收的令牌。接下来进入资源页面,我们可以添加另一个资源。我们把它命名为……第一个是 agent app,这个就叫 agent resource。我会使用我的 GitHub 用户名。我们用一个类似的标识符。这就是 agent resource。我会将凭据提供方设置为 zone provider,这基本上意味着 Keycard 将负责处理令牌交换,并为此资源签发令牌。然后我往下找到这里,并声明这个资源由我的应用提供,也就是 agent app 所属的那个应用。And in order to do that, we basically need to do another registration, and we need to say that this agent application can also act as a resource, which means it is a target recipient for tokens. So, the authentication token is the token that this agent is going to be receiving. So, if we go into resources, we can add another resource. We are going to call this one The first one was agent app, this one is agent resource. I'm going to use my GitHub name. Let's use a similar identifier. This is the agent resource. I'm going to set the credential provider to zone provider, which essentially means that Keycard is the entity that's going to be handling the token exchange and issuing uh tokens for this resource. And then I'm going to come down here, and I'm going to say this resource is being provided by my application, the agent app came out of.

所以,基本上,这个关联会告知 Keycard,此应用可以交换发往此资源的登录令牌。如果我们不建立这个关联,令牌交换请求就会被拒绝。接下来,我要保存设置。现在,我想添加我的代理应用所依赖的内容。回到“应用程序”页面后,我找到我的应用,进入“依赖项”,然后查看依赖项列表,并在这里添加一项。我要添加的第一项是代理资源,因为我们的代理应用会请求令牌交换,以访问下游资源。我们还希望能够请求令牌交换,以访问费用 MCP 服务器。这是一个已为你预先配置好的资源。So, basically, this is the relationship that tells Keycard that uh this application can exchange sign-in tokens addressed to this resource. If we don't make this association, then the exchange requests are going to be rejected. So, I'm going to save that. And now I want to add the things that my agent application depends on. So, if I come back to applications, I find my application. I'm going to go into dependencies, and then I'm going to look at the list of dependencies, and I'm going to add something here. So, the first one I'm going to add is the agent resource, and we need this because our agent application is going to request token exchanges to access uh downstream resources. We also want to be able to request token exchanges for the expense MCP server. So, this is a resource that's already been preconfigured for you.

大家都在使用同一个,所以我们没必要创建一大堆都以我们自己命名的实例。另外,我们有一个代理,我们希望把该代理的 API 密钥存储在 Keycard 中,这样磁盘上就不会留有它的密钥。因此,Keycard 还提供了一个密钥库,而且我们已经在密钥库中为你配置好了这个 API 密钥。这样,我们就可以选择 LLM API 了。最后,我们还要选择这个用户信息端点,因为它可以告诉我们当前登录的用户是谁,并向我们提供该用户的身份信息。接下来,我点击“连接”。现在,我要做的下一件事是为这个代理应用创建一个独立的身份;但要做到这一点,它必须有某种方式来识别自己并登录。呃,所以我会点击“添加凭据”。Everybody is using the same one, so we don't need to create a bunch of them that are all named after ourselves. Additionally, we have an agent, and we want to have the API key for that agent stored in Keycard so that we don't have its secret on disk. So, Keycard also provides a vault, and we have that API key configured in the vault already for you. So, we can select the LLM API. And finally, we're going to select this user info endpoint as well, because this is going to be able to tell us who the logged-in user is. It's going to be able to provide us with that identity information. So, I'm going to say connect. Now, the next thing I need to do is I need to give this agent app its own identity, and I can't really do that unless it has some way to identify itself to essentially log in. Uh so, I'm going to click on add credential.

我要点击客户端 ID 和密钥。现在,稍等。我们打开编辑器,进入 agent 文件夹,找到 .env 文件,然后把客户端 ID 和客户端密钥粘贴进去。这就是密钥。我们这么做是因为这基本上是一个本地沙盒环境。如果你在生产环境中使用,可能会采用类似工作负载身份的方式。另外,Keycard 提供了一个 CLI,可以动态填充密钥,无需将它们存储在磁盘上。最后,我要告诉它代理资源的标识符是什么。也就是我的代理资源,然后是我的 GitHub 名称。等到这里显示“更改已保存”。I'm going to click on client ID and secret. Now, wait. We're going to go into the editor. We are going into the agent folder. We're going to find the .env file. And we are going to paste in that client ID and that client secret. This is the secret. Now, we're doing this because this is essentially like a sandbox local environment. If you were using this in a production environment, you would probably use something like workload identity. Also, Keycard provides a CLI which can populate secrets for you dynamically and not require them to be on disk. And then finally, I'm going to tell it what the identifier for the agent resource is. So, that was my your an agent resource and then my GitHub name. Going to wait until this says changes saved.

将令牌交换接入代码中Wiring token exchange into the code

到目前为止,我们已经配置好 Keycard,使其能够处理令牌交换和交换请求。现在,我们需要确保我们的费用管理应用也能实际处理这些请求。也就是说,当我们查看这里的代码时,会发现 agent 文件夹中有这段 agent 身份验证代码。往下看,我们会发现……好,这个东西挡住了。我试着把它移开。接下来我们会找到检查点,也就是……这是那个尚未接入任何经过身份验证的 MCP 访问的文件,对吧?这个研习班的安排是,每节课都会有一个检查点,提供该课结束时对应的代码。因此,我们现在可以去找练习二的检查点,也就是我们当前所在的部分。进入 checkpoints,再进入 authenticated,就能找到同一个文件。And at this point, we have configured Keycard to be able to handle the token exchanges and exchange requests. Now, what we need to do is make sure that our expense desk application can actually handle that. So, essentially, when we go and look at the code here, we find that in the agent folder, we have this agent auth code. If we go down, we find Okay, this thing is in the way. I'm trying to move that. This we're going to find the checkpoint, which So, this is the file where we don't have any authenticated MCP access, right? Now, the way the workshop works is if you at each lesson we have a checkpoint that will provide the code essentially for the end of that lesson. So, what we can do now is we can go and look for the checkpoint for exercise two, which is the one that we're at. So, if we go to checkpoints, we go to authenticated, and then we find the same file.

我们现在到哪儿了?好。我们要找到这个 MCP 工具集。这里传入的是来自 Keycard SDK 的 Keycard zone token 身份验证信息。每当代理实际想要通过 MCP 服务器调用工具时,这段代码就会发起一次 token 请求。因此,每次单独调用工具时,我们都会获取一个作用域限定为该次工具调用的 token。 现在,这个调用是……好。那么,我们打开 Keycard 文件,找到这个 mint 函数。它会实际发起请求,为令牌交换铸造一个 token。如果我们没有 subject token,也就是说没有已登录的用户,那么代理就会使用 client credentials 授权方式,以自身身份进行身份验证。也就是说,它会使用 client ID 和 client secret 向 Keycard 表明自己的身份。Where are we here? Okay. So, we want to find this MCP tool set. This is passing in the Keycard zone token auth from the Keycard SDK. And this is what is going to actually make that token request every time the agent actually wants to make a tool call with the MCP server. So, for every individual tool call, we're going to get a token that's scoped to that specific tool call. Now, this call is the this is Okay. So, now if we go to the keycard file, we look for this mint function. This is what actually goes ahead and makes that request to mint a token for the exchange. So, if we don't have a subject token, that is if we don't have a logged in user, then the agent is actually going to authenticate as itself using a client credentials grant. So, it's going to use that uh client ID and client secret to identify itself to Keycard.

这意味着它是在代表自己行事。好,现在我们切换到终端选项卡,基本上要确保启用更新后的代码。我们运行它,让它切换到检查点二。然后必须重启服务。停止。好了。现在我们去看看费用管理台,就会看到一个界面,让我们继续使用经过身份验证的费用管理台代理。我们点击“以代理身份继续”,然后要问它——我可以直接把这句话复制过来:“现在正在使用哪个账户?我的审批限额是多少?”And this means it's acting on its own behalf. Okay, so now if we go to the terminal tab, we basically want to make sure that we activate this updated code. So, we're going to uh run that so it switches over to checkpoint two. And then we have to restart the services. Stop. There it goes. Okay, now if we go and look at the expense desk, we have the screen that will let us continue with an authenticated expense desk agent. So, if we go ahead and click continue as the agent, then we're going to ask it uh which I can just copy this. Which account is being used right now? And what is my approval limit?

以自身身份行事的代理The agent acting as itself

而审批限额本质上就是你作为用户获准为他人的开支进行审批的金额上限。这里显示我登录的身份是费用处理代理 K Mida;它在 Keycard 中,而 Keycard 是我们充当其身份的那个特定应用,我们为它提供了 client ID 和 secret。该代理的审批限额约为 50 美元。接下来,我要让代理以我的名义提交一笔 75 美元的费用,就说是为研讨会买一些周边产品吧。于是代理提交了这笔费用。备注中说这是代表 Kim 提交的,但实际活动详情显示,这笔费用是由这个特定代理提交的。因此,“代表 Kim”只是我告诉它的内容。它无法核实这一点,因为我作为用户并没有登录;它是以自己的身份创建了这笔费用。And the approval limit is essentially the amount of money that you as a user are allowed to approve for other people's expenses. So, it says I'm signed in as the expense desk agent K Mida, which was in Keycard that was the uh specific application that we are acting as that we provided the client ID and secret for. And the approval limit for the agent is about $50. So next I am going to tell the agent file a $75 expense or let's just say for some swag for the workshop on my behalf. So the agent submitted that expense. The memo says it was it filed on behalf of Kim but the actual activity details indicate that this was filed by this specific agent. So the on behalf of Kim is just something I told it. It has no way to verify that because I as a user have not signed in it created this expense under its own identity.

好的。那么现在如果我们进入“应用程序”,其实我本来可以把那个页面留着。哦,它就在那儿。Okay. So now if we go into our applications actually I probably could have left that up. Oh, it's right there.

读取审计日志Reading the audit log

我们可以在 Keycard 中查看活动记录。这本质上是 Keycard 生成的审计日志,记录了针对这个应用发生的所有活动。这里有好几种不同的资源,所以我想专门查找费用 MCP 资源。对于所有调用费用 MCP 资源的活动,我会将时间范围限定为最近一小时。我们可以看到,系统通过客户端凭据授权模式签发了一项凭据,这意味着发起操作的是应用本身,而且它访问了 MCP 资源。现在,我要查看之前已经为你注册的内容,也就是费用 MCP actor。费用 MCP 服务器也有多个注册项,因为它既可以接收来自代理应用的令牌,也可以将该令牌交换为下游令牌,以访问 ledger API。We can see the activity in Keycard. This is essentially the audit log that Keycard generates of all the activities that have been happening against this application. And I am going to look for specifically because there's uh several different resources here. To look for the expense MCP resource. Anything that was calling the expense MCP resource, I'm going to limit this to the last hour. And we can see that there was a credential issued using the client credentials grant, which means that the actor was the application itself, and it was accessing the MCP resource. So now I'm going to go and look for what we have already registered for you, which is the expense MCP actor. So the expense MCP server has multiple registrations as well because it can receive tokens uh from the agent application, and then it can exchange that token on for a downstream token to access the ledger API.

所以如果我们点击这个,现在,因为大家都在使用 MCP 服务器的同一个资源,我们就把结果筛选为它调用 ledger API 的记录。其实这些记录我都可以用,不过,哎,这条查找的是人员。所以,如果我们看一下 expense MCP 服务器执行过的一次调用,它通过这次调用交换了从代理应用收到的令牌,以便访问 ledger API。我们可以看到,委派链显示,发起请求的应用是 expense MCP 服务器,而且它是代表这个应用执行操作的。它访问的资源是 ledger API。所以,这本质上就是代理以自身身份执行操作时的委派链记录。我现在就勾选这条记录。So if we click on this one, now we have uh because everybody is using the same resource for the MCP server, we're going to go ahead and just filter it down to it calling the ledger API. And I could technically use any of these, but oops, this is looking for people. So if we look at one of the calls that the expense MCP server used executed in order to exchange the token that it received from the agent application to access the ledger API. We can see that the delegation chain shows that the application that made the request was the expense MCP server and that it was acting on behalf of this application. And the resource that is accessing is the ledger API. So, this is essentially the record of the delegation chain for the agent acting as itself. I'm going to go ahead and check that.

在不冒充他人的情况下添加真人用户Adding human users without impersonation

好的。现在我们有了一个能够识别自己并以自身身份采取行动的智能体,但我们可能希望它代表我们采取行动,同时又不冒充我们。所以,我们不想让它只说“Kim 做了这件事”,而是希望它说“这个智能体在 Kim 的授权下代表 Kim 做了这件事”。接下来,我们要把人类用户身份加入这个流程。现在,我们需要在新标签页中加载费用管理页面,因为嵌入沙盒中的 iframe 无法处理外部身份验证。嗯,我们现在就在外部窗口中打开它。把这个放到下面。现在右上方有一个“登录”按钮。我来点击一下。它正在转圈。好了,因为我之前已经登录过了,所以它自动帮我登录了。Okay. So, now we have an agent that can identify itself and act as itself, but we maybe want it to act on our behalf but without impersonating us. So, we don't want it to just say Kim did this. We want it to say this agent did this thing on behalf of Kim with Kim's permissions. So, the next thing we're going to do is we're going to add human user identity into this process. So, now we have user delegation. At this point, we need to load the expense desk in a new tab because the iframe that's embedded into the sandbox doesn't handle external authentication. Um so, we're going to go ahead and open that in external window. Put this down here. And now we have this sign in button up on the right. I'm going to click that. If it's spinning. Okay. So, because I was already logged in, it went ahead and logged me in automatically.

嗯,不过现在我可以这样说:我们能看到我的邮箱地址在这里。如果我问智能体“我现在使用的是哪个账户?”,之前它会说“你正在以这个智能体的身份行事”,而现在它知道我有自己的身份,因为我已经作为人类用户完成了身份验证。好。与我的身份元数据相关联的审批额度是 100 美元。我还没有以人类用户的身份提交过任何费用。所以,如果我把这个筛选条件改为显示所有费用,就能看到,之前的这些费用仍然显示是由 API 密钥提交的。也就是说,添加所有这些信息不会追溯性地更改过去发生的事情。我们还可以看到一些示例,说明 Expense Desk 智能体也会代表其他人行事。因此,现在我们既可以以人类用户的身份行事,也仍然保留了让智能体以自身身份行事的能力。Um but now what I can do is I can say we can see that my email is here. If I ask the agent which account am I using now, before it said you're use you're acting as this agent, now it knows that I have an identity because I authenticated as a human user. All right. And my approval limit, which is associated with my identity metadata, is $100. So, I haven't filed any expenses under my human user yet. So, if I change this filter to show all expenses, you can see that these expenses from earlier still um say that they were submitted by the API key. Like, adding all of this doesn't go back and change anything that's happened in the past. And we can see, you know, examples of the Expense Desk agent acting on behalf of other people as well. So, we have the ability now to act as ourself as a human user, but we have still retained the ability for the agent to act as itself as well.

审批限额及各项审批的批准人Approval limits and who approved what

该代理有权批准低于某个金额的费用。所以,如果我……我就来做这一笔。如果我提交一笔比如 30 美元的团队办公用品费用,它就会验证我的身份,然后提交这笔费用。The agent has the capability to approve expenses that are under a certain amount of money. So, if I I'm just going to do this one. If I submit an expense for, say, $30 for office supplies for the team, it's going to verify who I am, it's going to file the expense.

然后,因为这个智能体实际上有权自行批准低于 49.99 美元的费用,所以它提交了这笔费用,随后又以自己的身份批准了它。如果我点击活动记录,查看这里发生了什么,会看到这笔费用是我用自己的邮箱创建的。然后,它由费用处理智能体批准了。你知道,记录显示了执行批准操作的智能体标识符,并给出了理由:符合条件的办公用品,金额低于 50 美元,且有明确的业务用途。呃,现在也许我想……我们也来处理这笔 75 美元的费用。如果我提交的费用超过了智能体可以自行审批的限额,那么这笔费用的状态就会是待处理,需要由人工审核。如果我去说:“嘿,呃,我觉得这笔费用应该获批。”And then because the agent actually has the ability to approve expenses under $49.99 on its own, it filed the expense and then the agent approved it as itself. So if I click on the activity to examine what happened here, it says that this expense was created by me under my email. And then it was approved by the expense desk agent. So it shows, you know, that this is the identifier for the agent that acted in the approval and it gives a reason says eligible office supplies under $50 with a clear business purpose. Uh so now maybe I want to Let's do this $75 one as well. So if I submit an expense that is over that approval limit that the agent can act on its own, then the status for that one is pending. A human needs to review it. If I go and say, "Hey uh I think this should be approved.

所以我会给代理发送这样一条消息:“嘿,批准费用 123。”它会说:“我不能批准或拒绝自己的费用,所以我得请另一位审核员来处理。”它之所以能做到这一点,是因为我们现在有了身份信息。我们知道所有用户是谁、谁已登录,也知道代理本身的身份。呃,之前我们还没演示这一点,但以前我可以批准自己的费用,也可以批准其他任何人的费用,因为系统不知道任何人是谁。那我来这里看看。找一笔其他人的待处理费用。我会说,呃,我想拒绝这一笔。代理会走完那一套流程。现在,如果我们查看这笔费用的活动记录,会看到它是由费用处理代理创建的,呃,因为这笔费用来自之前那一轮。So I'm going to send the message to the agent that says, 'Hey, approve expense 123.'" It says, "I can't approve or reject my own expense. So I have to ask another reviewer to do that." And it's able to do that because we have identities now. We know who all of the users are, who are logged in, and we also know the identity of the agent itself. Uh so before we didn't demonstrate this, but before I could have approved my own expenses or anybody else's because it didn't know who anybody was. So if I go here, let's see. Let's find somebody else's pending expense. I'm going to say, uh, I want to reject this one. The agent goes through that song and dance. And now if we look at the recorded activity for this expense, it says that it was created by the expense desk agent, uh, because it was from kind of the previous cycle.

而且它被我的身份拒绝了。这样一来,我们现在就有了一份清晰的记录,显示谁提交了什么、谁批准或拒绝了什么。此时如果我们进入 Keycard 控制台,点击“应用程序”,就会找到 Kitematic 应用程序。And it was rejected by my identity. So, now we have a kind of a clear record of who filed what, who approved or rejected what. So, if we go into the Keycard console at this point, go to applications, we are going to find the Kitematic application.

追踪委派链路Tracing the delegation chain

我要清除之前的所有筛选条件。我想它们已经清除了。我要输入 actor,然后查找我的电子邮件。把这个改成 hour。如果我点击其中一个已签发的凭证,就能看到,发起令牌交换请求的应用是我的应用代理应用 Kitematic,它代表我这个人类用户 Kitematic Keycard(或 Kitematic Keycard)行事,并访问了 expense MCP 资源。所以,这是第一跳,对吧?现在,如果我们再次打开应用程序,想要查找 expense MCP actor。其实我觉得不需要筛选任何内容,所有内容都在这里。我们打开一个为访问 ledger API 而签发的凭证。现在可以看到,在第二跳中,expense MCP 服务器代表我发起了请求,并访问了 ledger API。I'm going to clear any previous filters. I think they're already gone. I'm going to say actor, and I'm going to look for my email. Change this to hour. And if I click on one of these issued credentials, I can see that the application that, uh, made the token exchange request was my application agent app Kitematic, and it was acting on behalf of me, the human user Kitematic Keycard or Kitematic Keycard, and it was accessing the expense MCP resource. So, that was the first hop, right? So, now if we go over to the applications again and we want to find the expense MCP actor. Actually, I don't think we need to filter anything. It's all here. And we go to a credential that was issued for access to the ledger API. We can now see that on the second hop, the expense MCP server made the request on behalf of me and it was accessing the ledger API.

那么,在第一跳中,发生的事情是:代理拿到了我以人类用户身份登录时获取的登录令牌,然后它向 Keycard 发起令牌交换请求,提供这个登录令牌,以换取链条中下一环节(也就是 MCP 服务器)的令牌,对吧?因此,它把主体令牌交换成了 MCP 服务器令牌。接着,它拿着这个 MCP 服务器令牌调用 MCP 服务器,使用具有相应工具作用域的权限来调用工具。然后,MCP 服务器拿着从代理那里收到的令牌,向 Keycard 发起令牌交换请求,请求用这个令牌换取一个能够访问 ledger API 的令牌,而 ledger API 就是流程中的下一个资源。你可以追踪这些跳转,也可以跨跳追踪这些操作,因为它们都关联着同一个会话。So, on the first hop, what happened is the agent took my sign-in token that it acquired when I logged in as my human user and it exchanged the it made a token exchange request to Keycard offering that sign-in token in exchange for a token for the next thing in the uh chain, right? Which is the MCP server. So, it exchanged the subject token for an MCP server token. It then took that MCP server token and called the MCP server to make tool call with the appropriate tool scoped scopes. And then the MCP server took that token that it had received from the agent and it made a token exchange request to Keycard asking to give that token up for a token to access the ledger API, which was the next resource in the stream. And you can trace these hops. You can trace these things across hops because they have a session associated with them.

所以,我们不会花太多时间深入研究并复制会话编号,不过你可以根据会话编号追踪任何特定的事件序列。第一次跳转和第二次跳转时,会话编号都会保持一致。我可以把这些都关掉。现在交给 Mason。So, we're not going to spend a whole lot of time like digging into the session numbers and copying them, but you can follow any specific sequence of events uh by the session number will be consistent on the first hop and also the second hop. I can close all of that. And I will now give it up to Mason.

梅森·埃格:太棒了。非常感谢你,金。我有一只小小的毛绒水熊虫齐吉,它总是陪着我到处跑。我们有很多只,所以离开前记得拿一只。带回家给孩子,或者留给自己都行。不用觉得不好意思,想怎么做就怎么做。Mason Egger: Awesome. Thank you so much, Kim. I have my little plushy tardigrade Ziggy goes with me everywhere. We have plenty of them. So, make sure you grab one before you leave. Take one home for your kids or for yourself. No shame. Whatever you'd like to do.

Temporal:面向智能体的持久化执行Temporal: durable execution for agents

我们不只是会介绍如何把所有这些安全机制之类的东西构建到我们的智能体中。接下来要讨论的是,我们该如何部署这些智能体,确保它们能够执行完毕,不会失败;即使失败了,我们也知道该如何处理,并且能够可靠地让它们恢复运行,这样就不会丢失整个流程,也不会浪费大量时间和算力。这就是 Temporal 发挥作用的地方。We won't just walk through how to build all of this security and stuff into our agent and stuff. But now, how do we deploy these agents to ensure that they execute to their completion, that they don't fail or when they do fail, we know how to handle that, and we know how to reliably bring them back up so we don't lose all of our process and waste a whole bunch of time and compute. And that's where Temporal comes into play.

Temporal 是一个持久化执行框架。简单来说,它会执行你的代码,并维护应用程序的状态。如果应用程序发生故障、遇到错误或出现类似情况,它会重建或恢复到故障发生时的状态,然后继续执行。对于函数调用等操作,如果分布式系统发生故障,框架还内置了重试机制以及各种故障处理模式。 我和客户以及用户交流时,常常听到一个需求:我们需要一种让智能体具备持久性的方式。实际上,智能体循环——或者说智能体本身——就是一个循环,不断调用外部服务的工具。归根结底,智能体就是分布式系统,只是在此基础上又加了一点思考的“调味料”。Temporal is a durable execution framework. So, essentially what it does is we take your code, we execute it, we maintain the state of the application. If the application were to fail, run into an error, things of that nature, it will reconstruct the state or rebuild the state to the point of failure and continue executing onwards. Certain things such as function calls and things if say a distributed system were to go down, there are built-in retries and all sorts of failure handling modes. So, one of the things that we've seen a lot of with the customers that I work with and with our users is that we need a way of making your agents durable. And in reality, the agent loop is or the agent itself is just a loop. It's a it's a loop of ever calling tool calls to external services. Agents are just distributed systems at the end of the day with a little bit of added spice on top with some thinking.

不过,我们为分布式系统解决的问题同样适用于智能体循环,而 Temporal 正好能派上用场。它非常适合用来保持应用持续运行,这样就不会丢失数据,也不必再次调用大语言模型,呃,也就不用重新消耗 token 之类的。所以,你的用户不会遇到任何形式的服务中断。它还能带来各种很棒的好处。 接下来,我们会简单聊聊 Temporal 应用代码该如何组织。这里我先放着,方便你之后阅读。我不会深入讲解代码本身。简单来说,Temporal 是一个系统,你可以在其中构建工作流,并通过所谓的活动(activity)来定义工作流。可以把它想成父函数调用子函数,这是最容易理解的方式。But the problems that we've solved for distributed systems apply to agent loops, and this is where Temporal comes in. It works out really well to keep your application running and going so you don't lose data, so you don't have to call the LLMs again, uh tokens and all of that nature. So, you So, your users don't experience any sort of outage. All sorts of really awesome things with that. So, what we're going to do a little bit is just kind of talk about a little bit of how you would structure uh Temporal application code. I'll leave this here for you to read later. I'm not going to dive super deep into the code itself, but essentially Temporal is a a system where you can you build a workflow and you define it by things what are called activities. Think parent function and child function calls. Easiest way to think about it.

我们把循环实现为一个父函数调用,然后它就会变成一个工作流。每一个工作流实例或执行过程——比如每次我们让 keycard 通过并为用户进行身份验证时——都会是一个工作流。每一次工具调用本质上都是一个活动。这些操作可能会失败,而且我们预期它们会失败;失败时,我们要么重试,要么重建状态,看看会发生什么。所以,我们这里有工作流调用。我还会把 Mac 鼠标的方向反过来。所以,如果我操作方向错了,那是因为我没在用自己的笔记本电脑。我会重新映射电脑上的所有设置。然后在这里往下看,你实际上能看到像 `async def SS` 这样的代码。这实际上就是一个活动的样子。所以,它看起来就像普通的 Python 代码。We implement our loop uh as a this parent function call and this becomes a workflow. And every single uh instance of or execution of this uh workflow, so like every single time we would have keycard go through and uh authenticate for user, that would be a workflow. And every single tool call is essentially an activity. These are things that can fail, that we expect to fail, and when they do fail, we either retry them or we rebuild the state and see what happens with it. So, we have our workflow calls here. I'm also I do the other direction for Mac mouse. So, if I go the wrong direction, it's cuz I'm not using my laptop. Um I remap everything on my computer. Uh and then down here you actually see like this async def SS. This is actually uh what an activity would look like. So, you it looks like normal Python code.

看起来就是你通常会编写的代码。Temporal 是一个以代码为先的系统,所以你可以构建所有需要的内容,并将它接入任何你想接入的东西。嗯,我们有一个结算代码工作流,我就不详细读了,留给你自己看吧。不过再说一次,这是一个 Temporal 工作流,它基本上可以让我们设置重试的频率,然后执行各项活动。这里有我们的借方账本,还有标记为已结算的操作。这样我们就能把内容添加到账本中,然后将其标记为已结算。中间这里有一个计时器,用来模拟人工介入时需要等待的一段时间,也就是等某人来处理这件事。你可以看到,其中一个与此相关的活动就是这里这一段,它就是很典型的代码。It looks like the typical code you would write. Temporal is a code-first system, so you can build all of your things and hook it into anything that you would want. Um so we have our settlement code workflow, which I'm not going to read through too much. I'll leave that to you for you. But again, it's a Temporal workflow that uh essentially allows us to set how often we want to retry, and then we execute the activities. We have our um our debit ledger right here and our mark settled. So, we're able to, you know, add something to the ledger and then mark it as settled. And we have this timer here in the middle for us to kind of simulate some like uh human-in-the-loop time that it would wait take for someone to do this. Uh and then one of our activities uh for this, as you can see here, is uh this section right here, which is just the typical code.

不过我想先让你把目光放到最上面的第一个装饰器上,也就是 grant resource。Temporal 在其代码库中提供了许多原生的原语,其中之一叫作拦截器。它允许你在代码中的特定位置执行一些操作,可以把它们理解为钩子。这实际上是 Keycard 团队实现的功能:每当这个活动执行时,它都会去 Keycard 获取权限和身份,然后再返回。这样,我们就能通过它在 Temporal 工作流和活动中进行身份验证,并保持 Kim 刚才提到的身份隔离;也就是说,每个工作器都可以拥有自己的身份。But I want to draw your line your eye to the first decorator on top, which is grant resource. So, Temporal exposes a lot of native uh primitives within its code base, uh one of which is what's called an interceptor. So, it allows you at certain points within the code to allow for essentially think of them as hooks. So, this is actually something that the Keycard team uh implemented, which is where whenever this um this activity is being executed, it actually goes and gets permission and gets an identity from Keycard, and then comes back, and essentially now we are able to authenticate this through uh our Temporal workflow and our activities through this, and we can maintain this identity separation that Kim was talking about, where you can have each individual worker have its own one.

你可以让一组这样的实例各自拥有自己的配置,不过你可以通过它们来识别并限制范围、访问权限,以及所有这些很棒的功能。嗯,接下来我们要做的基本上是找到我们的工作……我想我们已经在 Keycard 中完成了 worker 的身份配置。对。好,那么我们就启动 worker。实际上,我们已经设置好了 Temporal 应用,也配置好了所有这些环境变量。那我们就启动 worker 吧。Temporal 采用 worker-server 模型来执行任务。你……有一个 Temporal 服务负责维护执行状态、应用状态,以及所有这些内容。然后还有一组 worker。你可以把它们想成 Kubernetes Pod,或者 Pod 内的进程;当请求到来时,它们就会处理你的工作。You can have a fleet of them have their own, but you'd be able to identify and limit scope, access, all of those wonderful things uh through there. Um so, the next thing we're going to do is we're basically going to find uh our work I think we already did the workers' identity in Keycard. Yeah. Okay, so we're going to start our worker. So, we actually already set up our Temporal application and set up all of these uh environment variable configurations. So, let's go ahead and start uh our worker. So, Temporal is a worker-server model a version of execution. You There's a Temporal service that maintains the execution, the application state, all of that. And then there's a fleet of workers. You can think Kubernetes pods, processes within a pod that your work as it's being uh as it gets as the request comes in.

那么,接下来我们转到启动 worker,然后就在这里启动第一个 worker。So, from here we'll go to start worker, and we'll just start our first worker right here.

在运行过程中终止一个 worker 并恢复运行Killing a worker mid-run, and recovering

我们有一个正在运行的 worker,PID 为 572。现在,我们有一个自动运行的 Temporal worker,它运行的是上一个练习中刚刚完成的代理,会自动审批我们的费用。我们的 worker 就在这里运行,可以看到 PID 是 572。接下来,我们可以发起一笔结算。我们要做的是点击这里的客户端选项卡,然后调用一次结算流程。随后,我们会看到结算流程的工作流 ID 已显示出来。如果我们再回到这里的 worker 选项卡,就能看到 Temporal 应用程序的输出,它会记录正在执行的操作。如你所见,它目前正在处理:我们为这次调用扣除了一个令牌,又创建了一个令牌,然后正在运行流程。We have worker PID 572 running. So, now we have a uh automatic uh a Temporal worker that's running the agent that was just done in the previous thing in the previous exercise, which will automatically uh accept our uh expenses. So, we have our worker running right here, and we see our PID is 572. And then we can start a settlement. So, what we're going to do is we're going to click on this client tab here, and we're going to invoke a settlement uh that's going to happen, and we'll see our workflow ID settlement came up. So, if we go back to our worker tab here, we'll actually see the output of our Temporal application as it's logging out what it is doing, and it's currently doing it as you can see here, we debited a token um and minted it for this particular call, and then we're running it.

如果我们打开 Temporal Web UI,在这里刷新一下,就能看到我们的应用正在运行。我得稍微快点操作,然后就可以继续讲了。哦,不对,不是 Control……是这个。好了。你们这些 Mac 上的 Command-Control-C 按键组合对我来说真不好用。刚才我们的应用正在运行。我们说过有一个借方分类账。我直接强制终止了应用。所以你们可以看到,这个应用的运行状态已经消失了。我们不再有它了。在传统系统中,这可能会带来问题。我们得把状态保存到数据库里,然后等系统重新上线时再把它恢复出来。但在 Temporal 中,这不是什么问题,因为应用的状态就存储在这里面。所以,最开始的借方分类账函数把输出存储在了这里。If we go to our Temporal Web UI, and we refresh this here, we can actually see our application running. I have to do this a little quickly, and then I can continue talking. Oh, no, it's not control Is it There it is. Okay, command-control-C you Macs is not fun for me. So, we had our application running. We said we had our debit ledger. I killed the application cold. So, as you can see here, the running state of this application is gone. We no longer have it. In a traditional system, this would be potentially problematic. We would have to you know, either have saved that state to a database and then rehydrate it when it comes back online. But, uh with Temporal, that's not really a problem because to the state of our application is stored right here inside of it. So, the very first uh the debit ledger uh function stored the output right here.

所以,我们有一个重试策略,呃,活动任务已完成。我们扣款了 4,200。所以这部分已经完成了。现在没有任何任务在运行,因此它会开始报错并告诉我:“嘿,没有任务在运行。我们的工作流任务已超时。”我们的应用程序已经停止运行。呃,然后我们回到结算过程中的 worker。准备好后,我们只需重启 worker。应用程序就会继续执行,就好像故障从未发生过一样。它根据之前的所有响应,重建了整个智能体状态。没有重新发起任何 LLM 调用。之前的活动或函数也没有重新执行。它恢复了状态,重放了操作,然后继续推进,最终成功执行了这项任务。So, we have a retry policy uh activity task completed. We debited 4,200. So, we have that done. There's nothing running right now, so it's going to start complaining and telling me, "Hey, you have nothing running. Our workflow task has timed out." We have a dead application. Uh and then we go back to our worker mid-settlement. Whenever we're ready, we can just restart the worker. And the application picks up as if the if the failure never happened. It rebuilt the entire state of our agent from all of the previous responses. No LLM calls were redone. No previous activities or functions were re-executed. It has picked up the state, replayed the actions, and then pushed it through, and we got the execution to work.

为什么持久性对智能体至关重要Why durability matters for agents

那么,为什么持久性对智能体很重要?嗯,因为你的智能体是由循环构成的。嗯,你的智能体里现在有很多东西都很烧钱。大规模调用 LLM 并不便宜。你还得处理它们的 token。会遇到很多失败状态,也会遇到很多复杂状态。比如说,你经历了一个很长的循环,已经进行到第 12 步了,却丢失了整个应用程序的状态。这样一来,你要么基本上得自己构建一个 Temporal 版本,在某个数据库里维护应用程序的状态,然后还得构建所有状态重建逻辑和重试逻辑;要么就可以使用 Temporal 这样的持久化执行系统,它会帮你处理所有这些事情。So, why does durability matter for agents? Well, uh your agents are loops. Um there's a lot of things inside of your agents right now that cost a lot of money. Making LLM calls on a massive scale is not cheap. There's their tokens you have to deal with. There's a lot of failure states. There's a lot of advanced states. Say you get through a really long loop and you know, you're 12 steps in and you lose the entire state of that application. Now you either have you've either had to built basically built a version of Temporal yourself where you are maintaining the state of your application in a database somewhere, and then you have to build all the reconstruction logic and all the retry logic, or uh you can use something like a durable execution system like Temporal, and it will do all of this for you.

因此,这让你的 agents 可以离线、发生故障,LLM 提供商也可以宕机,你甚至可能失去 AWS 的整个区域。这些都无关紧要:等计算资源恢复、那些资源重新上线后,agents 会从上次停下的确切位置继续执行。如果故障恢复得足够快——也就是故障持续时间足够短——用户甚至不会察觉服务曾经中断过。嗯,如果你刚才是那个 worker 调用的接收方,我们等了一分钟;你知道自己要等一分钟才能获得审批。假如那时发生了故障,而我在故障发生后大概 5 秒内就重启了 worker,那么作为客户端的用户,你可能根本不会注意到发生过这种故障。这就是 Temporal 的作用。So, this allows for you to have your agents go offline, fail, your LLM provider can go down, you can lose an entire region in AWS. Doesn't matter when they when those when that compute comes back when those resources come back online, the agents pick up right where they left off at the exact last spot and continue execution. And if it happens quickly enough, if it's a quick enough failure, your user will never even be aware that they that they lost service of this. Um if you had been on the receiving end of this worker call that I had just done where we waited a minute and you were knew you were going to wait a minute for the approval and that failure happened and it happened and I restarted the worker within, you know, 5 seconds of the failure. As a user on the client side, you probably never would even notice that there was this kind of failure and that's what Temporal does.

它是不是会帮你把它一路完整地输出出来,然后无论如何都让执行继续进行?Is it brings it all the way out uh for you and just keeps the execution going forward regardless.

避免在事件历史记录中泄露机密信息Keeping secrets out of the event history

有意思的是,嗯,这一切的密钥实际上并没有存放在我们的 Temporal 事件历史记录中。所以,如果我们回到这里的客户端并运行这条命令——哎,我按错按钮了。哦,我得输入正确的工作流 ID。要是不输入,就行不通。如果我回到这里,把这个工作流 ID 复制出来。呃,它在客户端标签页里。对。然后我们点击向上箭头。现在,希望我还记得怎么在 Mac 上粘贴。啊哈,成功了。很好。我们可以看到这里有一个小小的 Python 脚本,它显示 JWT 令牌材料基本上没有被发送。我们并没有把 worker 的活动记录到这里的整个事件历史中,嗯,你可以看到启动过程和结果。所以,如果我们点进去,就能看到这里的借记账本,结果就在下面。Now, the interesting thing is um the secrets for all of this were actually not inside of our Temporal event history. So, if we go back to our client over here and we run this command, oop, I hit a button. Oh, I have to actually put in the correct workflow ID. If I don't, it doesn't work. If I go back here and collect this workflow ID. Uh it's on the client tab. Yeah. And we click the up arrow. Now, we're going to hope I know how to paste on Mac. Aha, we got it. Good. So, we can actually see we actually have a little Python script here that says that no JWT token uh material uh was basically sent. We didn't like our worker through the entire event history here, um you can see the starting process and the result. So, if we click in, I can see the debit ledger here with the result down here.

所以,我们每个单独的函数都存储在那里。这里的第二个函数 mark settled 也已经结算了。这就是它的结果。但如果我查看工作流中的实际事件历史——这实际上就是事件历史在底层的样子——你会发现里面完全找不到它,因为我们没有存储它。嗯,Temporal 基本上提供了两种不同的方式来避免敏感数据被记录下来。一种是拦截器,也就是 Keycard 为我们提供的方案。他们构建了一个拦截器,基本上会在执行时注入凭据,同时完全避免将数据和凭据存进去。你可以把它想成一种即时凭据机制。Temporal 还有另一项很棒的功能,就是所谓的负载编码器。So, we have our uh each individual function is stored there. So, our second function here, mark settled, was also uh settled as well. So, this was the result of it, but nowhere in our workflow if I go to the actual event history, which is this is actually what the event history looks like under the hood, you will not find it anywhere uh because we didn't store it. Um so, Temporal gives you basically two different ways uh to keep the sensitive data out. So, we have the interceptor, which is what Keycard provided for us. So, they built an interceptor that basically injects it at uh keeps the data and the credentials out of it entirely and then injects it back in when it's time to execute. Think of it like almost like just-in-time credentials uh and does that. Or the other thing for Temporal that's really awesome is there's what's called a payload uh encoder.

那么,你可以做的是——你刚才在我们的 Temporal Web UI 里看到,你的数据就在……好了,按错按钮了。你的数据就这样以清晰易读的纯文本形式放在这里。你可以自己对这些数据进行加密。基本上,你可以启动一个……Temporal 里有一整套模式和库。你启动一个 sidecar,创建一种加密算法,设置加密策略,这样所有传入 Temporal 的数据,无论是自托管还是在云端,基本上都会被加密。我们无法读取这些数据。我们不掌管你的密钥。说实话,我们也不太关心你在里面做什么。这不是我们关心的部分。我们关心的是把所有这些很棒的东西组织起来。以上就是对 Temporal 以及如何用它构建智能体的简要介绍。So, what you can do is you saw in our Temporal Web UI, your data was There we go. Wrong button. Your data is just sitting here in nice wonderful plain text. You have the ability to encrypt this yourself. So, you can basically spin up There's like a There's an entire pattern and library in Temporal. You spin up a sidecar, you create an encryption algorithm, you set what the encryption policy is, and all data that comes to Temporal, whether it's self-hosted or in the cloud, is essentially encrypted. And we can't read it. We don't control your keys. We honestly don't quite care about what you're doing inside of it. That's not the part that we care about. We care about putting all these fancy things in order. So, that's a quick overview of like Temporal and building out it building out agents with it.

你可以尝试很多与 Temporal 集成的方案,比如 OpenAI、Pydantic AI。我知道还有 LangChain,以及其他很多方案。嗯,我想我们现在甚至还在开发一个智能体运行框架,也即将推出。如果你想了解更多,欢迎来我们的展位找我们。嗯,是的,关于费用管理平台,基本上我们……我会请他回来做个总结,如果大家愿意,我们也可以回答一些问题。构建智能体身份,构建所有智能体工作进程以及相关内容,然后 Temporal 就会成为执行层,确保你的智能体正常运行,即使发生任何灾难性故障、API 中断,或者 AWS 某个区域突然决定要宕机一天——这种情况确实会发生。There's a lot of integrations with Temporal you can try out. There's an OpenAI, Pydantic AI. I know there's LangChain and a whole bunch of other ones too as well. Um And I think we even now have like an agentic harness now that we've been building out that's going out too. So, you can come by and see us at our booth if you want to learn more about that. Um And yeah, so the expense desk, you know, basically we I'll bring him back up to kind of wrap it up and we can take some questions if you want. Building out the agent identity, building out all of the agent worker and all of that stuff, and then Temporal becomes the execution layer to make sure that your agent runs properly and doesn't fail in the case of, you know, any sort of cataclysmic failure, API outage, AWS region just decides it wants to disappear for a day, which does happen.

嗯,是的,我要说的就这些。So, yeah, that's all I have.

← 回到本期 · 第 8 条