AI Agents Are Just Distributed Systems Now — Salman Munaf, TikTok https://www.youtube.com/watch?v=hD9-V56FNRI AI Engineer · 19 min · 133 sentences · sentence-level transcript from YouTube's caption track; each line begins at the time shown [00:01] [music] [00:12] Hello everyone. [00:13] Good good afternoon. [00:14] Uh today uh I will be talking about AI agents are also distributed systems. [00:20] So as uh the models have started to become more complex. [00:25] Initially the uh LLM models were just text in text out without performing any actions and uh the effect that they can produce was just a wrong model output. [00:39] However, with now the capability of agent the agent the rise in agent capabilities where the systems can now talk to external systems uh it has turned into a distributed systems and it is important [00:56] to incorporate distributed systems thinking and concepts when building AI agents. [01:02] So I will be going over uh that uh in this talk. [01:07] So you guys might have uh heard about incidents being caused by a AI agents. [01:14] Uh for instance, the replicate AI agent deleting a production incident production database or Air Canada chatbot basically making an uh an incorrect refund. [01:28] And both of these uh incidents or a lot of these incidents could have been prevented uh by good systems thinking when building these uh AI agents. [01:41] So for instance for the replet a uh uh uh incident we could have good uh uh we could have robust backups. [01:53] We could have scoped authority. [01:54] we should we shouldn't ideally have uh allow AI agents to delete production databases. [02:00] Uh moreover for Air Canada chatbot it would have been uh a good idea to have uh authoritative source of truth retrieval so that it's not making uh uh decisions based on stale or incorrect policies. [02:19] So let's uh go over the transition from chatbot to production system. [02:23] Uh so uh initially when we were in the uh in the uh age where LLMs were just chat bots uh we had prompt in and we were outputting text [02:38] there were no side effects the agent was not interacting with any other system. [02:44] However, uh due to agentic uh in the agentic era in the agentic revolution, now those agents uh by ingesting prompt can uh run an agent loop, call external services, call tools and also perform state changes. [03:01] The architectural boundary now has moved uh way beyond an LLM model. [03:08] And the difference is that it can now cause side effects in the outside world. [03:13] So when basic when building AI agents, it is important to recognize the external systems that it is talking to, [clears throat] the states that uh it is interacting with [03:25] and what credentials does it have and the actions that it can perform. [03:32] uh I ideally like to think about it as uh uh AI agents as basically having a probabilistic coordinator. [03:42] In distributed systems as well, we used to have services which were coordinating uh multi-step workflows. [03:48] However, they were deterministic in nature. [03:51] But in the case of AI agent, the AI acts as a probabilistic coordinator. [03:56] The amount of action, the kind of actions that it can take can vary quite a lot. [04:01] It is not just a decision tree that uh we typically in traditional systems would have mapped out and those uh actions can have severe consequences uh ba uh if they are not confined [04:18] by our determinist by having deterministic controls in place. [04:23] So it is important to ensure uh that we have deterministic controls in place to ensure that agent or the AI agent is not performing any uh any actions that might be [04:39] uh uh problematic. [04:42] So uh let's uh discuss the how a typical agent loop might look like. [04:51] So at first it might uh do some planning. [04:56] Then based on that plan it will it will perform an action and it will then observe the results of those actions and uh it might persist that into some d some data store and then decide what to do next. [05:12] Each step in this loop is basically crossing a a boundary. [05:16] During planning, it can interact with data sources to retrieve some data. [05:22] Uh during action, it can call external APIs, tools, uh databases and perform any actions. [05:30] During observation phase, it can perform it can get partial results and basically plan or make subsequent actions based on those partial results. [05:42] It can persist incorrect data or uh and uh when deciding it might also decide to uh perform an incorrect action or uh worse it can also do a retry [05:59] storm. [06:01] So it is very important when building an agent loop to persist every step of the process. [06:07] Whatever actions the agent is doing, whatever context it is retrieving, it is important to uh persist that so that if anything fails, the agent is able to recognize where it failed and it can perform [06:20] uh a reversible action. [06:23] Uh it can perform undo operations. [06:25] Similarly, there should be explicit transactions uh identified for each step. [06:31] So for instance, if an agent is making a call, if it fails, what it should do? [06:36] What should be the transaction to compensate for a uh for a irreversible or unsafe operation? [06:43] For instance, if an agent makes sends an email to a a wrong email to a customer, what should it do to compensate for that? [06:53] So, uh tool calls are just wrappers around uh external external APIs, databases, cues, uh and so on. [07:06] And uh with uh when calling the when making these remote calls, there are some failures that you incorporate uh such as network delays, timeouts, uh you can make duplicate requests [07:21] or worse the server side request uh might succeed. [07:26] However, the client however the client might be reported an error. [07:30] We have we have seen uh instances where uh a data by base might have written the data. [07:38] However, due to some other errors, the server might have reported uh uh to us the error and uh with humans in the loop we can make we can basically [07:52] perform correct corrective actions based on uh by seeing uh the database and actual source of truth. [07:58] But in agent's case, we need to ensure that we have uh we have proper guardrails in place. [08:04] So for instance, an agent calls refund customer uh tool call which basically performs a refund to the customer. [08:12] The request times out uh that did the refund happen or not? [08:18] What will the agent uh infer from that? [08:22] Would it retry uh refunding to the customer? [08:25] you basically the the timeout does not actually mean that there a failure had occurred. [08:30] It means unknown. [08:32] And it is important to have uh when designing these tools, it is important to have request ids, item potency keys so that when making duplicate requests, they are not causing duplicate side effects. [08:47] uh and the system can always do a status lookup like what the previous request was and what was the status of that so that it is not making side effect [08:59] it is not making side effects with duplicate a with duplicate requests. [09:05] So [clears throat] AI agents when they whenever they uh uh whenever they uh they face failures they retry the their first uh action is to perform retries. [09:20] So it is really important to have item potency baked in. [09:25] uh it if a same request is coming in to an external API or the tool it should recognize that this is a duplicate request and ensure that no side effects are being take are taking place. [09:38] Moreover, uh we should also prevent uh AI agents to perform retry storms to external APIs because this can cause cascading failures. [09:48] uh we we should have max turns budget spend and max parallel calls to prevent uh to uh to ensure that the fan out is not that large. [10:00] Moreover, we should have exponential back back off in place to ensure that uh the downstream dependencies are not being uh burdened and we should also have compensation uh operations in place for [10:17] uh operations that uh that that can have side effects. [10:25] Uh so uh a lot of uh teams when building AI agents think of AI agent context as just a AI cont the context that uh the AI agent has as [10:43] uh as just a context. [10:45] However, when that context can influence an action, it's a state and that state can become stale that can conflict with the authoritative data or corrupt future actions that the agent might perform. [10:59] I like to classify it into two different types of uh memory that the agent has. [11:05] First is the short-term memory which is the jet thread uh that the agent has uh the which is tied to a single execution thread and the second is the long-term memory. [11:16] It can be project files uh system prompts uh databases that it interacts with the cache layer and so on. [11:25] It is important to uh to to decide what will be the source of truth when these uh different data sources have conflicting information and we should ideally treat memory as a cache [11:42] which uh can be invalidated which can have provenence attached to it. [11:48] So for instance whenever a data store or a database is updated or the source of truth is updated we in we invalidate the context or the memory that the agent has to ensure that it is not making actions based on the [12:03] uh incorrect or stale data. [12:08] So usually these agents perform multi-step actions and uh the agent can succeed on uh on uh on the first couple of steps and then it fail. [12:22] Uh it is important to reverse the entire transaction that was performed and these can uh then can cross system boundaries. [12:31] So for instance, an agent can update an internal ticket uh send an email to a customer and fail to update the CRM. [12:41] We need to figure out what is the uh correct compensation operation when it when it hits that failure. [12:51] So for instance uh as I mentioned earlier that uh it improperly uh it improperly sends an incorrect email to the customer. [13:02] It is important that the compensation operation is defined for the AI agent to ensure that it is sending an uh uh an apology email to the customer or any or or any email or an email that is correcting that mistake. [13:19] So uh a the AI agent basically runs in a loop and uh whenever uh like it can it can do multiple calls. [13:31] It can it can have a retry uh retry loop that it can run based uh whenever it fails. [13:39] So it is important to have uh circuit breakers whenever it is making making external calls uh to ensure that the uh that the that it is not uh burdening the downstream [13:53] system. [13:54] Uh for instance if a downstream is unhealthy there should be system break uh circuit breakers in place that prevents AI agents to call call that dependency. [14:03] Moreover, it also prevents cascading failures when for instance the downstream dependency is uh unhealthy or uh is saturated. [14:14] It is also important to assign rate limits and budgets. [14:17] Uh an agent can uh go over uh can run your cost uh if it's not assigned proper budgets and rate limits. [14:27] it will uh keep retrying and try try to uh try to solve the problem that if it if it's facing. [14:35] So it is important that it is uh that we have uh set up max turns, max parallelism, max spend uh to ensure that the model is not uh uh not [14:46] uh crossing the uh the budget boundary that we have set. [14:52] Moreover, uh ideally uh usually whenever we are building AI agents, uh we usually try to give all the permissions that it can have to ensure that it has all that it can perform perform the task that we have. [15:09] That's the that's the uh first uh uh thing that we have that that's the first step that we take usually that to give the AI agents all the uh cred all the uh privileges [15:24] to perform any actions like for instance if it's interacting with the database we just give it all uh the readr access to the entire table. [15:32] However, uh it is important to give scoped credentials to it. [15:38] There should be separate read and write permissions and there should be allow list for the tools that it can call. [15:45] A harmless model can become dangerous when it can perform unsafe operations. [15:51] Moreover, uh a human approval shouldn't be tied uh to a blanket approval. [15:57] It should be tied to uh action, timestamp, actor and expiration. [16:04] So for instance, if a user has given uh an approval to approve a $30 refund, it shouldn't turn into a subsequent approval for $300 refund. [16:17] It is important that whenever an approval is given, it should be tied to the particular parameters that it was uh asked for. [16:31] So, uh observability is an important requirement when building AI agents because uh and logs are not enough. [16:42] Teams need to reconstruct when an agent failed, what happened, what information was was it reacting to and why it failed. [16:51] And logs alone are not enough to uh for an agent to uh for teams to determine that. [16:59] It is important to trace the model that was called, the prompt that was uh that was uh given to it and uh also the tool calls that were made [17:09] uh the request uh that was made, the response from the tool, the errors that it got, the retrieved context, what the agent was uh was the the retrieved information that the agent was reacting to, the rights that it made, and the approvals that it got and so on. [17:31] So uh I would like to uh end with uh the idea that yes model capability matters. [17:40] Having good models uh improves the uh likelihood of it making uh correct operations. [17:48] Smarter models reduce mistakes. [17:50] It uh it uh improves the capability that the model has. [17:55] However, it cannot eliminate network failures, stale data or adversarial input. [18:02] It is important when building this architecture, we also reason about can we bound, observe and recover from actions performed by the AI agent. [18:14] It is important to have tool contracts in place to ensure that uh it is only allowed to make uh operations that it is uh given that it is provided the contract and the contracts are clearly establishing the request and response [18:29] uh response uh response types uh the schema and all these tools have item potency baked into it. [18:38] so that uh when repeated requests are sent in uh it is not causing unsafe operations to be retried. [18:46] Moreover, there should be source of truth decisions made uh when there are conflict conflicting uh memory states. [18:53] It is important uh for the agent to realize this is the source of data that it should rely on and we should have re retry policies uh like rate limits set in to ensure that the agent is not uh retrying [19:08] uh ext uh aggressively. [19:11] Moreover, uh permissions should be set up. [19:15] There should be traces and recovery paths. [19:18] So when building AI agents, we should also ask what the system lets it do when it is wrong. [19:28] Thank you. [19:46] [music]