Event-Driven Agentic Loop with Claude Code with Ryan Sweet
O'Reilly · 2026-01-26 · 11м 27с · 925 просмотров · YouTube ↗
Топики: ai-loop-engineering
🎧 Аудио
📝 Summary
model=deepseek-v4-flash · prompt=summary-v7 · 3 963→1 869 tokens · 2026-07-20 11:49:03
🎯 Главная суть
Event-driven agentic loop — архитектура, в которой агент (например, Claude Code) реагирует на события (запуск, остановка, вызов инструментов, отправка промпта) и может отправлять собственные события. Используя настраиваемые хуки start/stop, разработчик может реализовать рефлексию: агент анализирует транскрипт сессии, извлекает уроки и инжектирует найденные правила обратно в будущие сессии. Этот цикл «действие → рефлексия → улучшение» позволяет системе эволюционировать, а сам процесс помогает выявить необходимые guardrails.
Самонастраивающиеся агенты: фракталы из паттернов
Самый простой способ начать — взять популярные инструменты и SDK: Claude Code (наиболее популярен), RueCode, GitHub Copilot CLI. Если строишь сырых агентов на LangChain или AutoGen, всё равно можно дать им способность к саморефлексии. Ключевой принцип — «фракталы — твои друзья». Например, микро-паттерн «автор — критик — исполнитель» (три агента) может быть частью большего воркфлоу по разработке кода, который в свою очередь входит в систему создания презентации или сайта. Другой набор «автор-критик-исполнитель» может проверять выход нескольких параллельных потоков и давать обратную связь, которая спускается вниз по системе. Так паттерны вкладываются друг в друга, образуя масштабируемую архитектуру.
Как работает event-driven agentic loop в Claude Code
Claude Code действует как агент, реагирующий на события. Отправка промпта — событие, старт/стоп — событие, использование инструмента вроде Bash или запись в to‑do list — тоже события. Агент может не только принимать, но и отправлять события. Для разработчика доступны настраиваемые call и call hooks — программные обработчики событий. Anthropic опубликовал Claude Code SDK, который позволяет строить собственных агентов на этой архитектуре без использования интерактивного интерфейса Claude Code.
Рефлексия через stop hook
Конфигурация хуков хранится в файле settings.json. Можно указать произвольный путь для скрипта, который выполняется на старт и на стоп сессии. В stop hook Claude отправляет транскрипт всей сессии. Разработчик получает возможность обработать этот транскрипт. Интересно, что из самого хука можно вызвать Claude Code — например, попросить его проанализировать транскрипт по шаблону: «Был ли пользователь раздражён? Какие типы взаимодействий? Какие вызовы инструментов? Что можно улучшить?». Чтобы избежать бесконечного цикла (вызов Claude Code порождает новый stop event), используется простой семафор. После рефлексии агент заполняет шаблон с наблюдениями и предложениями по улучшению. Даже такой простой фидбек-луп оказывается весьма эффективным.
От рефлексии к эволюции системы
На базе описанного механизма можно строить гораздо более сложные системы. Например, автор создавал агентов, которые самостоятельно делали несколько поколений самих себя: рефлексировали над выполненными задачами, модифицировали собственный код и снова принимались за задачу — и так по циклу. Такой подход, если экспериментировать в безопасной среде (Docker-контейнер или VM), отлично выявляет, где системе нужны guardrails: агент находит все неожиданные граничные случаи.
Session start hook: инъекция правил и guardrails
Помимо stop hook есть start hook. Если в ходе рефлексии были извлечены правила или ограничения, их можно внедрить обратно в новую сессию двумя способами: добавить контекст к пользовательскому промпту (если это нужно каждый раз на каждом шаге) или в системный промпт сессии (более глобально). Также можно сделать инжекцию только для определённых под-агентов. Это же позволяет кастомизировать поведение — например, задать, что агент всегда говорит «как пират». Практическая польза: если вы замечаете, что раз за разом даёте одни и те же инструкции (например, «не используй фейковые API, фейковые данные и нереализованные функции»), стоит вынести их в guardrails. Автор называет эту философию Zero BS — конкретные инструкции в начале сессии и повторная проверка в конце. Это предотвращает типичные ошибки кодогенерации.
Прогрессивное раскрытие и движение в код
В Claude Code есть механизм «скиллов» (skills) — тела знаний, к которому агент может обращаться по мере необходимости. Процесс развития агента — это постоянный перенос знаний из нечёткой семантической области (промпты) в детерминированную исполняемую область (код). Всё, что можно закодировать, должно быть закодировано, а в промптах остаётся только то, что трудно выразить кодом.
Альтернативы и общий принцип
Если вы не используете Claude Code, аналогичные возможности есть в AutoGen (который эволюционировал в Microsoft agent framework), OpenAI agent framework, а также в Claude Agent SDK (можно использовать без Claude Code). Но независимо от инструментария, ключевой подход остаётся тем же: решить, какие части системы должны обучаться и улучшаться, определить источники данных, построить циклы сбора информации, её анализа и инжекции результатов обратно в воркфлоу агента. И всё это делать в экспериментальном пространстве, чтобы понять, какие границы (guardrails) необходимо установить.
📜 Transcript
en · 1 728 слов · 24 сегментов · clean
Показать текст транскрипта
First off, I guess, how can you get started? I think the easiest way to get started building your own self-improvement systems is to go grab the tools and SDKs that folks are using. In this case, Cloud Code is by far, I think, the most popular one at the moment. And that's what I'm going to show you, one way to do that. Another one that makes this pretty easy is RueCode. Most recently, I think GitHub Copilot CLI is also like showing you a lot of opportunity for how to build in the type of hooks that I'm talking about. Even if you're just building raw agents with Langchain or Autogen or something else, you can still give your agents the ability to do that self-reflection. And the way to get there then is to really ask yourself those questions. Do that thinking about thinking and start putting small patterns together into bigger patterns and then put those together into bigger patterns. I like to say that fractals are your friend when it comes to designing agent systems. And one little example of that would be, let's say you have an author critic pattern, right? And maybe it's author critic and then person who implements the feedback from the critic and those three agents. are a pattern in the micro. And they might be part of a larger workflow for developing, say, a set of code. But then that might roll up to a larger system that involves that code in an overall creation of a presentation or a website. And you might have another set of author, critic, implementer that looks at the output of several different work streams together. right, and then gives feedback that flows all the way down to the bottom of the system and then flows back up. And that's what I mean by kind of building fractals of patterns upon patterns. So yeah, I want to dive in and show you a little bit about a few pieces here. How to do reflection, sort of leaning into building some tools and skills, and doing some customization for Cloud Code specifically and injecting context. We've got a little bit of time. So first, let's talk real quick about how Cloud Code and some of the other agentic coding tools work. Essentially, the way that Codex, GitHub Copilot CLI, Cloud Code work is an event-driven agentic loop. And Cloud is, I think, doing the best at illustrating this and making it easy for developers. It's an agent that it can respond to events, submitting your prompt as an event, starting, stopping, that's an event, using a tool like Bash or writing this to-do list, those are events. And it can also send events. And you can configure what call and call hooks, which is just a way to programmatically respond to an event. Anthropic has published their SDK, which is perfect for sort of building your own agents around this without using the Cloud Code interactive tool. And so I want to talk about reflection specifically. Reflection is where we can learn from how things are going and have a look at what happened during a session, reflect on that session, and then come up with some feedback. So QuadCode specifically keeps its hooks configuration in this file is called settings.json. And you can see here I have a start hook, I have a stop hook. Let me get my cursor back on the screen. There. Let me get this out of the way. And you can specify an arbitrary path for these things. And within that hook, then your job is to respond to the things that Claude sends you and decide what to do with it. And so here in the stop hook, Claude is sending us the transcript of the session that it had. And we get to operate over that transcript. And one of the things that's interesting is that you can call Claude code. from within the hook. So in this case, I want to run reflection on the transcript from the session, but I want to use Claude code to do that reflection. And so I've got a prompt that's going to ask it to fill out a template based on what happened in the session. Was the user frustrated? What kind of interactions? What tool calls did you do? Can you suggest anything that should happen? But you want to avoid an infinite loop because if you call Claude code and it's running reflection on your transcript, there's a stop event at the end of that. You just use a really simple semiflor here. I've got some code that sets up that semiflor. Then once we're actually running reflection, we invoke Claude with the Claude code SDK and we pass it the transcript. And then it has a chance to go and analyze the transcript with our prompt and come up with some feedback. And so really, really simple here. I just have it fill out this template. What did we do? What kind of things did you observe? And then there's some things you can improve. And you'd be surprised at just how effective that super simple feedback loop is. and then showing the user a set of things that they might make better. Now, this is just a really simple example that I cooked for this show, but you can make much more sophisticated examples, and there's a bunch you can find on my GitHub about how to do this more automated. I've built some systems that went off and made multiple generations of themselves by doing reflection over the tasks they were given and then going and modifying their own code and taking on the task again, et cetera, in a loop. What I found is that that's a really good way to discover where you need guardrails. If you can experiment safely in a Docker container or online VM and let a system evolve, finding all the ways that it goes into corners that you didn't expect ends up being a really interesting way to discover some of those guardrails. And so in addition to the stop hook, there's also session start hooks. And this is your opportunity, if you learn something from the reflection part of the session, to actually take things that you've pulled out, extracted as guidelines or guardrails, and inject them back into the session, whether it's adding context to the user prompt, which is good for something you want to do every time, every single turn, or adding context to the session as a whole. which is more like augmenting the system prompt, right? Or you can have a reflection injection that's for just very specific sub-agents, right? And then this is an interesting way also to do customization and to use hooks to kind of inject preferences. And so like a fun one that I like to play with is I tell the model that it always has to talk to me like a pirate, right? And you get some really interesting. conversations as a result, it makes interacting with the model a little bit more fun and you can go on. And like I said, the important part here is that this helps you figure out where you want to build guardrails and have to give it guidance. And so if you find in your prompting that you're giving specific instructions over and over again, and what I like to talk about is the zero BS philosophy where if you've done any bytecoding, then you know that without specific guidance, you have to tell the model over and over again that it should not do faked APIs or faked data or unimplemented functions, because it will do a bunch of that if not given that guidance. And so I have some really specific instructions in there. There are guardrails that help set that 0BS context at the beginning and then check it again at the end over and over again. exercise that process of identifying patterns building tools for those patterns moving them the tools to commands and from there you can build your own agents for specific types of tasks and then anyone that the plot is rolled out which you know i think is really interesting for its progressive disclosure are the cloud code skills but the basic idea is that you know there's a body of knowledge which an agent can access and And you can pull from that body of knowledge a little bit at a time as needed. But yeah, when something, we're constantly in the process here of moving things from fuzzy and semantic to deterministic and knowable. And when you can do that, anytime you can do that, move those things into code. Move things out of the prompt, the semantic world into code and leave the things that are less difficult to determine with code. And so what if you're not using cloud code? There's lots of other ways to do the same kinds of things. In Autogen, we sort of pioneered building event-driven agent frameworks, and that's now evolved into the Microsoft agent framework. That's a good place to look. The OpenAI agent framework also adopted a lot of the patterns from there. And there's several others that are out there and available. I strongly endorse the Clawed Agent SDK. You don't have to use Clawed code. Use the Clawed Agent SDK. But I find they've made things really easy. They kind of have built in a lot of solid stuff, and their tool use is great. But again, it goes back to doing that structured thinking about which pieces of the system do you want to learn. or do you want to have it better? What are the data sources you want them to look to? And then where are you going to build the loops that are going to collect the data that you're learning from, understand that data deeply, and then sort of inject it back into the agent's workflow? How is the agent going to get access to the things that you learn as you learn? And then doing all of that in an experimentation space where you can learn what are the boundaries or the guardrails I need to set.
⚙️ Pipeline jobs
| Stage | Status | Att. | Updated | Error |
|---|---|---|---|---|
| download | done | 1/3 | 2026-07-20 11:48:30 | |
| transcribe | done | 1/3 | 2026-07-20 11:48:40 | |
| summarize | done | 1/3 | 2026-07-20 11:49:03 | |
| embed | done | 1/3 | 2026-07-20 11:49:04 |
📄 Описание YouTube
Показать
Watch the entire Superstream: https://learning.oreilly.com/videos/ai-superstream-ai/0642572020243/0642572020243-video399662/?utm_medium=social&utm_source=youtube&utm_campaign=free+trial&utm_content=ai+superstream Building AI agents that improve over time means designing systems that can learn from their own actions. In this excerpt from the recent AI Superstream, Microsoft’s Ryan Sweet explores the event-driven agentic loop using Claude Code, showing how to create agents that reflect on their performance, adapt to feedback, and evolve through structured interaction. Check it out to discover how to configure hooks, inject context at key points in a session, and use reflection to guide future behavior. You'll also learn how fractal patterns help organize agent workflows across different layers of complexity. Whether you're using Claude or other frameworks like AutoGen or LangChain, Ryan offers practical takeaways for building flexible, autonomous systems that learn and grow. Follow O'Reilly on: LinkedIn: https://www.linkedin.com/company/oreilly/ Facebook: http://facebook.com/OReilly Instagram: https://www.instagram.com/oreillymedia BlueSky: https://bsky.app/profile/oreilly.bsky.social