hey everyone in this video we're going to explore a multi-agent or agentic workflows this system that I develop can write a multifile full games closing on 1,000 lines of code which work uh really well using on average about 10 agents at a time but there's really no limit on how many files project you can make or how many agents that this can spring into being let me just show you uh some of the games it has coded then I'll expl explain uh what this is all about for example was able to build this uh snake game which is very difficult to build actually and it is fully functional with a predictive block where the blocks are going to fall and uh this is close to 600 lines of code spanning over eight files and it has level uh increments meaning the game will get speedier everything in the game actually works so this is pretty spectacular I do want to say that there's a difference between open-ended agents and restrictive agents restrictive agent systems this is a restricted or semiopen agentic system that's why I restricted it to P game building P game but by simply changing the system messages you can actually implement this to your use case very easily let me show you the space shooter game that it is built it's uh almost 800 lines of code actually which is not possible with a single output from an llm since the lm's outputs are limited to uh 4,000 tokens which equates to about give or take 400 lines of cod we can actually play this game with sound on and off and H the sound off so it is a leveling system and it has actually upgrades that drop and when you pick them up your ship will either become speedier or your firing rate will increase these larger enemies are difficult to defeat you have to shoot them multiple times so there's quite a lot going on like I said this is 800 lines of code it just simply is not possible in a single go but it's very nice actually it works very well I'm a surprise and it's actually made up of eight Files toing about 800 lines of Code by the way the code files for the gentic game maker will be available at my patreon link will be in the description let's take a look at another game it had the checkers game right after that we'll start how it works and I'll explain it so here's a checkers game we can play as you can see it works well so this is multi file game that it was able to build let me quickly explain with a diagram what's going on and then we'll also run the code and go over the code as well so we have two planner agents which talk to one another and user specified end many times and they come up with a game plan which then we write to uh game plan. XML so each one of the agents that actually write these files so each one of these files are written by a separate agent which doesn't know what the other files are going to be so as you can see the difficulty in this for example the agent that ran main.py had no idea what the minute. pi was going to be except except for what was described in the game plan that ml so that's why this multi-agent interaction where a lot of the agents actually don't know the final outcome being able to produce this complex game is pretty mindboggling actually actually so after the planner two planner agents talk to one another they create a game plan and they outline how many files they're going to be in our project and then this my script automatically spawns up at many code writing agents they say in this case five and each of these is responsible for a file without knowing what the other agents are writing and once the entire project is written then we enter an error correcting phase where we execute the code and an error correcting agent actually tries to modify any one of the files or multiple files at a time to fix the error you repeat this a total of 10 times and you can actually manually continue this process as well but once the game is running properly then we enter a user feedback phas where you get to play the game and then give feedback to the user feedback agent and this agent will try to improve the game for you so we at any time you can have two of planner agents four or five coding agents and an error correcting agent and a user feedback agent all working in conjunction with one another to build multifile full projects for you so there is no limit to how many agents can spring into being but usually speaking on average there's about seven or eight and they all coordinate Without Really knowing the uh all the information that is uh available for continuing I do want to say that this uh agent this implementation is restricted to building games in pame but like I said you can change the system messages for agents to actually accommodate many use cases so let's go ahead and run this and see what is happening when we first run it we're going to get a welcome message to agent Game Dev and it warns you that this script will automatically execute code and we have to say yes to it that we understand it's going to be executing code and then it wants us to describe a a p game that we want to create uh let's just say a game of pong uh let's say AI versus user and how many planning iterations do we want Let's uh go ahead and say two for previty so now the agents are going to go through planning iterations now currently we're not seeing what they're talking about but we can actually turn on on CL false to true and we can actually see that so we can actually read what they're saying to one another let's turn this to true and actually run this again so again I've uh selected a game of pong AI versus user so now the first part of the planning has began we will first see the first agent uh give an outline and the second agent actually is responsible for uh creating more of the structure of the game so uh here is the first agent plan and then the second agent plan is going to show up in blue and at the final stage we change the system message so that they finalize their plans usually it's good to let them talk maybe for three or four iterations but if you let them talk for many iterations then we can actually kind of go go off course and talk about some stuff that maybe you don't want such as how how long the game is going to be take take the build anyway now after the file the game plan has been created you save to the game plan. XML file you can read more about it here and then we start creating g a game files each one of the game files are created in parallel by a separate agent which has no idea of what each one of these files are going to be and after that we run the game and we get an error message and we attempt we attempt we do a total of 10 attempts to uh correct errors and then we can also continue beyond that but I just thought what user can select it so correcting agents uh reasoning process and the corrections to different files and we get to see which files were updated in this case the game. piy and main.py and we still got another error and we also keep track of which which files were present in the error message but I realize that giving sending all files to correct error is actually the best way to go for reasons maybe I'll dive into later now it seems like yeah they were able to create this game but as you can see the pawn game the ball is not bouncing from the up and walls and now the game is running without error we get a we enter the Final Phase of this agentic system where we can provide feedback so I'm going to ask make sure the ball is bouncing off of the top and the bottom walls and now feedback agent is going to take us taking our feedback and going to reason about it and return corrected files and we can actually iterate many times if you want it so here we go so this is working but the balancing is not now I'm asking the ball is not bouncing off the top and bottom walls please implement the math for it and like I said we can repeat this as many times as we like we do want to say that our app currently has six files with a total of 260 lines of code so far and it's very difficult to file system working as in this case for examp to manage multiple files and their interactions actually much more difficult than writing a single file project okay now it had updated after another try but now it actually is running into another error so during the feedback phase if the when the gamees trying to execute if you're run into errors it will automatically try to fix this errors too so the error correcting agent will trigger but now we have a game that actually is corrected itself and now we can play it against the AI so this took a few iterations but uh imagining that we didn't give the planning agents too many iterations to plan this is actually pretty good and it is with six differents so this is how it works in a nutshell now let's go on to uh review the code files so we can understand how we can build something like this so bear in mind that this is quite a complex 500 lines of code but you will have learned a lot about agentic systems by the end of it but just so you know the code files will be available at my patreon and I am starting regular ask me anything sessions at my patreon for archit plus patrons link for that will be in the description as well the requirements for this is anthropic and py game now the sub process is uses whatever envir libraries that are installed in your environment so if you change this to reflect your own use case make sure the necessary libraries are installed otherwise you'll get an error and you'll have to install them anyway but just keep that in mind I would like to take a moment to talk about the benefits of becoming a patron as some of you may know in the last year and a half I've spent 3,000 hours over 300 uh projects as a patron you will have access to all the code files so you can get inspiration and iterate quickly another benefit is that you'll have access to all my course and my most recent and most proud one the THX Master Class teaching how I what I've learned on how to code fast and efficiently also the streamlit course and the fast API course in my patreon I also have tiers in which you can connect with me oneon-one check those out as well actually I forgot to mention that and another requirement is the term color for colorful printing in the terminal let's actually begin uh at the end of this script within the main Lo which is the create G name function all the functions in this process is async because we are using some parallel processing so we taken a user input to describe the game that they're looking for iterations is how many planning iterations there's going to be and then we call the plan project function to get a final plan after that we save it to XML file and then we create Asing tasks for agent right file method functions to actually write each one of the files to file and then after that now that we have our project ready we enter a while loop to actually continually execute the code and get feedback if there is no errors we start taking user feedback immediately and update game files with the feedback agent otherwise we try to identify the errors and actually call the error evicting agent and then this Loop continually keeps running until uh the user decides to break it so now that we've seen how the main Loop runs let's come back to the beginning of the file and start talk going over it one by one so this is all our Imports the interesting import here is sub process to execute the code and this XML X3 element Tre this actually is going to parse the file structure from the XML string uh that's how we are actually uh creating this uh game plan XML since we are instructing plot to return in between XML tags so this print we initialize our async entropic we are importing async entropic from anthropic so that we can call it in parallel for file generation we pass in our API key here we are detecting it from an environment variable but feel free to replace this feure key we are printing a warning that the script will automatically execute code on your machine which can be potentially dangerous and then we ask user to confirm this by typing yes and then our main files are always going to be written on under this app folder but at the start of every iteration we actually remove that folder so each time you run this file your app folder will be removed so if you have produced something you like make sure to copy this now if you uh have this app folder open in a command prompt somewhere or something like that or have a file open uh within it then it's asking you to close in a terminal which which have the F folder open but if you also have the files open close them so that this can proper and then rerun the script to properly remove the app folder then we have a function which is necessary for entropic this is to add a separator message between consecutive user messages Now anthropics API doesn't accept consecutive user or assistant messages in a row we in this implementation we never get consecutive assistant responses but we do get consecutive user responses in into the message list so this convenient function actually just checks for that and inserts an assistant message with just three dots so this is just something we have to deal with other than that here we are defin defining our plan project uh function which is the function for planner agents to discuss and plan the project the first system is it says your logical critical game design expert and the second one is a logical critical python architecture expert your role is to discuss and plan a critical and with a critical and rigorous eye a pame Project based on user input you can see how you can change this to accommodate different uh projects that you want this agentic system to execute for you so we're not going to uh read all of it but there were some important uh parts for example I did say that focus on game mechanic structure and overall design here in this case code organization modularity and best best practice I did mention make sure no files need oh I'm saying make sure no files I should have said maybe code files need external files all assets must be generated Within py game uh this is because when you're creating P game maybe it may want to use some PNG or something like that or some sound files we don't want that we just want everything to be all inclusive uh so that uh we can actually automatically execute this the critical object is to keep the project structure simple while making no circular Imports and broken Imports to occur and I also mentioned that no need to discuss timelines or GI commands because they can just get carried away with planning how long Project's going to take and start planning what we what they should do in the week one and stuff like that but this is this was very important that uh Imports for each file to be properly configured since planning the game plan that XML needs to include what each file needs to import and make sure that there are no circular Imports now we we we are trying to fix that in the error correction phase but we are also trying to eliminate any errors from occurring and we just pass into to the system messages the user input as well and then we add a message to the first agent first planning agent please plan a py game Project based on the following user input remember that the game should start with a main module in the main.py file now this was another important aspect and the reason why restricting these agents is important because this game can have many different file names for example the main.py is usually the choice to start ANC multifile application but this could have also been like start up Pi but that's just the file name but even within the file name the main function that runs it can have multiple names for example it can be main Loop or start but we want it always to be main both for the file name and for the function name and also we want main to not take any arguments so we can actually run the code properly so that's why we are actually mentioning it here and we're going to mention it in other parts of this code as well so then we enter a loop for as many iterations that the user has uh defined we print a message and if we are in the final uh iteration range which uh we should talk about at the end but uh we'll come back to this okay so this is going to take place at the final uh iteration let's actually talk about it now so so there's some code that is going to run uh but when when we at the final stage of the iteration then we want to check Pan the system uh user message that says this is the final iteration please provide your final game structure along with file structure you think is best for the game don't return any directories but just the file names and descriptions because we want all files to be within the app directory not within any additional directories make sure to mention what imports are necessary for each file this is very critical critical objective is to keep the project structure simple while making no circular Imports or broken Imports to occur I guess there's a typo here uh or a broken sentence actually but it works ensure function and other meth method inputs are accurate as well as their return so we also want to make sure in the game plan like which methods we going to have and what what will be their inputs and what will they what will they be returning it's becomes crucial when each agent is working independently on their own file so they give a general idea of what functions or methods and what inputs they're taking and what they're returning remember that the game should start with a main module in the mainy file we are again repeating that concern that the the game starting file is main apply and the game starting function is main function which doesn't take in any arguments so we iterate this here now at this point uh we do want to make sure that message one since we added a user message here and we are adding another user message here that we have to add a separator between those two then we just simply call and uh if we if we set the print respon or to we print it and then just append the messages to the appropriate agents uh message list and if it is final the last message that it gets the second agent gets which is going to be the one responsible from returning the actual XML game plan this is the final iteration please review the game design carefully and provide your final response in the following XML format so if you past this here but you can see it more clearly here it starts with a game plan and then continues with overview mechanics along with the files uh with a file for each file as you can see this just is a repeating process along with the definition of the file along with the necessary methods or functions that are in it and we do say we do instruct it to not return any additional directories but just the file names and descriptions along with simple descriptions of functions and methods along with their inputs and returns anyway so uh you can read this but this is a very important message this is the final message and I'm not saying this is the best message to if you do download the code feel free to improve on it and at this point we do need to add a separator in this juncture too and then we get a response so we keep getting these responses anyway all throughout the iteration cycle but uh only if it is final we add these final messages to actually for them to gather their thoughts and provide the final plan I would like to take a moment to talk about the benefits of becoming a p as some of you may know in the last year and a half I've spent 3,000 hours over 300 uh projects as a patron you will have access to all the code files so you can get inspiration and iterate quickly another benefit is that you'll have access to all my courses and my most recent and most proud one the THX Master Class teaching how I what I've learned on how to code fast and efficiently also the streamlit course and the fast API course in my patreon I also have tiers in which you can connect with me oneon-one check those out as well and at this point we just use rigex to extract the plan and return XML contents now the next one is the defined agent right file I guess this is not the best name to give it but this is actually going to take in the file name file description and game plan and we're going to call this multiple times in parallel so this is a very important agent this going each one of these agents are going to be responsible for each one of these files within our project so the system messages that you're a python game developer uh to write error free code based on the overall project structure do not include any external media files val. commented code the game should start with a main module in the main.py file main shouldn't take any arguments again we are reiterating here and it's going to return the code in this XML code blocks this is the system message and here is the prompt create a python file name and we pass in the file name name for example the aiy b.y config.py with the following description we pass the file description but we also pass him the overall game plan remember the game should start with a main module so if we are repeating crucial stuff at every at every juncture we also ask to return the full contents of the file and then we make the call we get it we we split the we take The Code by splitting the string and now we just write it to the appropriate file name now this PAR file structure is important only when we receive the XML string to actually uh parse it and actually write it to the game plan that XML now next next is the Run game which is going to be responsible for running the game that has been completed to check for errors we're going to print information some information and we're going to initialize two strings full full output and full error we're going to run it with sub process and we're going to keep track of the both the CD out and a CD error and we're going to append it because when you're running the P game you can get runtime errors plus terminal errors so we're trying to keep track of both so essentially we while through we try to keep track of output error and a regular error for example the game may not you may get an error during the launch of the game or you may get an error trying to perform an action because maybe the code is broken so we're trying to capture all of it and append it and at the end of the day turn it right here if we keep adding the SD out and SD eror to full output and full error and including the error summary and return the error summary and otherwise the sometimes we're not going to rent any errors then we say game completed successfully then we return none now create game again is Rel looked is going to be the main Loop we specify mixed attempt I'm sorry actually this is not the main Loop but this is the loop where we create and Run the game and collect error messages so we we going to try to do that up to 10 but we are also going to allow user to continue if they want to after that we're going to enter a infinite Loop and we're going to run run the game and we're going to get an error summary and if the error summary is not done we're going to say G game Run successfully and we're going to pick in user input if and along with that we're going to pass on to the update game files which we're going to actually look look into it this is the one that is responsible for making a call to the feedback agent and and we count we print the count lines of code we print how many lines of code that are actually present in our ire project now otherwise we print the errors is detected and then for we try to attempt as many times is allowed fix the game files and print the count lines of code after each iteration and when all the errors are fixed are fixed successfully otherwise fail to fix all errors after mix tries then we ask the user if they actually want to continue so this actually can continue beyond the mix attempts but with only with user consent now here is the fix game files function to fix errors in the game files this is going to print attempt in the fixed error and it's going to actually find all the files in the error messages so my original thought was that uh I only wanted to sign for error correction the files that were present in the error message but that just wasn't correct solution because sometimes a main.py file for example would throw an error about config.py not having like let's say screen height config.py wouldn't be present in the error because config Pi is not really associated with that error so at the end of the day I do I do actually extract all the error file names but in the end actually I create a dictionary and go over all the files that are in app and actually essentially have all the file names and their contents in this dictionary and send each time for error correction when we are making the API call but if you do have error file names we still print them but when we are sending we do all the files for error correction and we just print them for information purposes and here is the system message for the error correcting agent your task is the fix error in py game project analyze the error so this is pretty standard but we are also again reminding that the game should start with a main module in the mainy file so this type stuff you have to do I I found that but I did say that it needs to carefully reason about the error in a step by step matter and I with XML tags within which it can reason and then after that it can actually return the corrected file contents now you can do that for multiple files although although I should have mentioned that it should but it does that anyway and then we also have another so this is the system message but we also have a user message an error occurred while running the py game project here's the error message we pass the error message and then we pass the entire contents of our project please analyze the error and provide a corrected version of the files resolve the error return the full contents of the file so this was necessary to repeat at several places so that it doesn't redact the final solution but to just return it and so we can write it as is remember the game should start with the main module you can see I repeat this at every possibility then we just make the call and we get the corrected files and we just Loop over them and rewrite over them essentially and then we ensure that they were written and then we clear the Python's module cache I don't think we need to do this anymore but I'll just leave it there since this code became quite complex I don't want to remove anything but originally I was using a different way of executing the code uh which wasn't working very well then I switched to sub process so this is this was necessary to clear the module cach for that uh so it may not be necess NE maybe I'll not may not be necessary and then we we sleep a little just to give system some time to actually make sure the files are written otherwise we PR print no corrected file content found in the response just in case there was such a response but I never ran into it then here's the function to update game files this is going to gather all existing game files uh go over them and this is uh actually now I'm realizing these function names are not very well written I'm sorry about that uh but uh this one actually is the feedback agent you can change them if you choose to download it this is going to take into the user feedback and it is again is going to gather all the existing game files and repair the prompt for the API and here's the prompt here are the current we pass the entire contents and then we provide the user feedback and then we say please analyze the feedback and suggest updates to the game files to address the user comments I feel like this any one of these system messages can be improved but anyway when we do we do say that please provide the fully updated content not just redacted versions of them turn the updated file in this in this format so we can know which file name plus the contents always return to full contents game should start with a main module in main. file with no arguments again you're also mentioning it here now this is the regular prompt and the system message for that for this is you're an expert python py game developer your task is to update the py game Project based on user feedback you can see that by changing this py game developer and the mentioning of games here you can make this accommodate any type of projects anyway so this is the system message for the feedback agent and the call and then we get the uh extract the updated file contents from the response and then we write them again this this may not be necessary to clear the cache but we again sleep and otherwise say no updates were necessary here's the function to count the lines of code going looping over all the files just to see how many lines of code we're writing but like I said out of all the ones that I worked on like space shooter is nearing about 800 lines of code and Tetris 600 so this kind of project are not possible with a one shot uh generation with just 4,000 token output limits on large language models at least currently now I do want to mention that these are this multifile projects are way harder for large language models the single single you know script like a single go at a single file project is actually it seems large language model will have a much easier time with that especially considering each one of these files is written by a separate reg not knowing how the entire project will actually turn up turn out so keep that in mind and here in the create game this is the main function we take in user input take in number of iterations we get the final plan write it to game plan. XML we get the file structure out of that and then we create a app directory if it doesn't exist and then we create an asyn Kyo list who to call the agent write file these are the agents that are going to be responsible for writing each one of our files and we ASN get gather those tasks count the lines of code and then we enter the the iterative code execution and it's R of Correction Loop so in this Loop we can to check to see if there are any error messages there are no error messages then we're going to move on to the feedback correction so this single Loop actually takes care of feedback a correction loop with the feedback agent plus the error correction loop with the error correction agent so uh if there are no errors this part will run and we'll continually uh iterate on the code but even after this if there is any this allows us to even after a feedback if there's an error in the code then we can actually execute that code and try to auto correct it now if there is an error we print the error and we try to attempt in makes attempt try to fix it call the fix game files count the lines of code uh Run Run the game uh try running the game again after fixing to see what happens so this can repeat iteratively otherwise if if all iterations have gone by and we we weren't able to fix it and we print failed to fix all errors after this many attempts but then user gets the choice to run it one more time but after that point the user will have to explicitly agree agre to run the auto correction again and then we just as co. run run the entire game so I believe this was this was kind of eye opening for me I do want to explore gentic workflows more deeply going into the future I will be exploring more about fonts and calling as well think you will find this very useful I hope you found the video description of it useful but uh if you want to just grab the code files quickly you can find it at my uh patreon this will be available for architect level patrons I will be doing more videos on function calling as well and build agentic systems like that also remember that I will be starting regular withme anything sessions for my patrons architect level plus patrons you can find the link in the description also if you want to get in touch me for oneon-one you can you can check out the higher level tiers on my patreon on which you can connect with me uh one one on a monthly basis thank you for watching and see you in the next video I would like to take a moment to talk about the benefits of becoming a patron and some of you may know in the last year and a half I've spent 3,000 hours over 300 uh projects as a patron you will have access to all the code files so you can get inspiration and iterate quickly another benefit is that you'll have access to all my courses and my most recent and most proud one the THX Master Class teaching how I what I've learned on how to code fast and efficiently also the streamlit course and the fast API course in my patreon I also have tiers in which you can connect with me oneon-one check those out as well