Context Engineering Simply Explained

codebasics · Beginner ·📊 Data Analytics & Business Intelligence ·11mo ago

Key Takeaways

Explains context engineering as a natural evolution of prompt engineering, using LangChain and Cogntion as examples

Full Transcript

Since the time Andre Karpathi posted about context engineering on his Twitter, this term has gained a lot of momentum over the internet. It went to a level where folks at Cognition called it number one skills for engineers building AI agents. The goal of this video is to explain context engineer in a most intuitive language such that even a high school student can understand it easily. Context engineering is a natural evolution of prompt engineering. So let's summarize what is prompt engineering. Let's say you are summarizing an article and you write this prompt. Summarize the following article. This is very generic prompt and it will produce a very generic output. If you are a good prompt engineer, you will give a specific prompt saying that summarize article in following three bullet points. You will mention your bullet points. You will also say use simple clear language suitable for high school students. So you will specify the audience which is going to consume this. So this is prompt engineering. Now when you talk about agents, you give an action to an agent such as book a flight between two places and an agent will have access to tools such as Xedia API and using these tools it will first search for flights. So let's say five different flights. Then it will decide which is the cheapest one. Let's say this is the cheapest one and it will book the flight. So an agent is nothing but a program which is using LLM along with tools, knowledge, memory etc to perform autonomous action. Previously when we were looking at the Q&A chatboard is just simple question and answer whereas agent will perform an action autonomously. Now internally it will take multiple turns. What I mean by that is when you say okay book applied between place A and B it will actually first it will make LLM call right so this is let's say first turn okay so this is Turn first turn where it will determine the plan that it needs to do all these three actions and it will perform the first action which is searching the flight in the second turn or a third turn it will look at the Xedia API It will find the flights, right? So it see it found all these flights. So this is turn number two. Then it will decide the cheapest flight. So that will be turn number three. Then turn number four, five. It might perform n number of turns which is n number of llm calls along with this specific prompts to accomplish a goal. Now here these prompts are generated by LLM. It's not like you are manually generating all these prompts, right? You don't have time for it when you're building LLM application. Let's say an agent, it will do this thing autonomously. Now, you run into this risk where these turns might get costly. You know, it might increase your LLM API cost bill. Also, if there is a problem in one of these prompts, it will misbehave, right? It will take an action that you don't want it to take. So there is certain risk in terms of generating these intermediate prompts or intermediate contexts right this is the context that you're giving to your LLM and in these terms whatever context is being generated it has to be really good and accurate now here you can see that every time I am appending to the previous context so if you give more information if you give the information of the previous context uh from the previous done it is going to be useful in general but the problem with in appending to this context is that you might run out of your context window right so this is the diagram I took from cognition blog where let's say the agent will break down the task for our flight scenario then this is a subtask one right and let's say it is appending that context then it is appending a new context now you might run into this context overflow. Not only the length of the context is a problem. Let's say even if you don't run into context overflow, longer context will result into some other problems too. I'm going to link this article in a video description. Please read through it. It says how long context will fail. You'll run into four different problems. The first one is context poisoning, then context distraction, context confusion, and context clash. I don't want to go through these in detail because then this video will become too longer. But read through this article. Just remember that not having a proper content in the context will result into numerous problems for your agent. Therefore, what you need is context engineering. As per Andre Karpathi who prefers the term context engineering over prompt engineering uh the Twitter says people associate prompts with short task description you would give an LLM in your dayto-day use that is your prompt engineering but when you're building industrial strength LLM app it can be workflow app or it can be an agent context engineering is the delicate art and science of filling the context window with just the right information for the next step. So you have a context window and you want to fill that context with the exactly the right information. So it becomes art as well as science as per cognition prompt engineering was coined as a term for the afford needing to write your task in the ideal format for LLM chatboard. Right? For the chatboard you are writing a good prompt which is good enough for that narrow task that you're performing. Whereas context engineering is the next level of this. It's an evolution of prompt engineering. It is about doing this automatically in a dynamic system. Dynamic system is your agent. You remember those turn one, turn two, turn three where agent is producing that that context. You are not writing it in that dynamic system. You want to do this in a very effective way. It takes more nuances and is effectively the number one job. Folks, if you are an AI engineer, if you are an AI agent engineer building agents, this is a number one skill that you need to have for anybody who is building AI agents. So essentially, if you consider LLM as a brain, okay, so let's say LLM as a brain or let's say CPU, then this context window is a RAM for your computer and if you have good quality data in your RAM, your output will be better. and context window you know context engineering is all about getting the right content inside your context and how do you get that content well through three different ways one is through tools so let's say when you are looking at Xedia APIs you want to get the right information on the flights by calling Xedia API and that information you got through a tool call second one is a knowledge if you're building a rag based system and let's say you are doing something with your database you pull the information from your Oracle or MySQL database that is your knowledge. It can be PDF file too. And the third one is instructions. So this is your prompt engineering. You write the proper instructions. There are four common strategies for context engineering as per lang chain. And we are going to go through each of them quickly. The first one is context compression. So when you run into this context overflow situation where your context is very very big one obvious technique you can deploy is you can compress the context. So whatever conversation and actions you have so far you see this green box now you that box has become little thinner. So you will remove unnecessary information from this context or you will extract only useful information. See key moments and decisions from that context and you will make that context smaller it each turn. So this is the first turn. In the second turn this particular context you make it you compress it. this one you compress it and that way you your context is not only good quality but you are also respecting that context window limit which every LLM will have different token limit. Then the second one is writing the context. So for writing the context you can use uh something called a scratch pad. So scratch pad is uh something you can implement with a tool call. So let's say you have a simple tool call which is writing to a file on a disk and this scratch pad is different than your LLM context. So let's say your LLM context window has a limit of 30,000 tokens. So that window is there. On top of that it's like as a human you know you have a notepad and when you're having a conversation you will make points in that notepad. So that notepad is a scratch pad but in your brain you will remember that information. So that remembering information in brain is your alarm context window. So this is an additional basically memory that you get and it can be implemented easily with a tool call which just writes to a file on a disk. This is useful for an agent session. The second thing that is useful for an agent session is a state. So if you have worked with langraph, we have a langraph tutorial on our YouTube channel. In lang graph there is a state that you will pass across all the nodes that is also within the agent session. So you can use that state to write information meaningful information for the context. And if you use chat GPT whatever your past conversations are chat GPT remembers that you might have noticed that if you turn off your computer today tomorrow if you turn on your computer which is a different session in chat GPT and if you say okay uh define my personality based on past conversation it will define it. So that is using long-term memory and it is valid across agent sessions. Okay, so these are the three techniques uh that you use for write context and it means saving it outside the context window to help an agent or a workflow application perform a task and once you have saved this information the second step is obviously retrieving that right retrieving that context it's called selecting the context and in that of course you are retrieving from long-term memory that GBD example that I gave then you are retrieving relevant knowledge. This one is a rag. So when you're performing rag you will have vector database from which you will retrieve necessary context that you can pass to LLM. And this rag is nothing this retrieving information through rag method is nothing but selecting the context. Okay you can do the same thing from scratch pad you are retrieving from relevant tools you are retrieving. So that is the second strategy which is selecting the right context in an efficient manner. And then comes I think the third strategy is compressing the context. So the fourth one is isolating the context. Let's say you are working on a big task and you you are building multi- aent system where you can divide the task across two agents. So that will reduce your individual context window for the agents. Right? So this is the context window for context for agent one. This is for agent two. And since you have divided them individual context window will not only be shorter but it will also be more relevant more it will be high quality for that agent one. Okay. Now building multi- aent system also comes with certain risk. So you have to be very mindful. You have to uh make sure that you can really parallelize this work. In some cases, you can't parallelize this work, right? You have to do things in a sequence because agent 2 relies on the output from agent one. So based on the situation, you can use this isolating context approach. So those are the four strategy. I'm going to link another blog from lang chain which will describe these four strategies in detail. So let's go through Andre's uh tweet in detail. So he prefers contextual engineering over prompt engineering especially for the people who are building AI agents. And what he says that it is an art delicate art and science. Okay. What you fill in your context basically. So context engineering is nothing but delicate art and science of how you fill the context window with just the right information and highquality information so that it is useful for the next step and it is science because doing this right involves task descriptions and explanations few short examples right you are filling right kind of few short examples because let's say you have 2,000 token context window it's like you have limited currency and you have to use those token very accurately. So let's say if you are providing too few short example you have to provide very good high quality diverse examples then rag okay related data then tools what tool calls you're making what kind of output tool call is returning sometimes these tools will return big output and 80% of this output might not be even relevant to the LLM so you have to tweak your tool calls and provide exactly the right outut put then state history compacting too little or the wrong information and the LLM doesn't have the right context for the optimal performance. If you have too much or too irrelevant then context then LLM cost might go up and performance might come down. Okay, doing this well is highly non-trivial. So folks there is no fixed set of guidelines. Therefore it is called art and science. Okay. And art because of the guiding intuition around LLM psychology of paper spirit. I hope this video helped you understand context engineering. If you have any question, please post in the comment box below. Once again, check video description for all relevant articles and information. Thank you very much for watching. If you like this video, please give it a thumbs up and share it with your friends. [Music]

Original Description

Context engineering is a natural evolution of prompt engineering. In this video, we will understand context engineering in a simple language. We will also understand the difference between prompt engineering and context engineering. Karpathy's tweet: https://x.com/karpathy/status/1937902205765607626 Langchain article: https://blog.langchain.com/context-engineering-for-agents/ Cogntion article: https://cognition.ai/blog/dont-build-multi-agents How Long Contexts Fail: https://www.dbreunig.com/2025/06/22/how-contexts-fail-and-how-to-fix-them.html Do you want to learn technology from me? Check https://codebasics.io/?utm_source=description&utm_medium=yt&utm_campaign=description&utm_id=description for my affordable video courses. Need help building software or data analytics/AI solutions? My company https://www.atliq.com/ can help. Click on the Contact button on that website. 🎥 Codebasics Hindi channel: https://www.youtube.com/channel/UCTmFBhuhMibVoSfYom1uXEg #️⃣ Social Media #️⃣ 🧑‍🤝‍🧑 Discord for Community Support: https://discord.gg/r42Kbuk 📸 Codebasics' Instagram: https://www.instagram.com/codebasicshub/ 📝 Codebasics' Linkedin : https://www.linkedin.com/company/codebasics/ ------ 📝 Dhaval's Linkedin : https://www.linkedin.com/in/dhavalsays/ 📝 Hem's Linkedin: https://www.linkedin.com/in/hemvad/ 📽️ Hem's Instagram for daily tips: https://www.instagram.com/hemvadivel/ 📸 Dhaval's Personal Instagram: https://www.instagram.com/dhavalsays/ 🔗 Patreon: https://www.patreon.com/codebasics?fan_landing=true
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from codebasics · codebasics · 0 of 60

← Previous Next →
1 Python Tutorial - 1. Install python on windows
Python Tutorial - 1. Install python on windows
codebasics
2 Python Tutorial - 2. Variables
Python Tutorial - 2. Variables
codebasics
3 Python Tutorial - 3. Numbers
Python Tutorial - 3. Numbers
codebasics
4 Python Tutorial - 4. Strings
Python Tutorial - 4. Strings
codebasics
5 Python Tutorial - 5. Lists
Python Tutorial - 5. Lists
codebasics
6 Python Tutorial - 6. Install PyCharm on Windows
Python Tutorial - 6. Install PyCharm on Windows
codebasics
7 PyCharm Tutorial - 7. Debug python code using PyCharm
PyCharm Tutorial - 7. Debug python code using PyCharm
codebasics
8 Python Tutorial -  8. If Statement
Python Tutorial - 8. If Statement
codebasics
9 Python Tutorial - 9. For loop
Python Tutorial - 9. For loop
codebasics
10 Python Tutorial -  10. Functions
Python Tutorial - 10. Functions
codebasics
11 Python Tutorial - 11. Dictionaries and Tuples
Python Tutorial - 11. Dictionaries and Tuples
codebasics
12 Python Tutorial - 12. Modules
Python Tutorial - 12. Modules
codebasics
13 Python Tutorial - 13. Reading/Writing Files
Python Tutorial - 13. Reading/Writing Files
codebasics
14 How to install Julia on Windows
How to install Julia on Windows
codebasics
15 Python Tutorial - 14. Working With JSON
Python Tutorial - 14. Working With JSON
codebasics
16 Julia Tutorial - 1. Variables
Julia Tutorial - 1. Variables
codebasics
17 Julia Tutorial - 2. Numbers
Julia Tutorial - 2. Numbers
codebasics
18 Python Tutorial - 15. if __name__ == "__main__"
Python Tutorial - 15. if __name__ == "__main__"
codebasics
19 Julia Tutorial - Why Should I Learn Julia Programming Language
Julia Tutorial - Why Should I Learn Julia Programming Language
codebasics
20 Python Tutorial  - 16. Exception Handling
Python Tutorial - 16. Exception Handling
codebasics
21 Julia Tutorial - 3. Complex and Rational Numbers
Julia Tutorial - 3. Complex and Rational Numbers
codebasics
22 Julia Tutorial - 4. Strings
Julia Tutorial - 4. Strings
codebasics
23 Python Tutorial -  17. Class and Objects
Python Tutorial - 17. Class and Objects
codebasics
24 Julia Tutorial - 5. Functions
Julia Tutorial - 5. Functions
codebasics
25 Julia Tutorial - 6. If Statement and Ternary Operator
Julia Tutorial - 6. If Statement and Ternary Operator
codebasics
26 Julia Tutorial - 7. For While Loop
Julia Tutorial - 7. For While Loop
codebasics
27 Python Tutorial  - 18. Inheritance
Python Tutorial - 18. Inheritance
codebasics
28 Julia Tutorial - 8. begin and (;) Compound Expressions
Julia Tutorial - 8. begin and (;) Compound Expressions
codebasics
29 Python Tutorial - 12.1 - Install Python Module (using pip)
Python Tutorial - 12.1 - Install Python Module (using pip)
codebasics
30 Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
codebasics
31 Julia Tutorial - 10. Exception Handling
Julia Tutorial - 10. Exception Handling
codebasics
32 Python Tutorial  - 19. Multiple Inheritance
Python Tutorial - 19. Multiple Inheritance
codebasics
33 Python Tutorial - 20. Raise Exception And Finally
Python Tutorial - 20. Raise Exception And Finally
codebasics
34 Python Tutorial - 21. Iterators
Python Tutorial - 21. Iterators
codebasics
35 Python Tutorial - 22. Generators
Python Tutorial - 22. Generators
codebasics
36 Python Tutorial - 23. List Set Dict Comprehensions
Python Tutorial - 23. List Set Dict Comprehensions
codebasics
37 Python Tutorial - 24. Sets and Frozen Sets
Python Tutorial - 24. Sets and Frozen Sets
codebasics
38 Python Tutorial - 25. Command line argument processing using argparse
Python Tutorial - 25. Command line argument processing using argparse
codebasics
39 Debugging Tips - What is bug and debugging?
Debugging Tips - What is bug and debugging?
codebasics
40 Debugging Tips - Conditional Breakpoint
Debugging Tips - Conditional Breakpoint
codebasics
41 Debugging Tips - Watches and Call Stack
Debugging Tips - Watches and Call Stack
codebasics
42 Python Tutorial - 26. Multithreading - Introduction
Python Tutorial - 26. Multithreading - Introduction
codebasics
43 Git Tutorial 3:  How To Install Git
Git Tutorial 3: How To Install Git
codebasics
44 Git Tutorial 1: What is git / What is version control system?
Git Tutorial 1: What is git / What is version control system?
codebasics
45 Git Tutorial 2 : What is Github? | github tutorial
Git Tutorial 2 : What is Github? | github tutorial
codebasics
46 Git Tutorial 4: Basic Commands: add, commit, push
Git Tutorial 4: Basic Commands: add, commit, push
codebasics
47 Git Tutorial 5: Undoing/Reverting/Resetting code changes
Git Tutorial 5: Undoing/Reverting/Resetting code changes
codebasics
48 Git Tutorial 6: Branches (Create, Merge, Delete a branch)
Git Tutorial 6: Branches (Create, Merge, Delete a branch)
codebasics
49 Git Github Tutorial 10: What is Pull Request?
Git Github Tutorial 10: What is Pull Request?
codebasics
50 Git Tutorial 7: What is HEAD?
Git Tutorial 7: What is HEAD?
codebasics
51 Git Tutorial 9: Diff and Merge using meld
Git Tutorial 9: Diff and Merge using meld
codebasics
52 Difference between Multiprocessing and Multithreading
Difference between Multiprocessing and Multithreading
codebasics
53 Python Tutorial - 27. Multiprocessing Introduction
Python Tutorial - 27. Multiprocessing Introduction
codebasics
54 Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
codebasics
55 Git Tutorial 8 - .gitignore file
Git Tutorial 8 - .gitignore file
codebasics
56 Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
codebasics
57 Python Tutorial - 30. Multiprocessing Lock
Python Tutorial - 30. Multiprocessing Lock
codebasics
58 Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
codebasics
59 What is code?
What is code?
codebasics
60 Python unit testing - pytest introduction
Python unit testing - pytest introduction
codebasics

Related Reads

📰
AI-Powered Data Cleaning and Transformation for Excel & CSV: A Technical Guide
Learn how to leverage AI for data cleaning and transformation in Excel and CSV files to streamline business operations
Dev.to AI
📰
🇮🇳 ForThePeople.in: One Engineer’s Quest to Put Every Indian District’s Data in Your Pocket
Learn how one engineer's project, ForThePeople.in, aims to increase civic transparency in India by aggregating government data, and how you can apply similar principles to your own projects
Dev.to · yash561321
📰
Master Data Analytics with Gen AI at IDSA Janakpuri Step into the future of analytics with the…
Learn data analytics with Gen AI at IDSA Janakpuri for hands-on training in tools and visualization
Medium · Data Science
📰
7 DuckDB Features That Made Me Stop Using Traditional SQL Databases
Discover 7 DuckDB features that make it a superior choice to traditional SQL databases for analytics and ETL, and learn how to apply them
Medium · Python
Up next
Moneyball Economics - 60 Second Enrichment Economics
tutor2u
Watch →