{"slug": "understanding-how-ai-agents-work", "title": "Understanding How AI Agents Work?", "summary": "A developer explains the concept of AI agents, comparing them to a personal guide that provides direct answers and relevant references. The post details the workflow of AI agents, including chunking large documents, embedding text chunks, and storing them in a vector database for efficient retrieval.", "body_md": "What is an AI Agent?\n\nThe AI agent is a tool that helps the user to get the answer/info directly what he is seeking.\n\nIf we just talk in the context of coding only before the AI come coding is like a open book exam where writing code is like answering the question in the test and if you stuck you just open the book or go to internet and get the answer but even its a open book exam you must atleast have a basic idea where to look and the available answer is suit for you code you not its like you look for an Math query and you checking the chemistry answer.\n\nWhat change after AI is that we all get our own guider who directly gives us the answer and relevant links to where he gets the answer for reference.\n\nSo we can say that AI Agent is a more of our personal guider which we can use in any way, anytime we want.\n\nWorkflow of AI Agents:-\n\nSo we see we can use the AI Agent at many places and more of a guider now, let’s see how the guider does things behind to help us.\n\nLets go with the example of the Open Book Test, and we user Our Guider AI Agent to clear the test.\n\nSo we Stuck in some questions and Now We need the help of our Guider to get the answer for the relevant question.\n\nSo the first question was whether our Guider needed to look at the Whole Book?\n\nThe answer is no as book may be very long and its just a example of book the data can be very long so machine/ AI Agent cant look whole data at once so there is limited size it can look at a time and that called Context Window and its depends on LLM some have big window and some has small\n\nSo the question may come now that the data is always more than the context window, so how can we store and get the answer?\n\nThe answer is we divided the book into small chunks, let’s say pages or more small (put 1000 words at a time together into the list), we do not need to always look through the whole data to get the answer\n\nAfter dividing the Big PDF into small chunks, we pass the relevant chunks into the context window, and LLM can do the process\n\nfrom langchain_community.document_loaders import PyPDFLoader\n\nfrom langchain_text_splitters import RecursiveCharacterTextSplitter\n\nfile_path = \"./example.pdf\"\n\nloader = PyPDFLoader(file_path=file_path)\n\ndocs = []\n\nprint(\"LOADER\", loader)\n\n// Loads the file\n\ndocs_lazy = loader.lazy_load()\n\nfor doc in docs_lazy:\n\ndocs.append(doc)\n\ntext_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)\n\ntext = text_splitter.split_documents(documents=docs)\n\nWhy require to do Embedding:-\n\nNow we do the chunking or divide the data into small parts, you think now just put it into the vector database and we're good to go. But that’s not true as it is like we just put the text into the database and if the query comes it’s not possible to get the relevant data from the database.\n\nTake an example like Translation from English to Your Language\n\n“The sun sets in the west”\n\nCan its make sense if you translate the things word by word and say it may be, but its rare, most of the time it’s not you have to find the relation and semantic meaning for the words and how they are related to translate it well.\n\nI don’t like, grammar, and i have a sensei who always corrects me.\n\nSo like in translation, we put the word based on its relation we put the chunking data in a way that the same meaning of words are closer to each other in the vector db so its easy and get the relevant data easily for the query.\n\nDo not worry, this is all done by the LLM.\n\nSo far, we take a big file —> Divide the file into small chunks —> Do the Embedding of the chunks and put in the Vector Db.\n\nfrom langchain_google_genai import GoogleGenerativeAIEmbeddings\n\nfrom langchain_qdrant import QdrantVectorStore\n\nembedder = GoogleGenerativeAIEmbeddings(\n\nmodel=\"models/text-embedding-004\",\n\ngoogle_api_key=\"Your Api key\",\n\n)\n\nvector_store = QdrantVectorStore.from_documents(\n\ndocuments=[],\n\n```\n url=\"http://localhost:6333\",\ncollection_name=\"learning_langchain\",\nembedding=embedder,\n```\n\n)\n\nvector_store.add_documents(documents=text)\n\nRetrieval & Generation -\n\nWhen the user asks a query/question, the agent:-\n\nWhen user asks a que/query, we can directly search the query in our database as user can ask anything, and its very hard and maybe the data we got is not fully correct to find what the user is asking about, so for this.\n\nWe convert the user query into an embedding.\n\nThen we get the embedding of the query and search into our Vector Database for the relevant meaning word or similar semantic meaning.\n\nAfter getting the relevant semantic meaning, we pass the ( relevant data + user query ) to our LLM and we got the result.\n\nfrom langchain_qdrant import QdrantVectorStore\n\nretriver = QdrantVectorStore.from_existing_collection(\n\nurl=\"[http://localhost:6333](http://localhost:6333)\",\n\ncollection_name=\"learning_langchain\",\n\nembedding=embedder,\n\n)\n\nquery = input(\"Ask the Doubt:=> \")\n\nmessage.append({\"role\": \"user\", \"content\": query})\n\nsearch_result = retriver.similarity_search(query)\n\nmessage.append({\"role\": \"system\", \"content\": search_result[0].page_content})\n\nres = client.chat.completions.create(\n\nmodel=\"gemini-2.0-flash\",\n\nmessages=message,\n\nresponse_format={\"type\": \"json_object\"},\n\n)\n\nprint(res.choices[0].message.content)\n\nConnect With me:-\n\nGithub:- [https://github.com/hksoldev](https://github.com/hksoldev)\n\nLinkedIn :- [https://www.linkedin.com/in/mrhemantkumarr/](https://www.linkedin.com/in/mrhemantkumarr/) Youtube\n\nX(Twitter) :- [https://x.com/hksoldev](https://x.com/hksoldev)", "url": "https://wpnews.pro/news/understanding-how-ai-agents-work", "canonical_source": "https://dev.to/hksoldev/understanding-how-ai-agents-work-18lj", "published_at": "2026-07-12 00:58:20+00:00", "updated_at": "2026-07-12 01:13:58.640949+00:00", "lang": "en", "topics": ["artificial-intelligence", "ai-agents", "large-language-models", "natural-language-processing", "developer-tools"], "entities": ["LangChain", "GoogleGenerativeAIEmbeddings", "QdrantVectorStore", "PyPDFLoader", "RecursiveCharacterTextSplitter"], "alternates": {"html": "https://wpnews.pro/news/understanding-how-ai-agents-work", "markdown": "https://wpnews.pro/news/understanding-how-ai-agents-work.md", "text": "https://wpnews.pro/news/understanding-how-ai-agents-work.txt", "jsonld": "https://wpnews.pro/news/understanding-how-ai-agents-work.jsonld"}}