{"id":248,"date":"2026-10-27T13:28:30","date_gmt":"2026-10-27T13:28:30","guid":{"rendered":"https:\/\/devdojo.co.in\/?p=248"},"modified":"2026-10-27T13:28:31","modified_gmt":"2026-10-27T13:28:31","slug":"agentic-ai-series-part-2-anatomy-of-an-ai-agent","status":"publish","type":"post","link":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/27\/agentic-ai-series-part-2-anatomy-of-an-ai-agent\/","title":{"rendered":"Agentic AI Part 2: Anatomy of an AI Agent: Models, Tools, Instructions and Memory"},"content":{"rendered":"\n<div class=\"dd-post\">\n<style>\n.single article:has(.dd-hero) .entry-media{display:none}\n.dd-post{--navy:#0b1020;--blue:#3178c6;--violet:#7c5cff;--ink:#1f2433;--muted:#5b6275;--line:#e3e6ef;font-size:17px;line-height:1.75;color:var(--ink)}\n.dd-post h2{margin:2.2em 0 .6em;font-size:1.6em;line-height:1.3;scroll-margin-top:90px}\n.dd-post h3{margin:1.6em 0 .5em;font-size:1.22em;scroll-margin-top:90px}\n.dd-post a{color:var(--blue);text-decoration:underline;text-underline-offset:3px}\n.dd-post pre{background:#0f1430;color:#e6e9f5;padding:18px 20px;border-radius:12px;overflow-x:auto;line-height:1.6;border:1px solid #232a52;margin:1.2em 0;font-size:14.5px !important}\n.dd-post pre code{font-size:1em !important;background:none;color:inherit;padding:0;font-family:\"JetBrains Mono\",\"Fira Code\",Consolas,Menlo,monospace;white-space:pre}\n.dd-post :not(pre)>code{background:#eef1fb;color:#2b3a8f;padding:2px 6px;border-radius:5px;font-size:.9em}\n.dd-post .dd-file{display:inline-block;background:#151b3b;color:#c9d1ff;font:600 13px\/1 \"JetBrains Mono\",Consolas,monospace;padding:7px 12px;border-radius:8px 8px 0 0;margin:1em 0 -1.2em;position:relative}\n.dd-post .dd-hero{margin:0 0 1.6em;border-radius:16px;overflow:hidden;box-shadow:0 10px 30px rgba(11,16,32,.25)}\n.dd-post .dd-hero svg{display:block;width:100%;height:auto}\n.dd-post .dd-hero .node{transition:transform .25s,filter .25s;transform-box:fill-box;transform-origin:center;cursor:pointer}\n.dd-post .dd-hero .node:hover{transform:scale(1.08);filter:drop-shadow(0 0 10px #7c5cff)}\n.dd-post .dd-series{display:inline-flex;align-items:center;gap:8px;background:#151b3b;color:#dfe3ff;border-radius:999px;padding:6px 14px;font-size:.88em;font-weight:600;margin:0 0 .8em}\n.dd-post .dd-series span{background:var(--violet);color:#fff;border-radius:999px;padding:2px 10px}\n.dd-post .dd-prereq{font-size:.96em;color:var(--muted);border-left:3px solid var(--line);padding-left:12px;margin:.6em 0 1.2em}\n.dd-post .dd-summary{background:linear-gradient(135deg,#eef3ff,#f4efff);border:1px solid #d6dcf5;border-left:5px solid var(--violet);border-radius:12px;padding:18px 22px;margin:1.4em 0}\n.dd-post .dd-summary strong.lbl{display:block;font-size:1.05em;margin-bottom:6px;color:#3a2fa0}\n.dd-post .dd-summary ul{margin:.3em 0 0 1.1em}\n.dd-post .dd-toc{background:#f8f9fc;border:1px solid var(--line);border-radius:12px;padding:16px 22px;margin:1.4em 0}\n.dd-post .dd-toc ol{margin:.4em 0 0 1.2em;columns:2;column-gap:28px}\n.dd-post .dd-toc li{break-inside:avoid;margin:.15em 0}\n@media(max-width:640px){.dd-post .dd-toc ol{columns:1}}\n.dd-post .dd-tip,.dd-post .dd-note,.dd-post .dd-warn{border-radius:10px;padding:14px 18px;margin:1.3em 0}\n.dd-post .dd-tip{background:#ecfbf3;border-left:4px solid #1f9d61}\n.dd-post .dd-note{background:#eef4ff;border-left:4px solid var(--blue)}\n.dd-post .dd-warn{background:#fff6e8;border-left:4px solid #e08a00}\n.dd-post .dd-cards{display:grid;grid-template-columns:repeat(auto-fit,minmax(210px,1fr));gap:14px;margin:1.3em 0}\n.dd-post .dd-card{border:1px solid var(--line);border-radius:12px;padding:14px 16px;background:#fff;box-shadow:0 2px 8px rgba(20,30,70,.05)}\n.dd-post .dd-card b{display:block;color:#3a2fa0;margin-bottom:4px}\n.dd-post table{width:100%;border-collapse:collapse;margin:1.3em 0;font-size:.95em}\n.dd-post th,.dd-post td{border:1px solid var(--line);padding:10px 12px;text-align:left;vertical-align:top}\n.dd-post th{background:#151b3b;color:#fff}\n.dd-post tr:nth-child(even) td{background:#f8f9fc}\n.dd-post .dd-tablewrap{overflow-x:auto}\n.dd-post figure{margin:1.6em 0;text-align:center}\n.dd-post figure svg{width:100%;height:auto;border-radius:12px}\n.dd-post figcaption{font-size:.9em;color:var(--muted);margin-top:8px}\n.dd-post details{border:1px solid var(--line);border-radius:10px;padding:12px 16px;margin:.7em 0;background:#fff}\n.dd-post details summary{cursor:pointer;font-weight:600}\n.dd-post details[open] summary{margin-bottom:8px;color:#3a2fa0}\n.dd-post .dd-roadmap{border:2px solid #d6dcf5;background:#fbfbff}\n.dd-post .dd-roadmap h4{margin:1em 0 .3em;color:#151b3b;font-size:1em;text-transform:uppercase;letter-spacing:.04em}\n.dd-post .dd-roadmap ol{margin:.2em 0 .6em 1.4em;padding:0}\n.dd-post .dd-roadmap li{margin:.2em 0}\n.dd-post .dd-roadmap .here{background:var(--violet);color:#fff;border-radius:999px;padding:1px 9px;font-size:.8em;font-weight:700;margin-left:6px}\n.dd-post .dd-roadmap .soon{color:#8a90a6;font-size:.82em;margin-left:6px}\n.dd-post .dd-tabs{border:1px solid var(--line);border-radius:12px;padding:6px 16px 16px;margin:1.4em 0;background:#fbfbfe}\n.dd-post .dd-tabs>input{position:absolute;opacity:0;pointer-events:none}\n.dd-post .dd-tabs .labels label{display:inline-block;padding:8px 16px;margin:10px 6px 0 0;border-radius:999px;border:1px solid #cfd5ea;cursor:pointer;font-weight:600;font-size:.95em;background:#fff;transition:background .2s}\n.dd-post .dd-tabs .labels label:hover{border-color:var(--violet)}\n.dd-post .dd-tabs .panel{display:none;margin-top:12px}\n.dd-post .dd-tabs #ti1:checked~.labels label[for=ti1],.dd-post .dd-tabs #ti2:checked~.labels label[for=ti2],.dd-post .dd-tabs #ti3:checked~.labels label[for=ti3],.dd-post .dd-tabs #ti4:checked~.labels label[for=ti4]{background:var(--violet);color:#fff;border-color:var(--violet)}\n.dd-post .dd-tabs #ti1:checked~.p1,.dd-post .dd-tabs #ti2:checked~.p2,.dd-post .dd-tabs #ti3:checked~.p3,.dd-post .dd-tabs #ti4:checked~.p4{display:block}\n.dd-post .dd-tabs #ti1:focus-visible~.labels label[for=ti1],.dd-post .dd-tabs #ti2:focus-visible~.labels label[for=ti2],.dd-post .dd-tabs #ti3:focus-visible~.labels label[for=ti3],.dd-post .dd-tabs #ti4:focus-visible~.labels label[for=ti4]{outline:2px solid var(--blue);outline-offset:2px}\n.dd-post .dd-cta{background:linear-gradient(135deg,#0b1020,#151b3b);color:#e8ebff;border-radius:16px;padding:24px 26px;margin:2em 0;text-align:center}\n.dd-post .dd-cta h3{color:#fff;margin-top:0}\n.dd-post .dd-cta a{color:#a99bff}\n.dd-post .dd-vsc{position:relative;margin:1.5em 0;border-radius:12px;overflow:hidden;background:#1e1e1e;border:1px solid #2b2b2b;box-shadow:0 14px 34px rgba(0,0,0,.28);font-family:\"Segoe UI\",system-ui,sans-serif}\n.dd-post .dd-vsc>input{position:absolute;opacity:0;pointer-events:none}\n.dd-post .dd-vsc-bar{display:flex;align-items:center;gap:12px;background:#323233;color:#cccccc;font-size:12.5px;padding:8px 12px}\n.dd-post .dd-dots{display:inline-flex;gap:7px}.dd-post .dd-dots i{width:12px;height:12px;border-radius:50%;background:#ff5f57;display:block}.dd-post .dd-dots i:nth-child(2){background:#febc2e}.dd-post .dd-dots i:nth-child(3){background:#28c840}\n.dd-post .dd-vsc-title{flex:1;text-align:center;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;padding-right:50px;opacity:.85}\n.dd-post .dd-vsc-tabs{display:flex;align-items:stretch;background:#252526;border-bottom:1px solid #1e1e1e;overflow-x:auto}\n.dd-post .dd-vsc-tabs label{display:flex;align-items:center;gap:7px;padding:9px 16px;color:#969696;font-size:13px;cursor:pointer;background:#2d2d2d;border-right:1px solid #252526;border-top:1px solid transparent;white-space:nowrap;margin:0;transition:color .15s}\n.dd-post .dd-vsc-tabs label:hover{color:#e0e0e0}\n.dd-post .dd-vsc-tabs .ic{font:700 9.5px\/1 Consolas,monospace;padding:3px 4px;border-radius:3px;color:#1e1e1e}\n.dd-post .ic.py{background:#ffd43b}.dd-post .ic.js{background:#f1e05a}.dd-post .ic.sh{background:#89e051}\n.dd-post .dd-vsc-tabs label.run{margin-left:auto;background:transparent;border:0;color:#89d185;font-weight:600;display:none}\n.dd-post .dd-vsc-tabs label.run:hover{color:#b5f0b0;background:#2a2d2e}\n.dd-post .dd-vsc-pane{display:none}\n.dd-post .dd-vsc>.f1:checked~.dd-vsc-tabs .t1,.dd-post .dd-vsc>.f2:checked~.dd-vsc-tabs .t2,.dd-post .dd-vsc>.f3:checked~.dd-vsc-tabs .t3{background:#1e1e1e;color:#ffffff;border-top-color:#3178c6}\n.dd-post .dd-vsc>.f1:checked~.p1,.dd-post .dd-vsc>.f2:checked~.p2,.dd-post .dd-vsc>.f3:checked~.p3{display:block}\n.dd-post .dd-vsc>.f1:checked~.dd-vsc-tabs .run1,.dd-post .dd-vsc>.f2:checked~.dd-vsc-tabs .run2,.dd-post .dd-vsc>.f3:checked~.dd-vsc-tabs .run3{display:flex}\n.dd-post .dd-vsc>.f1:focus-visible~.dd-vsc-tabs .t1,.dd-post .dd-vsc>.f2:focus-visible~.dd-vsc-tabs .t2,.dd-post .dd-vsc>.f3:focus-visible~.dd-vsc-tabs .t3,.dd-post .dd-vsc>input[type=checkbox]:focus-visible~.dd-vsc-tabs .run{outline:2px solid #3178c6;outline-offset:-2px}\n.dd-post .dd-vsc-crumbs{background:#1e1e1e;color:#a9a9a9;font-size:12px;padding:5px 16px 2px}\n.dd-post .dd-vsc pre.dd-vsc-code{background:#1e1e1e;color:#d4d4d4;border:0;border-radius:0;margin:0;padding:8px 0 14px;counter-reset:ln;line-height:1.62;overflow-x:auto}\n.dd-post .dd-vsc pre code{font-family:Consolas,\"JetBrains Mono\",\"Fira Code\",Menlo,monospace;color:inherit;background:none;white-space:pre}\n.dd-post .dd-vsc .l{display:inline-block;min-width:100%;padding-right:18px;box-sizing:border-box}\n.dd-post .dd-vsc .l::before{counter-increment:ln;content:counter(ln);display:inline-block;width:3.2em;padding-right:1.4em;text-align:right;color:#6e7681;user-select:none;-webkit-user-select:none}\n.dd-post .dd-vsc .l:hover{background:#2a2d2e}.dd-post .dd-vsc .l:hover::before{color:#c6c6c6}\n.dd-post .dd-vsc .k,.dd-post .dd-vsc .kn{color:#c586c0}.dd-post .dd-vsc .kc{color:#569cd6}.dd-post .dd-vsc .s{color:#ce9178}.dd-post .dd-vsc .c{color:#6a9955}.dd-post .dd-vsc .m{color:#b5cea8}.dd-post .dd-vsc .nf{color:#dcdcaa}.dd-post .dd-vsc .nc{color:#4ec9b0}.dd-post .dd-vsc .nb{color:#4ec9b0}.dd-post .dd-vsc .nd{color:#dcdcaa}.dd-post .dd-vsc .n{color:#9cdcfe}.dd-post .dd-vsc .o{color:#d4d4d4}.dd-post .dd-vsc .gp{color:#89d185;user-select:none;-webkit-user-select:none}\n.dd-post .dd-vsc-term{display:none;border-top:1px solid #3c3c3c;background:#181818}\n.dd-post .dd-vsc>.r1:checked~.p1 .dd-vsc-term,.dd-post .dd-vsc>.r2:checked~.p2 .dd-vsc-term,.dd-post .dd-vsc>.r3:checked~.p3 .dd-vsc-term{display:block;animation:ddterm .35s ease-out}\n@keyframes ddterm{from{opacity:0;transform:translateY(6px)}to{opacity:1;transform:none}}\n.dd-post .dd-term-head{display:flex;gap:18px;align-items:center;font-size:11px;letter-spacing:.06em;color:#8b8b8b;padding:7px 16px 0}\n.dd-post .dd-term-head span:first-child{color:#e7e7e7;border-bottom:1px solid #e7e7e7;padding-bottom:4px}\n.dd-post .dd-term-head .dd-term-note{margin-left:auto;letter-spacing:0;text-transform:none;color:#d7ba7d;border:0;font-size:11.5px}\n.dd-post .dd-vsc-term pre{background:#181818;color:#cccccc;border:0;margin:0;padding:10px 16px 14px;border-radius:0}\n.dd-post .dd-vsc-status{display:flex;flex-wrap:wrap;gap:4px 14px;background:#007acc;color:#fff;font-size:11.5px;padding:4px 12px}\n.dd-post .dd-vsc-status .sp{flex:1}\n.dd-post .dd-vsc-termonly pre.dd-vsc-code{background:#181818;padding:12px 16px}\n@media(max-width:640px){.dd-post .dd-vsc-title{padding-right:0;text-align:left}.dd-post .dd-vsc-status span:nth-child(n+4){display:none}.dd-post .dd-vsc .l::before{width:2.2em;padding-right:1em}}\n.dd-post .dd-vsc-hint{font-size:.93em;color:var(--muted);margin-top:-.6em}\n.dd-post .dd-next{border:1px dashed var(--violet);border-radius:12px;padding:16px 20px;margin:1.6em 0;background:#f7f4ff}\n<\/style>\n\n<div class=\"dd-hero\">\n<svg viewBox=\"0 0 1200 520\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" role=\"img\" aria-label=\"Agentic AI series Part 2 banner: an AI agent at the centre connected to its four parts - model, tools, instructions and memory\">\n<defs>\n<linearGradient id=\"ddbg\" x1=\"0\" y1=\"0\" x2=\"1\" y2=\"1\"><stop offset=\"0\" stop-color=\"#0b1020\"\/><stop offset=\"1\" stop-color=\"#151b3b\"\/><\/linearGradient>\n<linearGradient id=\"ddac\" x1=\"0\" y1=\"0\" x2=\"1\" y2=\"0\"><stop offset=\"0\" stop-color=\"#3178c6\"\/><stop offset=\"1\" stop-color=\"#7c5cff\"\/><\/linearGradient>\n<radialGradient id=\"ddglow\" cx=\".5\" cy=\".5\" r=\".5\"><stop offset=\"0\" stop-color=\"#7c5cff\" stop-opacity=\".45\"\/><stop offset=\"1\" stop-color=\"#7c5cff\" stop-opacity=\"0\"\/><\/radialGradient>\n<\/defs>\n<rect x=\"0\" y=\"-60\" width=\"1200\" height=\"640\" fill=\"url(#ddbg)\"\/>\n<circle cx=\"900\" cy=\"290\" r=\"260\" fill=\"url(#ddglow)\"\/>\n<g opacity=\".12\" stroke=\"#9fb2ff\"><path d=\"M0 100H1200M0 200H1200M0 300H1200M0 400H1200M200 0V520M400 0V520M600 0V520M800 0V520M1000 0V520\"\/><\/g>\n<rect x=\"60\" y=\"46\" width=\"128\" height=\"38\" rx=\"19\" fill=\"url(#ddac)\"\/>\n<text x=\"124\" y=\"71\" text-anchor=\"middle\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"18\" font-weight=\"700\" fill=\"#fff\">PART 2<\/text>\n<text x=\"204\" y=\"71\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"18\" fill=\"#aab4e8\">Agentic AI Series \u00b7 DevDojo<\/text>\n<text x=\"60\" y=\"150\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"54\" font-weight=\"800\" fill=\"#ffffff\">Anatomy of an<\/text>\n<text x=\"60\" y=\"212\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"54\" font-weight=\"800\" fill=\"url(#ddac)\">AI Agent<\/text>\n<text x=\"60\" y=\"260\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"24\" fill=\"#c9d1ff\">Models, tools, instructions and memory<\/text>\n<text x=\"60\" y=\"292\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"24\" fill=\"#c9d1ff\">explained with runnable code<\/text>\n<g fill=\"none\" stroke=\"url(#ddac)\" stroke-width=\"3\" stroke-dasharray=\"8 8\" opacity=\".75\">\n<path id=\"ddp1\" d=\"M853 243 L790 180\"\/>\n<path id=\"ddp2\" d=\"M947 243 L1010 180\"\/>\n<path id=\"ddp3\" d=\"M853 337 L790 400\"\/>\n<path id=\"ddp4\" d=\"M947 337 L1010 400\"\/>\n<\/g>\n<g class=\"node\"><rect x=\"680\" y=\"118\" width=\"160\" height=\"62\" rx=\"14\" fill=\"#1b2250\" stroke=\"#3178c6\" stroke-width=\"2\"\/><text x=\"760\" y=\"146\" text-anchor=\"middle\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"21\" font-weight=\"800\" fill=\"#fff\">Model<\/text><text x=\"760\" y=\"168\" text-anchor=\"middle\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"14\" fill=\"#aab4e8\">the brain<\/text><\/g>\n<g class=\"node\"><rect x=\"970\" y=\"118\" width=\"160\" height=\"62\" rx=\"14\" fill=\"#1b2250\" stroke=\"#3178c6\" stroke-width=\"2\"\/><text x=\"1050\" y=\"146\" text-anchor=\"middle\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"21\" font-weight=\"800\" fill=\"#fff\">Tools<\/text><text x=\"1050\" y=\"168\" text-anchor=\"middle\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"14\" fill=\"#aab4e8\">the hands<\/text><\/g>\n<g class=\"node\"><rect x=\"680\" y=\"400\" width=\"160\" height=\"62\" rx=\"14\" fill=\"#1b2250\" stroke=\"#7c5cff\" stroke-width=\"2\"\/><text x=\"760\" y=\"428\" text-anchor=\"middle\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"21\" font-weight=\"800\" fill=\"#fff\">Instructions<\/text><text x=\"760\" y=\"450\" text-anchor=\"middle\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"14\" fill=\"#aab4e8\">the job description<\/text><\/g>\n<g class=\"node\"><rect x=\"970\" y=\"400\" width=\"160\" height=\"62\" rx=\"14\" fill=\"#1b2250\" stroke=\"#7c5cff\" stroke-width=\"2\"\/><text x=\"1050\" y=\"428\" text-anchor=\"middle\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"21\" font-weight=\"800\" fill=\"#fff\">Memory<\/text><text x=\"1050\" y=\"450\" text-anchor=\"middle\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"14\" fill=\"#aab4e8\">the notebook<\/text><\/g>\n<g class=\"node\"><circle cx=\"900\" cy=\"290\" r=\"66\" fill=\"#0b1020\" stroke=\"url(#ddac)\" stroke-width=\"4\"\/><text x=\"900\" y=\"285\" text-anchor=\"middle\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"21\" font-weight=\"800\" fill=\"#fff\">AI Agent<\/text><text x=\"900\" y=\"309\" text-anchor=\"middle\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"14\" fill=\"#aab4e8\">loop + runtime<\/text><\/g>\n<circle r=\"7\" fill=\"#7c5cff\"><animateMotion dur=\"2.6s\" repeatCount=\"indefinite\"><mpath href=\"#ddp1\"\/><\/animateMotion><\/circle>\n<circle r=\"7\" fill=\"#3178c6\"><animateMotion dur=\"2.6s\" begin=\".65s\" repeatCount=\"indefinite\"><mpath href=\"#ddp2\"\/><\/animateMotion><\/circle>\n<circle r=\"7\" fill=\"#ffffff\"><animateMotion dur=\"2.6s\" begin=\"1.3s\" repeatCount=\"indefinite\"><mpath href=\"#ddp3\"\/><\/animateMotion><\/circle>\n<circle r=\"7\" fill=\"#61dafb\"><animateMotion dur=\"2.6s\" begin=\"1.95s\" repeatCount=\"indefinite\"><mpath href=\"#ddp4\"\/><\/animateMotion><\/circle>\n<g font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"15\" fill=\"#c9d1ff\">\n<rect x=\"60\" y=\"350\" width=\"150\" height=\"36\" rx=\"18\" fill=\"#0b1020\" stroke=\"#7c5cff\" stroke-width=\"2\"\/><text x=\"135\" y=\"374\" text-anchor=\"middle\">Python<\/text>\n<rect x=\"222\" y=\"350\" width=\"150\" height=\"36\" rx=\"18\" fill=\"#0b1020\" stroke=\"#7c5cff\" stroke-width=\"2\"\/><text x=\"297\" y=\"374\" text-anchor=\"middle\">Node.js<\/text>\n<rect x=\"384\" y=\"350\" width=\"170\" height=\"36\" rx=\"18\" fill=\"#0b1020\" stroke=\"#7c5cff\" stroke-width=\"2\"\/><text x=\"469\" y=\"374\" text-anchor=\"middle\">No API key needed<\/text>\n<\/g>\n<text x=\"60\" y=\"505\" font-family=\"Segoe UI,Arial,sans-serif\" font-size=\"16\" fill=\"#7f89b5\">devdojo.co.in \u00b7 Beginner friendly \u00b7 Python + Node.js examples<\/text>\n<\/svg>\n<\/div>\n\n<div class=\"dd-series\">Series: Agentic AI <span>Part 2 of 30<\/span><\/div>\n<p class=\"dd-prereq\"><strong>What you should know first:<\/strong> read <a href=\"https:\/\/devdojo.co.in\/index.php\/2026\/10\/20\/agentic-ai-series-part-1-what-is-agentic-ai\/\">Agentic AI Part 1: What Is Agentic AI and How Do AI Agents Work?<\/a>, where we built our first agent loop. Basic Python or JavaScript is enough. If LLM words like &#8220;tokens&#8221; and &#8220;context window&#8221; are new to you, see <a href=\"https:\/\/devdojo.co.in\/?p=219\">Generative AI Part 2: Tokens, Context Windows and Model Parameters<\/a>.<\/p>\n\n<p>In Part 1 you saw that an AI agent is &#8220;an LLM in a loop that can use tools&#8221;. That is true, but when you sit down to build one, you quickly ask: <em>which model should I pick? How do I describe my tools? What do I write in the instructions? Where does the agent remember things?<\/em> This post answers all of that. We will take a close look at the <strong>anatomy of an AI agent<\/strong>: the four building blocks every agent is made of (the <strong>model<\/strong>, the <strong>tools<\/strong>, the <strong>instructions<\/strong> and the <strong>memory<\/strong>) plus the small runtime loop that connects them. You will build each part in Python and Node.js, run everything without an API key, and then plug the same design into a real LLM.<\/p>\n\n<div class=\"dd-summary\">\n<strong class=\"lbl\">Quick Summary<\/strong>\n<ul>\n<li>Every AI agent has <strong>four parts<\/strong>: a <strong>model<\/strong> (the brain that decides), <strong>tools<\/strong> (functions it can call), <strong>instructions<\/strong> (its job description) and <strong>memory<\/strong> (what it knows so far).<\/li>\n<li>A small <strong>runtime loop<\/strong> in your code connects them: send instructions + memory + tool list to the model, run the tool it picks, save the result, repeat until it answers or a step limit is hit.<\/li>\n<li>A tool = <strong>name + description + input schema + a real function<\/strong>. The description is what the model reads, so write it clearly.<\/li>\n<li>Good instructions have six sections: role, goal, rules, tool tips, output format and when to stop.<\/li>\n<li>Memory comes in three kinds: <strong>short-term<\/strong> (chat history), <strong>scratchpad<\/strong> (notes for this task) and <strong>long-term<\/strong> (saved facts that survive restarts).<\/li>\n<\/ul>\n<\/div>\n\n<nav class=\"dd-toc\" aria-label=\"Table of contents\">\n<strong>Table of contents<\/strong>\n<ol>\n<li><a href=\"#anatomy\">The anatomy of an AI agent at a glance<\/a><\/li>\n<li><a href=\"#explore\">Explore the four parts<\/a><\/li>\n<li><a href=\"#model\">Part 1 of the agent: the model<\/a><\/li>\n<li><a href=\"#tools\">Part 2 of the agent: tools<\/a><\/li>\n<li><a href=\"#instructions\">Part 3 of the agent: instructions<\/a><\/li>\n<li><a href=\"#memory\">Part 4 of the agent: memory<\/a><\/li>\n<li><a href=\"#runtime\">The runtime loop that connects everything<\/a><\/li>\n<li><a href=\"#together\">Build an agent with all four parts<\/a><\/li>\n<li><a href=\"#real-llm\">Plug in a real LLM<\/a><\/li>\n<li><a href=\"#common-errors\">Common errors and fixes<\/a><\/li>\n<li><a href=\"#best-practices\">Best practices<\/a><\/li>\n<li><a href=\"#practice\">Practice task<\/a><\/li>\n<li><a href=\"#faq\">FAQ<\/a><\/li>\n<li><a href=\"#conclusion\">Conclusion<\/a><\/li>\n<\/ol>\n<\/nav>\n\n<details class=\"dd-roadmap\">\n<summary>Full Agentic AI roadmap (30 parts)<\/summary>\n<h4>Beginner<\/h4>\n<ol start=\"1\">\n<li><a href=\"https:\/\/devdojo.co.in\/index.php\/2026\/10\/20\/agentic-ai-series-part-1-what-is-agentic-ai\/\">What Is Agentic AI and How Do AI Agents Work?<\/a><\/li>\n<li>Anatomy of an AI Agent: Models, Tools, Instructions and Memory <span class=\"here\">You are here<\/span><\/li>\n<li>Tool Calling for Agents: Letting an LLM Use Your Functions <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Build Your First AI Agent from Scratch in Python (No Framework) <span class=\"soon\">Coming soon<\/span><\/li>\n<li>The ReAct Pattern: Reason, Act, Observe in Practice <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Writing Good Agent Instructions and Tool Descriptions <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Agent Memory: Short-Term, Long-Term and Scratchpads <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Planning Agents: Breaking Big Goals into Steps <span class=\"soon\">Coming soon<\/span><\/li>\n<\/ol>\n<h4>Intermediate<\/h4>\n<ol start=\"9\">\n<li>Workflow Patterns: Chaining, Routing and Parallel Steps <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Model Context Protocol (MCP): Connecting Agents to Tools and Data <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Agent Frameworks Compared: OpenAI Agents SDK, LangGraph, Claude Agent SDK and More <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Building Stateful Agents with LangGraph <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Agentic RAG: Agents That Search, Read and Cite <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Browser and Computer-Use Agents <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Coding Agents: How AI Writes, Runs and Fixes Code <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Human-in-the-Loop: Approvals, Interrupts and Checkpoints <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Multi-Agent Systems: Handoffs, Supervisors and Teams <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Agent-to-Agent Communication with the A2A Protocol <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Guardrails and Structured Outputs for Agents <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Tracing and Debugging Agents <span class=\"soon\">Coming soon<\/span><\/li>\n<\/ol>\n<h4>Advanced<\/h4>\n<ol start=\"21\">\n<li>Evaluating Agents: Task Success, Trajectories and Test Suites <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Agent Security: Prompt Injection, Permissions and Sandboxing <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Controlling Agent Cost and Latency <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Long-Running Agents: Queues, Retries and Durable Execution <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Deep Research Agents: Search, Synthesise and Report <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Reflection and Self-Correcting Agents <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Voice and Real-Time Agents <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Deploying Agents to Production: APIs, Serverless and Containers <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Monitoring and Improving Agents in Production <span class=\"soon\">Coming soon<\/span><\/li>\n<li>Final Project: Build a Multi-Agent Developer Assistant <span class=\"soon\">Coming soon<\/span><\/li>\n<\/ol>\n<\/details>\n\n<h2 id=\"anatomy\">The anatomy of an AI agent at a glance<\/h2>\n<p>Think of an AI agent like a new employee you hire for a support desk. To do the job well, that person needs four things:<\/p>\n<div class=\"dd-cards\">\n<div class=\"dd-card\"><b>1. A brain \u2192 the model<\/b>The LLM (for example a GPT, Claude or Gemini model) that reads the situation and decides what to do next.<\/div>\n<div class=\"dd-card\"><b>2. Hands \u2192 tools<\/b>Functions the agent can call: look up an order, search the web, run SQL, send an email.<\/div>\n<div class=\"dd-card\"><b>3. A job description \u2192 instructions<\/b>The system prompt that says who the agent is, what its goal is and which rules it must follow.<\/div>\n<div class=\"dd-card\"><b>4. A notebook \u2192 memory<\/b>Everything the agent knows right now: the chat so far, tool results, notes, and saved facts.<\/div>\n<\/div>\n<p>None of these parts is an agent on its own. A model without tools can only talk. Tools without a model never run. Without instructions the agent has no focus, and without memory it forgets the result of the tool it called one second ago. The magic happens in <strong>your code<\/strong>, which connects the four parts in a loop.<\/p>\n\n<figure>\n<svg viewBox=\"0 0 900 430\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" role=\"img\" aria-label=\"Diagram: the runtime loop sends instructions, memory and the tool list to the model; the model either answers or asks for a tool; the tool result is saved into memory\">\n<rect width=\"900\" height=\"430\" rx=\"16\" fill=\"#0f1430\"\/>\n<g font-family=\"Segoe UI,Arial,sans-serif\">\n<rect x=\"340\" y=\"30\" width=\"220\" height=\"70\" rx=\"14\" fill=\"url(#ddf1)\"\/>\n<text x=\"450\" y=\"62\" text-anchor=\"middle\" font-size=\"20\" font-weight=\"800\" fill=\"#fff\">Model (LLM)<\/text>\n<text x=\"450\" y=\"85\" text-anchor=\"middle\" font-size=\"14\" fill=\"#eef\">decides the next step<\/text>\n<rect x=\"320\" y=\"175\" width=\"260\" height=\"80\" rx=\"14\" fill=\"#151b3b\" stroke=\"#7c5cff\" stroke-width=\"2\"\/>\n<text x=\"450\" y=\"208\" text-anchor=\"middle\" font-size=\"19\" font-weight=\"800\" fill=\"#fff\">Runtime loop (your code)<\/text>\n<text x=\"450\" y=\"232\" text-anchor=\"middle\" font-size=\"14\" fill=\"#aab4e8\">step limit \u00b7 error handling \u00b7 logs<\/text>\n<rect x=\"40\" y=\"175\" width=\"200\" height=\"80\" rx=\"14\" fill=\"#151b3b\" stroke=\"#3178c6\" stroke-width=\"2\"\/>\n<text x=\"140\" y=\"208\" text-anchor=\"middle\" font-size=\"18\" font-weight=\"800\" fill=\"#fff\">Instructions<\/text>\n<text x=\"140\" y=\"232\" text-anchor=\"middle\" font-size=\"14\" fill=\"#aab4e8\">role, goal, rules<\/text>\n<rect x=\"660\" y=\"175\" width=\"200\" height=\"80\" rx=\"14\" fill=\"#151b3b\" stroke=\"#3178c6\" stroke-width=\"2\"\/>\n<text x=\"760\" y=\"208\" text-anchor=\"middle\" font-size=\"18\" font-weight=\"800\" fill=\"#fff\">Tools<\/text>\n<text x=\"760\" y=\"232\" text-anchor=\"middle\" font-size=\"14\" fill=\"#aab4e8\">schemas + functions<\/text>\n<rect x=\"340\" y=\"330\" width=\"220\" height=\"70\" rx=\"14\" fill=\"#151b3b\" stroke=\"#61dafb\" stroke-width=\"2\"\/>\n<text x=\"450\" y=\"361\" text-anchor=\"middle\" font-size=\"18\" font-weight=\"800\" fill=\"#fff\">Memory<\/text>\n<text x=\"450\" y=\"384\" text-anchor=\"middle\" font-size=\"14\" fill=\"#aab4e8\">history, notes, saved facts<\/text>\n<g stroke=\"#9fb2ff\" stroke-width=\"2.5\" fill=\"none\" marker-end=\"url(#ddar)\">\n<path d=\"M420 175 V108\"\/><path d=\"M480 108 V175\"\/>\n<path d=\"M240 215 H312\"\/><path d=\"M580 205 H652\"\/><path d=\"M652 228 H588\"\/>\n<path d=\"M430 255 V322\"\/><path d=\"M470 322 V263\"\/>\n<\/g>\n<text x=\"360\" y=\"145\" font-size=\"13\" fill=\"#c9d1ff\">prompt<\/text>\n<text x=\"490\" y=\"145\" font-size=\"13\" fill=\"#c9d1ff\">answer or tool call<\/text>\n<text x=\"590\" y=\"195\" font-size=\"13\" fill=\"#c9d1ff\">run tool<\/text>\n<text x=\"600\" y=\"248\" font-size=\"13\" fill=\"#c9d1ff\">result<\/text>\n<text x=\"340\" y=\"295\" font-size=\"13\" fill=\"#c9d1ff\">save<\/text>\n<text x=\"480\" y=\"295\" font-size=\"13\" fill=\"#c9d1ff\">read<\/text>\n<\/g>\n<defs>\n<linearGradient id=\"ddf1\" x1=\"0\" y1=\"0\" x2=\"1\" y2=\"0\"><stop offset=\"0\" stop-color=\"#3178c6\"\/><stop offset=\"1\" stop-color=\"#7c5cff\"\/><\/linearGradient>\n<marker id=\"ddar\" viewBox=\"0 0 10 10\" refX=\"8\" refY=\"5\" markerWidth=\"7\" markerHeight=\"7\" orient=\"auto\"><path d=\"M0 0L10 5L0 10z\" fill=\"#9fb2ff\"\/><\/marker>\n<\/defs>\n<\/svg>\n<figcaption>The anatomy of an AI agent: four parts, connected by a runtime loop that you write.<\/figcaption>\n<\/figure>\n\n<h2 id=\"explore\">Explore the four parts<\/h2>\n<p>Click each part to see what it does, what can go wrong, and the one question to ask when you design it.<\/p>\n<div class=\"dd-tabs\">\n<input type=\"radio\" name=\"ddt\" id=\"ti1\" checked><input type=\"radio\" name=\"ddt\" id=\"ti2\"><input type=\"radio\" name=\"ddt\" id=\"ti3\"><input type=\"radio\" name=\"ddt\" id=\"ti4\">\n<div class=\"labels\"><label for=\"ti1\">Model<\/label><label for=\"ti2\">Tools<\/label><label for=\"ti3\">Instructions<\/label><label for=\"ti4\">Memory<\/label><\/div>\n<div class=\"panel p1\"><p><strong>Job:<\/strong> read the instructions, memory and tool list, then decide: answer now, or call a tool?<\/p><p><strong>Goes wrong when:<\/strong> the model is too small for the task, so it picks the wrong tool or invents arguments.<\/p><p><strong>Design question:<\/strong> &#8220;What is the cheapest model that still passes my test cases?&#8221;<\/p><\/div>\n<div class=\"panel p2\"><p><strong>Job:<\/strong> do real work in the outside world: read data, call APIs, change things.<\/p><p><strong>Goes wrong when:<\/strong> descriptions are vague, two tools overlap, or a tool can do something dangerous with no checks.<\/p><p><strong>Design question:<\/strong> &#8220;Could a new team member use this tool correctly from its name and description alone?&#8221;<\/p><\/div>\n<div class=\"panel p3\"><p><strong>Job:<\/strong> give the agent its identity, goal, rules, tool tips and stopping rule.<\/p><p><strong>Goes wrong when:<\/strong> instructions are a wall of text, contradict each other, or never say when to stop.<\/p><p><strong>Design question:<\/strong> &#8220;If the agent breaks a rule, is the rule actually written down clearly?&#8221;<\/p><\/div>\n<div class=\"panel p4\"><p><strong>Job:<\/strong> keep the chat history, tool results and saved facts the model needs.<\/p><p><strong>Goes wrong when:<\/strong> history grows past the context window, or old, wrong facts are never cleaned up.<\/p><p><strong>Design question:<\/strong> &#8220;What must the agent remember for this task, and what can it forget?&#8221;<\/p><\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"dd-post\">\n<h2 id=\"model\">Part 1 of the agent: the model (the brain)<\/h2>\n<p>The <strong>model<\/strong> is the large language model that makes every decision. In each turn of the loop it receives three things (instructions, memory and the list of tools) and returns either a final answer or a <em>tool call<\/em>, like &#8220;call <code>get_order_status<\/code> with <code>order_id = A102<\/code>&#8220;. It never runs the tool itself. Your code does that.<\/p>\n<h3>How to choose a model for an agent<\/h3>\n<p>Agents put more pressure on a model than a simple chat does, because the model must make many correct decisions in a row. Look at these five things:<\/p>\n<div class=\"dd-tablewrap\"><table>\n<thead><tr><th>What to check<\/th><th>Why it matters for agents<\/th><\/tr><\/thead>\n<tbody>\n<tr><td>Tool calling support<\/td><td>The model must return clean, structured tool calls. All major API models (OpenAI, Anthropic, Google, and many open models) support this today.<\/td><\/tr>\n<tr><td>Reasoning quality<\/td><td>Multi-step tasks need a model that plans well and recovers from errors. Bigger or &#8220;reasoning&#8221; models do this better.<\/td><\/tr>\n<tr><td>Context window<\/td><td>Every tool result is added to memory. Long tasks need a big context window or memory trimming.<\/td><\/tr>\n<tr><td>Speed (latency)<\/td><td>An agent may call the model 5\u201320 times for one task. A slow model makes the whole agent feel slow.<\/td><\/tr>\n<tr><td>Cost<\/td><td>Many calls \u00d7 many tokens adds up quickly. Small models can be 10\u00d7 or more cheaper.<\/td><\/tr>\n<\/tbody><\/table><\/div>\n<div class=\"dd-tip\"><strong>Simple rule:<\/strong> start building with a strong model so you know the task is possible. Then write a few test cases and try a smaller, cheaper model. Keep the small one only if it still passes. You can also mix: a small model for easy routing, a strong one for hard reasoning.<\/div>\n<p>Keep the model name in an environment variable (for example <code>OPENAI_MODEL<\/code>) instead of hard-coding it. Model names change often, and this lets you switch without editing code.<\/p>\n\n<h2 id=\"tools\">Part 2 of the agent: tools (the hands)<\/h2>\n<p>A <strong>tool<\/strong> is just a normal function plus a short description that the model can read. Each tool has four pieces:<\/p>\n<ol>\n<li><strong>Name<\/strong>: short and clear, like <code>get_order_status<\/code>.<\/li>\n<li><strong>Description<\/strong>: one or two sentences saying what it does and when to use it. The model chooses tools mainly by reading this.<\/li>\n<li><strong>Input schema<\/strong>: a JSON Schema describing the arguments (names and types).<\/li>\n<li><strong>Function<\/strong>: the real code your program runs.<\/li>\n<\/ol>\n<p>Writing JSON schemas by hand gets boring, so let&#8217;s build a tiny <strong>tool registry<\/strong>. In Python a decorator reads the function&#8217;s name, docstring and type hints and builds the schema automatically. The Node.js version registers each tool with its schema and function side by side. Both include a safe <code>call_tool<\/code> helper that refuses unknown tools.<\/p>\n<div class=\"dd-vsc\" role=\"group\" aria-label=\"Code editor: tools.py, tools.mjs\">\n<input type=\"radio\" name=\"v1\" id=\"v1f1\" class=\"f1\" checked>\n<input type=\"radio\" name=\"v1\" id=\"v1f2\" class=\"f2\">\n<input type=\"checkbox\" id=\"v1r1\" class=\"r1\">\n<input type=\"checkbox\" id=\"v1r2\" class=\"r2\">\n<div class=\"dd-vsc-bar\"><span class=\"dd-dots\"><i><\/i><i><\/i><i><\/i><\/span><span class=\"dd-vsc-title\">2 files \u2014 agentic-ai-part2 \u2014 Visual Studio Code<\/span><\/div>\n<div class=\"dd-vsc-tabs\">\n<label for=\"v1f1\" class=\"t1\"><b class=\"ic py\">PY<\/b>tools.py<\/label>\n<label for=\"v1f2\" class=\"t2\"><b class=\"ic js\">JS<\/b>tools.mjs<\/label>\n<label for=\"v1r1\" class=\"run run1\" title=\"Show the output in the terminal\">\u25b6 Run<\/label>\n<label for=\"v1r2\" class=\"run run2\" title=\"Show the output in the terminal\">\u25b6 Run<\/label>\n<\/div>\n<div class=\"dd-vsc-pane p1\">\n<div class=\"dd-vsc-crumbs\">agentic-ai-part2 \u203a tools.py<\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\"><span class=\"c\"># A tiny tool registry: turn normal Python functions into agent tools.<\/span><\/span>\n<span class=\"l\"><span class=\"kn\">import<\/span> <span class=\"n\">inspect<\/span><\/span>\n<span class=\"l\"><span class=\"kn\">import<\/span> <span class=\"n\">json<\/span><\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"n\">TYPE_MAP<\/span> = {<span class=\"nb\">str<\/span>: <span class=\"s\">\"string\"<\/span>, <span class=\"nb\">int<\/span>: <span class=\"s\">\"integer\"<\/span>, <span class=\"nb\">float<\/span>: <span class=\"s\">\"number\"<\/span>, <span class=\"nb\">bool<\/span>: <span class=\"s\">\"boolean\"<\/span>}<\/span>\n<span class=\"l\"><span class=\"n\">REGISTRY<\/span> = {}<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">def<\/span> <span class=\"nf\">tool<\/span>(<span class=\"n\">func<\/span>):<\/span>\n<span class=\"l\">    <span class=\"s\">\"\"\"Decorator: read the function's name, docstring and type hints.\"\"\"<\/span><\/span>\n<span class=\"l\">    <span class=\"n\">params<\/span> = {}<\/span>\n<span class=\"l\">    <span class=\"k\">for<\/span> <span class=\"n\">name<\/span>, <span class=\"n\">p<\/span> <span class=\"k\">in<\/span> <span class=\"n\">inspect<\/span>.<span class=\"n\">signature<\/span>(<span class=\"n\">func<\/span>).<span class=\"n\">parameters<\/span>.<span class=\"n\">items<\/span>():<\/span>\n<span class=\"l\">        <span class=\"n\">params<\/span>[<span class=\"n\">name<\/span>] = {<span class=\"s\">\"type\"<\/span>: <span class=\"n\">TYPE_MAP<\/span>.<span class=\"n\">get<\/span>(<span class=\"n\">p<\/span>.<span class=\"n\">annotation<\/span>, <span class=\"s\">\"string\"<\/span>)}<\/span>\n<span class=\"l\">    <span class=\"n\">REGISTRY<\/span>[<span class=\"n\">func<\/span>.<span class=\"n\">__name__<\/span>] = {<\/span>\n<span class=\"l\">        <span class=\"s\">\"function\"<\/span>: <span class=\"n\">func<\/span>,<\/span>\n<span class=\"l\">        <span class=\"s\">\"schema\"<\/span>: {<\/span>\n<span class=\"l\">            <span class=\"s\">\"type\"<\/span>: <span class=\"s\">\"function\"<\/span>,<\/span>\n<span class=\"l\">            <span class=\"s\">\"name\"<\/span>: <span class=\"n\">func<\/span>.<span class=\"n\">__name__<\/span>,<\/span>\n<span class=\"l\">            <span class=\"s\">\"description\"<\/span>: <span class=\"n\">inspect<\/span>.<span class=\"n\">getdoc<\/span>(<span class=\"n\">func<\/span>),<\/span>\n<span class=\"l\">            <span class=\"s\">\"parameters\"<\/span>: {<\/span>\n<span class=\"l\">                <span class=\"s\">\"type\"<\/span>: <span class=\"s\">\"object\"<\/span>,<\/span>\n<span class=\"l\">                <span class=\"s\">\"properties\"<\/span>: <span class=\"n\">params<\/span>,<\/span>\n<span class=\"l\">                <span class=\"s\">\"required\"<\/span>: <span class=\"nb\">list<\/span>(<span class=\"n\">params<\/span>),<\/span>\n<span class=\"l\">                <span class=\"s\">\"additionalProperties\"<\/span>: <span class=\"kc\">False<\/span>,<\/span>\n<span class=\"l\">            },<\/span>\n<span class=\"l\">        },<\/span>\n<span class=\"l\">    }<\/span>\n<span class=\"l\">    <span class=\"k\">return<\/span> <span class=\"n\">func<\/span><\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"nd\">@tool<\/span><\/span>\n<span class=\"l\"><span class=\"k\">def<\/span> <span class=\"nf\">get_order_status<\/span>(<span class=\"n\">order_id<\/span>: <span class=\"nb\">str<\/span>):<\/span>\n<span class=\"l\">    <span class=\"s\">\"\"\"Look up the delivery status of a customer order by its ID, e.g. 'A102'.\"\"\"<\/span><\/span>\n<span class=\"l\">    <span class=\"n\">orders<\/span> = {<span class=\"s\">\"A101\"<\/span>: <span class=\"s\">\"delivered\"<\/span>, <span class=\"s\">\"A102\"<\/span>: <span class=\"s\">\"out for delivery\"<\/span>}<\/span>\n<span class=\"l\">    <span class=\"k\">return<\/span> <span class=\"n\">orders<\/span>.<span class=\"n\">get<\/span>(<span class=\"n\">order_id<\/span>.<span class=\"n\">upper<\/span>(), <span class=\"s\">\"not found\"<\/span>)<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"nd\">@tool<\/span><\/span>\n<span class=\"l\"><span class=\"k\">def<\/span> <span class=\"nf\">convert_currency<\/span>(<span class=\"n\">amount<\/span>: <span class=\"nb\">float<\/span>, <span class=\"n\">rate<\/span>: <span class=\"nb\">float<\/span>):<\/span>\n<span class=\"l\">    <span class=\"s\">\"\"\"Convert an amount using an exchange rate. Returns the converted value.\"\"\"<\/span><\/span>\n<span class=\"l\">    <span class=\"k\">return<\/span> <span class=\"nb\">round<\/span>(<span class=\"n\">amount<\/span> * <span class=\"n\">rate<\/span>, <span class=\"m\">2<\/span>)<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">def<\/span> <span class=\"nf\">call_tool<\/span>(<span class=\"n\">name<\/span>, <span class=\"n\">arguments_json<\/span>):<\/span>\n<span class=\"l\">    <span class=\"s\">\"\"\"What the agent runtime does when the model asks for a tool.\"\"\"<\/span><\/span>\n<span class=\"l\">    <span class=\"k\">if<\/span> <span class=\"n\">name<\/span> <span class=\"k\">not<\/span> <span class=\"k\">in<\/span> <span class=\"n\">REGISTRY<\/span>:<\/span>\n<span class=\"l\">        <span class=\"k\">return<\/span> <span class=\"s\">f\"error: unknown tool '{<\/span><span class=\"n\">name<\/span><span class=\"s\">}'\"<\/span><\/span>\n<span class=\"l\">    <span class=\"n\">args<\/span> = <span class=\"n\">json<\/span>.<span class=\"n\">loads<\/span>(<span class=\"n\">arguments_json<\/span>)<\/span>\n<span class=\"l\">    <span class=\"k\">return<\/span> <span class=\"n\">REGISTRY<\/span>[<span class=\"n\">name<\/span>][<span class=\"s\">\"function\"<\/span>](**<span class=\"n\">args<\/span>)<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"nb\">print<\/span>(<span class=\"n\">json<\/span>.<span class=\"n\">dumps<\/span>(<span class=\"n\">REGISTRY<\/span>[<span class=\"s\">\"get_order_status\"<\/span>][<span class=\"s\">\"schema\"<\/span>], <span class=\"n\">indent<\/span>=<span class=\"m\">2<\/span>))<\/span>\n<span class=\"l\"><span class=\"nb\">print<\/span>(<span class=\"n\">call_tool<\/span>(<span class=\"s\">\"get_order_status\"<\/span>, <span class=\"s\">'{\"order_id\": \"a102\"}'<\/span>))<\/span>\n<span class=\"l\"><span class=\"nb\">print<\/span>(<span class=\"n\">call_tool<\/span>(<span class=\"s\">\"convert_currency\"<\/span>, <span class=\"s\">'{\"amount\": 25, \"rate\": 83.2}'<\/span>))<\/span>\n<span class=\"l\"><span class=\"nb\">print<\/span>(<span class=\"n\">call_tool<\/span>(<span class=\"s\">\"delete_database\"<\/span>, <span class=\"s\">'{}'<\/span>))<\/span><\/code><\/pre>\n<div class=\"dd-vsc-term\"><div class=\"dd-term-head\"><span>TERMINAL<\/span><span>OUTPUT<\/span><span>PROBLEMS<\/span><span class=\"dd-term-note\">Real output<\/span><\/div><pre><code><span class=\"gp\">$<\/span> python tools.py\n{\n  \"type\": \"function\",\n  \"name\": \"get_order_status\",\n  \"description\": \"Look up the delivery status of a customer order by its ID, e.g. 'A102'.\",\n  \"parameters\": {\n    \"type\": \"object\",\n    \"properties\": {\n      \"order_id\": {\n        \"type\": \"string\"\n      }\n    },\n    \"required\": [\n      \"order_id\"\n    ],\n    \"additionalProperties\": false\n  }\n}\nout for delivery\n2080.0\nerror: unknown tool 'delete_database'<\/code><\/pre><\/div>\n<div class=\"dd-vsc-status\"><span>\u2387 main<\/span><span>Ln 50, Col 1<\/span><span class=\"sp\"><\/span><span>Spaces: 4<\/span><span>UTF-8<\/span><span>LF<\/span><span>Python<\/span><\/div>\n<\/div>\n<div class=\"dd-vsc-pane p2\">\n<div class=\"dd-vsc-crumbs\">agentic-ai-part2 \u203a tools.mjs<\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\"><span class=\"c\">\/\/ The same tool registry in Node.js: each tool = schema + function.<\/span><\/span>\n<span class=\"l\"><span class=\"k\">const<\/span> <span class=\"n\">REGISTRY<\/span> = {};<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">function<\/span> <span class=\"n\">tool<\/span>({ <span class=\"n\">name<\/span>, <span class=\"n\">description<\/span>, <span class=\"n\">properties<\/span> }, <span class=\"n\">fn<\/span>) {<\/span>\n<span class=\"l\">  <span class=\"n\">REGISTRY<\/span>[<span class=\"n\">name<\/span>] = {<\/span>\n<span class=\"l\">    <span class=\"n\">fn<\/span>,<\/span>\n<span class=\"l\">    <span class=\"n\">schema<\/span>: {<\/span>\n<span class=\"l\">      <span class=\"n\">type<\/span>: <span class=\"s\">\"function\"<\/span>,<\/span>\n<span class=\"l\">      <span class=\"n\">name<\/span>,<\/span>\n<span class=\"l\">      <span class=\"n\">description<\/span>,<\/span>\n<span class=\"l\">      <span class=\"n\">parameters<\/span>: {<\/span>\n<span class=\"l\">        <span class=\"n\">type<\/span>: <span class=\"s\">\"object\"<\/span>,<\/span>\n<span class=\"l\">        <span class=\"n\">properties<\/span>,<\/span>\n<span class=\"l\">        <span class=\"n\">required<\/span>: <span class=\"nb\">Object<\/span>.<span class=\"n\">keys<\/span>(<span class=\"n\">properties<\/span>),<\/span>\n<span class=\"l\">        <span class=\"n\">additionalProperties<\/span>: <span class=\"kc\">false<\/span>,<\/span>\n<span class=\"l\">      },<\/span>\n<span class=\"l\">    },<\/span>\n<span class=\"l\">  };<\/span>\n<span class=\"l\">}<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"n\">tool<\/span>(<\/span>\n<span class=\"l\">  {<\/span>\n<span class=\"l\">    <span class=\"n\">name<\/span>: <span class=\"s\">\"get_order_status\"<\/span>,<\/span>\n<span class=\"l\">    <span class=\"n\">description<\/span>: <span class=\"s\">\"Look up the delivery status of a customer order by its ID, e.g. 'A102'.\"<\/span>,<\/span>\n<span class=\"l\">    <span class=\"n\">properties<\/span>: { <span class=\"n\">order_id<\/span>: { <span class=\"n\">type<\/span>: <span class=\"s\">\"string\"<\/span> } },<\/span>\n<span class=\"l\">  },<\/span>\n<span class=\"l\">  ({ <span class=\"n\">order_id<\/span> }) =&gt; {<\/span>\n<span class=\"l\">    <span class=\"k\">const<\/span> <span class=\"n\">orders<\/span> = { <span class=\"n\">A101<\/span>: <span class=\"s\">\"delivered\"<\/span>, <span class=\"n\">A102<\/span>: <span class=\"s\">\"out for delivery\"<\/span> };<\/span>\n<span class=\"l\">    <span class=\"k\">return<\/span> <span class=\"n\">orders<\/span>[<span class=\"n\">order_id<\/span>.<span class=\"n\">toUpperCase<\/span>()] ?? <span class=\"s\">\"not found\"<\/span>;<\/span>\n<span class=\"l\">  }<\/span>\n<span class=\"l\">);<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"n\">tool<\/span>(<\/span>\n<span class=\"l\">  {<\/span>\n<span class=\"l\">    <span class=\"n\">name<\/span>: <span class=\"s\">\"convert_currency\"<\/span>,<\/span>\n<span class=\"l\">    <span class=\"n\">description<\/span>: <span class=\"s\">\"Convert an amount using an exchange rate. Returns the converted value.\"<\/span>,<\/span>\n<span class=\"l\">    <span class=\"n\">properties<\/span>: { <span class=\"n\">amount<\/span>: { <span class=\"n\">type<\/span>: <span class=\"s\">\"number\"<\/span> }, <span class=\"n\">rate<\/span>: { <span class=\"n\">type<\/span>: <span class=\"s\">\"number\"<\/span> } },<\/span>\n<span class=\"l\">  },<\/span>\n<span class=\"l\">  ({ <span class=\"n\">amount<\/span>, <span class=\"n\">rate<\/span> }) =&gt; <span class=\"nb\">Math<\/span>.<span class=\"n\">round<\/span>(<span class=\"n\">amount<\/span> * <span class=\"n\">rate<\/span> * <span class=\"m\">100<\/span>) \/ <span class=\"m\">100<\/span><\/span>\n<span class=\"l\">);<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">function<\/span> <span class=\"n\">callTool<\/span>(<span class=\"n\">name<\/span>, <span class=\"n\">argumentsJson<\/span>) {<\/span>\n<span class=\"l\">  <span class=\"k\">if<\/span> (!<span class=\"n\">REGISTRY<\/span>[<span class=\"n\">name<\/span>]) <span class=\"k\">return<\/span> <span class=\"s\">`error: unknown tool '${<\/span><span class=\"n\">name<\/span><span class=\"s\">}'`<\/span>;<\/span>\n<span class=\"l\">  <span class=\"k\">return<\/span> <span class=\"n\">REGISTRY<\/span>[<span class=\"n\">name<\/span>].<span class=\"n\">fn<\/span>(<span class=\"nb\">JSON<\/span>.<span class=\"n\">parse<\/span>(<span class=\"n\">argumentsJson<\/span>));<\/span>\n<span class=\"l\">}<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"nb\">console<\/span>.<span class=\"n\">log<\/span>(<span class=\"nb\">JSON<\/span>.<span class=\"n\">stringify<\/span>(<span class=\"n\">REGISTRY<\/span>.<span class=\"n\">get_order_status<\/span>.<span class=\"n\">schema<\/span>, <span class=\"kc\">null<\/span>, <span class=\"m\">2<\/span>));<\/span>\n<span class=\"l\"><span class=\"nb\">console<\/span>.<span class=\"n\">log<\/span>(<span class=\"n\">callTool<\/span>(<span class=\"s\">\"get_order_status\"<\/span>, <span class=\"s\">'{\"order_id\": \"a102\"}'<\/span>));<\/span>\n<span class=\"l\"><span class=\"nb\">console<\/span>.<span class=\"n\">log<\/span>(<span class=\"n\">callTool<\/span>(<span class=\"s\">\"convert_currency\"<\/span>, <span class=\"s\">'{\"amount\": 25, \"rate\": 83.2}'<\/span>));<\/span>\n<span class=\"l\"><span class=\"nb\">console<\/span>.<span class=\"n\">log<\/span>(<span class=\"n\">callTool<\/span>(<span class=\"s\">\"delete_database\"<\/span>, <span class=\"s\">\"{}\"<\/span>));<\/span><\/code><\/pre>\n<div class=\"dd-vsc-term\"><div class=\"dd-term-head\"><span>TERMINAL<\/span><span>OUTPUT<\/span><span>PROBLEMS<\/span><span class=\"dd-term-note\">Real output<\/span><\/div><pre><code><span class=\"gp\">$<\/span> node tools.mjs\n{\n  \"type\": \"function\",\n  \"name\": \"get_order_status\",\n  \"description\": \"Look up the delivery status of a customer order by its ID, e.g. 'A102'.\",\n  \"parameters\": {\n    \"type\": \"object\",\n    \"properties\": {\n      \"order_id\": {\n        \"type\": \"string\"\n      }\n    },\n    \"required\": [\n      \"order_id\"\n    ],\n    \"additionalProperties\": false\n  }\n}\nout for delivery\n2080\nerror: unknown tool 'delete_database'<\/code><\/pre><\/div>\n<div class=\"dd-vsc-status\"><span>\u2387 main<\/span><span>Ln 50, Col 1<\/span><span class=\"sp\"><\/span><span>Spaces: 2<\/span><span>UTF-8<\/span><span>LF<\/span><span>JavaScript<\/span><\/div>\n<\/div>\n<\/div>\n<p class=\"dd-vsc-hint\">Switch between Python and Node.js with the tabs, then press <strong>\u25b6 Run<\/strong> to see the real output.<\/p>\n<p>Notice the last line: when the model asks for a tool that does not exist (<code>delete_database<\/code>), the runtime returns an error message instead of crashing. That error goes back into memory so the model can correct itself. This is the format OpenAI&#8217;s Responses API expects for function tools. See the official <a href=\"https:\/\/developers.openai.com\/api\/docs\/guides\/function-calling\" target=\"_blank\" rel=\"noopener\">function calling guide<\/a> for all options.<\/p>\n<div class=\"dd-warn\"><strong>Safety first:<\/strong> a tool can do anything your code can do. Start with <em>read-only<\/em> tools (lookups, searches). For tools that change things (refunds, emails, deletes) add checks inside the function and, later, human approval. We cover this fully in Part 16 and Part 22.<\/div>\n\n<h2 id=\"instructions\">Part 3 of the agent: instructions (the job description)<\/h2>\n<p>The <strong>instructions<\/strong> (also called the system prompt) are the first thing the model reads every turn. They are your main way to shape behaviour without changing code. A messy paragraph gives messy results, so use a fixed structure with six short sections:<\/p>\n<div class=\"dd-cards\">\n<div class=\"dd-card\"><b>Role<\/b>Who the agent is and who it serves.<\/div>\n<div class=\"dd-card\"><b>Goal<\/b>What &#8220;done&#8221; looks like.<\/div>\n<div class=\"dd-card\"><b>Rules<\/b>What it must always or never do.<\/div>\n<div class=\"dd-card\"><b>Tool tips<\/b>When to use each tool.<\/div>\n<div class=\"dd-card\"><b>Output format<\/b>Length, tone, structure.<\/div>\n<div class=\"dd-card\"><b>When to stop<\/b>So it does not loop forever.<\/div>\n<\/div>\n<div class=\"dd-vsc\" role=\"group\" aria-label=\"Code editor: instructions.py\">\n<input type=\"radio\" name=\"v2\" id=\"v2f1\" class=\"f1\" checked>\n<input type=\"checkbox\" id=\"v2r1\" class=\"r1\">\n<div class=\"dd-vsc-bar\"><span class=\"dd-dots\"><i><\/i><i><\/i><i><\/i><\/span><span class=\"dd-vsc-title\">instructions.py \u2014 agentic-ai-part2 \u2014 Visual Studio Code<\/span><\/div>\n<div class=\"dd-vsc-tabs\">\n<label for=\"v2f1\" class=\"t1\"><b class=\"ic py\">PY<\/b>instructions.py<\/label>\n<label for=\"v2r1\" class=\"run run1\" title=\"Show the output in the terminal\">\u25b6 Run<\/label>\n<\/div>\n<div class=\"dd-vsc-pane p1\">\n<div class=\"dd-vsc-crumbs\">agentic-ai-part2 \u203a instructions.py<\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\"><span class=\"c\"># Build clear agent instructions from six simple sections.<\/span><\/span>\n<span class=\"l\"><span class=\"k\">def<\/span> <span class=\"nf\">build_instructions<\/span>(<span class=\"n\">role<\/span>, <span class=\"n\">goal<\/span>, <span class=\"n\">rules<\/span>, <span class=\"n\">tool_tips<\/span>, <span class=\"n\">output_format<\/span>, <span class=\"n\">stop_when<\/span>):<\/span>\n<span class=\"l\">    <span class=\"n\">lines<\/span> = [<span class=\"s\">f\"# Role\\n{<\/span><span class=\"n\">role<\/span><span class=\"s\">}\"<\/span>, <span class=\"s\">f\"# Goal\\n{<\/span><span class=\"n\">goal<\/span><span class=\"s\">}\"<\/span>, <span class=\"s\">\"# Rules\"<\/span>]<\/span>\n<span class=\"l\">    <span class=\"n\">lines<\/span> += [<span class=\"s\">f\"- {<\/span><span class=\"n\">r<\/span><span class=\"s\">}\"<\/span> <span class=\"k\">for<\/span> <span class=\"n\">r<\/span> <span class=\"k\">in<\/span> <span class=\"n\">rules<\/span>]<\/span>\n<span class=\"l\">    <span class=\"n\">lines<\/span> += [<span class=\"s\">\"# How to use tools\"<\/span>] + [<span class=\"s\">f\"- {<\/span><span class=\"n\">t<\/span><span class=\"s\">}\"<\/span> <span class=\"k\">for<\/span> <span class=\"n\">t<\/span> <span class=\"k\">in<\/span> <span class=\"n\">tool_tips<\/span>]<\/span>\n<span class=\"l\">    <span class=\"n\">lines<\/span> += [<span class=\"s\">f\"# Output format\\n{<\/span><span class=\"n\">output_format<\/span><span class=\"s\">}\"<\/span>, <span class=\"s\">f\"# When to stop\\n{<\/span><span class=\"n\">stop_when<\/span><span class=\"s\">}\"<\/span>]<\/span>\n<span class=\"l\">    <span class=\"k\">return<\/span> <span class=\"s\">\"\\n\"<\/span>.<span class=\"n\">join<\/span>(<span class=\"n\">lines<\/span>)<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"n\">INSTRUCTIONS<\/span> = <span class=\"n\">build_instructions<\/span>(<\/span>\n<span class=\"l\">    <span class=\"n\">role<\/span>=<span class=\"s\">\"You are ShopHelp, a friendly support agent for an Indian online store.\"<\/span>,<\/span>\n<span class=\"l\">    <span class=\"n\">goal<\/span>=<span class=\"s\">\"Solve the customer's order or refund question in as few steps as possible.\"<\/span>,<\/span>\n<span class=\"l\">    <span class=\"n\">rules<\/span>=[<\/span>\n<span class=\"l\">        <span class=\"s\">\"Never guess order details. Always check with a tool first.\"<\/span>,<\/span>\n<span class=\"l\">        <span class=\"s\">\"Never promise a refund. Explain the policy instead.\"<\/span>,<\/span>\n<span class=\"l\">        <span class=\"s\">\"If the customer is angry or asks for a human, say you will connect them.\"<\/span>,<\/span>\n<span class=\"l\">    ],<\/span>\n<span class=\"l\">    <span class=\"n\">tool_tips<\/span>=[<\/span>\n<span class=\"l\">        <span class=\"s\">\"get_order_status: use when the customer gives an order ID.\"<\/span>,<\/span>\n<span class=\"l\">        <span class=\"s\">\"get_refund_policy: use for any return or refund question.\"<\/span>,<\/span>\n<span class=\"l\">    ],<\/span>\n<span class=\"l\">    <span class=\"n\">output_format<\/span>=<span class=\"s\">\"Two or three short sentences in simple English.\"<\/span>,<\/span>\n<span class=\"l\">    <span class=\"n\">stop_when<\/span>=<span class=\"s\">\"Stop as soon as the question is answered, or after 5 tool calls.\"<\/span>,<\/span>\n<span class=\"l\">)<\/span>\n<span class=\"l\"><span class=\"nb\">print<\/span>(<span class=\"n\">INSTRUCTIONS<\/span>)<\/span>\n<span class=\"l\"><span class=\"nb\">print<\/span>(<span class=\"s\">f\"\\n({<\/span><span class=\"nb\">len<\/span>(<span class=\"n\">INSTRUCTIONS<\/span>.<span class=\"n\">split<\/span>())<span class=\"s\">} words)\"<\/span>)<\/span><\/code><\/pre>\n<div class=\"dd-vsc-term\"><div class=\"dd-term-head\"><span>TERMINAL<\/span><span>OUTPUT<\/span><span>PROBLEMS<\/span><span class=\"dd-term-note\">Real output<\/span><\/div><pre><code><span class=\"gp\">$<\/span> python instructions.py\n# Role\nYou are ShopHelp, a friendly support agent for an Indian online store.\n# Goal\nSolve the customer's order or refund question in as few steps as possible.\n# Rules\n- Never guess order details. Always check with a tool first.\n- Never promise a refund. Explain the policy instead.\n- If the customer is angry or asks for a human, say you will connect them.\n# How to use tools\n- get_order_status: use when the customer gives an order ID.\n- get_refund_policy: use for any return or refund question.\n# Output format\nTwo or three short sentences in simple English.\n# When to stop\nStop as soon as the question is answered, or after 5 tool calls.\n\n(119 words)<\/code><\/pre><\/div>\n<div class=\"dd-vsc-status\"><span>\u2387 main<\/span><span>Ln 25, Col 1<\/span><span class=\"sp\"><\/span><span>Spaces: 4<\/span><span>UTF-8<\/span><span>LF<\/span><span>Python<\/span><\/div>\n<\/div>\n<\/div>\n<p class=\"dd-vsc-hint\">Press <strong>\u25b6 Run<\/strong> to print the finished instructions.<\/p>\n<p>About 120 words is plenty for a focused agent. Using headings and bullet points makes the instructions easy for the model to follow and easy for you to edit. If you notice the agent breaking a rule, first check whether the rule is actually written clearly. We go deeper into this in Part 6, &#8220;Writing Good Agent Instructions and Tool Descriptions&#8221;.<\/p>\n\n<h2 id=\"memory\">Part 4 of the agent: memory (the notebook)<\/h2>\n<p>An LLM has no memory between API calls. Every time you call it, it only knows what you send in that request. So &#8220;memory&#8221; in an agent is really <strong>data your code stores and sends back to the model<\/strong>. There are three useful kinds:<\/p>\n\n<figure>\n<svg viewBox=\"0 0 900 300\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" role=\"img\" aria-label=\"Diagram comparing three kinds of agent memory: short-term chat history, scratchpad for the current task, and long-term memory saved to a file or database\">\n<rect width=\"900\" height=\"300\" rx=\"16\" fill=\"#0f1430\"\/>\n<g font-family=\"Segoe UI,Arial,sans-serif\">\n<rect x=\"30\" y=\"40\" width=\"260\" height=\"220\" rx=\"14\" fill=\"#151b3b\" stroke=\"#3178c6\" stroke-width=\"2\"\/>\n<text x=\"160\" y=\"80\" text-anchor=\"middle\" font-size=\"21\" font-weight=\"800\" fill=\"#fff\">Short-term<\/text>\n<text x=\"160\" y=\"106\" text-anchor=\"middle\" font-size=\"14\" fill=\"#9fb2ff\">the conversation<\/text>\n<text x=\"160\" y=\"150\" text-anchor=\"middle\" font-size=\"15\" fill=\"#c9d1ff\">user + model messages<\/text>\n<text x=\"160\" y=\"174\" text-anchor=\"middle\" font-size=\"15\" fill=\"#c9d1ff\">tool calls and results<\/text>\n<text x=\"160\" y=\"222\" text-anchor=\"middle\" font-size=\"14\" fill=\"#7f89b5\">lives: one chat session<\/text>\n<rect x=\"320\" y=\"40\" width=\"260\" height=\"220\" rx=\"14\" fill=\"#151b3b\" stroke=\"#7c5cff\" stroke-width=\"2\"\/>\n<text x=\"450\" y=\"80\" text-anchor=\"middle\" font-size=\"21\" font-weight=\"800\" fill=\"#fff\">Scratchpad<\/text>\n<text x=\"450\" y=\"106\" text-anchor=\"middle\" font-size=\"14\" fill=\"#b9a8ff\">working notes<\/text>\n<text x=\"450\" y=\"150\" text-anchor=\"middle\" font-size=\"15\" fill=\"#c9d1ff\">plan, progress, findings<\/text>\n<text x=\"450\" y=\"174\" text-anchor=\"middle\" font-size=\"15\" fill=\"#c9d1ff\">for the current task<\/text>\n<text x=\"450\" y=\"222\" text-anchor=\"middle\" font-size=\"14\" fill=\"#7f89b5\">lives: one task<\/text>\n<rect x=\"610\" y=\"40\" width=\"260\" height=\"220\" rx=\"14\" fill=\"#151b3b\" stroke=\"#61dafb\" stroke-width=\"2\"\/>\n<text x=\"740\" y=\"80\" text-anchor=\"middle\" font-size=\"21\" font-weight=\"800\" fill=\"#fff\">Long-term<\/text>\n<text x=\"740\" y=\"106\" text-anchor=\"middle\" font-size=\"14\" fill=\"#61dafb\">saved facts<\/text>\n<text x=\"740\" y=\"150\" text-anchor=\"middle\" font-size=\"15\" fill=\"#c9d1ff\">user preferences, profile<\/text>\n<text x=\"740\" y=\"174\" text-anchor=\"middle\" font-size=\"15\" fill=\"#c9d1ff\">file, database, vector DB<\/text>\n<text x=\"740\" y=\"222\" text-anchor=\"middle\" font-size=\"14\" fill=\"#7f89b5\">lives: across restarts<\/text>\n<\/g>\n<\/svg>\n<figcaption>Three kinds of agent memory and how long each one lives.<\/figcaption>\n<\/figure>\n\n<p>Here is a small, runnable version of all three. Short-term memory keeps only the newest messages so it never overflows the context window. Long-term memory writes to a JSON file, so a brand-new object (like after a restart) still remembers the user&#8217;s city.<\/p>\n<div class=\"dd-vsc\" role=\"group\" aria-label=\"Code editor: memory.py\">\n<input type=\"radio\" name=\"v3\" id=\"v3f1\" class=\"f1\" checked>\n<input type=\"checkbox\" id=\"v3r1\" class=\"r1\">\n<div class=\"dd-vsc-bar\"><span class=\"dd-dots\"><i><\/i><i><\/i><i><\/i><\/span><span class=\"dd-vsc-title\">memory.py \u2014 agentic-ai-part2 \u2014 Visual Studio Code<\/span><\/div>\n<div class=\"dd-vsc-tabs\">\n<label for=\"v3f1\" class=\"t1\"><b class=\"ic py\">PY<\/b>memory.py<\/label>\n<label for=\"v3r1\" class=\"run run1\" title=\"Show the output in the terminal\">\u25b6 Run<\/label>\n<\/div>\n<div class=\"dd-vsc-pane p1\">\n<div class=\"dd-vsc-crumbs\">agentic-ai-part2 \u203a memory.py<\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\"><span class=\"c\"># Three kinds of agent memory in one small file.<\/span><\/span>\n<span class=\"l\"><span class=\"kn\">import<\/span> <span class=\"n\">json<\/span><\/span>\n<span class=\"l\"><span class=\"kn\">from<\/span> <span class=\"n\">pathlib<\/span> <span class=\"kn\">import<\/span> <span class=\"n\">Path<\/span><\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">class<\/span> <span class=\"nc\">ShortTermMemory<\/span>:<\/span>\n<span class=\"l\">    <span class=\"s\">\"\"\"The conversation so far. Trimmed so it fits the context window.\"\"\"<\/span><\/span>\n<span class=\"l\">    <span class=\"k\">def<\/span> <span class=\"nf\">__init__<\/span>(<span class=\"nb\">self<\/span>, <span class=\"n\">max_messages<\/span>=<span class=\"m\">6<\/span>):<\/span>\n<span class=\"l\">        <span class=\"nb\">self<\/span>.<span class=\"n\">messages<\/span> = []<\/span>\n<span class=\"l\">        <span class=\"nb\">self<\/span>.<span class=\"n\">max_messages<\/span> = <span class=\"n\">max_messages<\/span><\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\">    <span class=\"k\">def<\/span> <span class=\"nf\">add<\/span>(<span class=\"nb\">self<\/span>, <span class=\"n\">role<\/span>, <span class=\"n\">content<\/span>):<\/span>\n<span class=\"l\">        <span class=\"nb\">self<\/span>.<span class=\"n\">messages<\/span>.<span class=\"n\">append<\/span>({<span class=\"s\">\"role\"<\/span>: <span class=\"n\">role<\/span>, <span class=\"s\">\"content\"<\/span>: <span class=\"n\">content<\/span>})<\/span>\n<span class=\"l\">        <span class=\"k\">if<\/span> <span class=\"nb\">len<\/span>(<span class=\"nb\">self<\/span>.<span class=\"n\">messages<\/span>) &gt; <span class=\"nb\">self<\/span>.<span class=\"n\">max_messages<\/span>:<\/span>\n<span class=\"l\">            <span class=\"nb\">self<\/span>.<span class=\"n\">messages<\/span> = <span class=\"nb\">self<\/span>.<span class=\"n\">messages<\/span>[-<span class=\"nb\">self<\/span>.<span class=\"n\">max_messages<\/span>:]  <span class=\"c\"># keep newest<\/span><\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">class<\/span> <span class=\"nc\">Scratchpad<\/span>:<\/span>\n<span class=\"l\">    <span class=\"s\">\"\"\"Working notes for the current task only. Cleared when the task ends.\"\"\"<\/span><\/span>\n<span class=\"l\">    <span class=\"k\">def<\/span> <span class=\"nf\">__init__<\/span>(<span class=\"nb\">self<\/span>):<\/span>\n<span class=\"l\">        <span class=\"nb\">self<\/span>.<span class=\"n\">notes<\/span> = {}<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">class<\/span> <span class=\"nc\">LongTermMemory<\/span>:<\/span>\n<span class=\"l\">    <span class=\"s\">\"\"\"Facts that survive restarts, saved in a JSON file.\"\"\"<\/span><\/span>\n<span class=\"l\">    <span class=\"k\">def<\/span> <span class=\"nf\">__init__<\/span>(<span class=\"nb\">self<\/span>, <span class=\"n\">path<\/span>=<span class=\"s\">\"memory.json\"<\/span>):<\/span>\n<span class=\"l\">        <span class=\"nb\">self<\/span>.<span class=\"n\">path<\/span> = <span class=\"n\">Path<\/span>(<span class=\"n\">path<\/span>)<\/span>\n<span class=\"l\">        <span class=\"nb\">self<\/span>.<span class=\"n\">facts<\/span> = <span class=\"n\">json<\/span>.<span class=\"n\">loads<\/span>(<span class=\"nb\">self<\/span>.<span class=\"n\">path<\/span>.<span class=\"n\">read_text<\/span>()) <span class=\"k\">if<\/span> <span class=\"nb\">self<\/span>.<span class=\"n\">path<\/span>.<span class=\"n\">exists<\/span>() <span class=\"k\">else<\/span> {}<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\">    <span class=\"k\">def<\/span> <span class=\"nf\">remember<\/span>(<span class=\"nb\">self<\/span>, <span class=\"n\">key<\/span>, <span class=\"n\">value<\/span>):<\/span>\n<span class=\"l\">        <span class=\"nb\">self<\/span>.<span class=\"n\">facts<\/span>[<span class=\"n\">key<\/span>] = <span class=\"n\">value<\/span><\/span>\n<span class=\"l\">        <span class=\"nb\">self<\/span>.<span class=\"n\">path<\/span>.<span class=\"n\">write_text<\/span>(<span class=\"n\">json<\/span>.<span class=\"n\">dumps<\/span>(<span class=\"nb\">self<\/span>.<span class=\"n\">facts<\/span>, <span class=\"n\">indent<\/span>=<span class=\"m\">2<\/span>))<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\">    <span class=\"k\">def<\/span> <span class=\"nf\">recall<\/span>(<span class=\"nb\">self<\/span>, <span class=\"n\">key<\/span>):<\/span>\n<span class=\"l\">        <span class=\"k\">return<\/span> <span class=\"nb\">self<\/span>.<span class=\"n\">facts<\/span>.<span class=\"n\">get<\/span>(<span class=\"n\">key<\/span>, <span class=\"s\">\"I don't know yet\"<\/span>)<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"n\">short<\/span> = <span class=\"n\">ShortTermMemory<\/span>(<span class=\"n\">max_messages<\/span>=<span class=\"m\">4<\/span>)<\/span>\n<span class=\"l\"><span class=\"k\">for<\/span> <span class=\"n\">i<\/span> <span class=\"k\">in<\/span> <span class=\"nb\">range<\/span>(<span class=\"m\">1<\/span>, <span class=\"m\">7<\/span>):<\/span>\n<span class=\"l\">    <span class=\"n\">short<\/span>.<span class=\"n\">add<\/span>(<span class=\"s\">\"user\"<\/span>, <span class=\"s\">f\"message {<\/span><span class=\"n\">i<\/span><span class=\"s\">}\"<\/span>)<\/span>\n<span class=\"l\"><span class=\"nb\">print<\/span>(<span class=\"s\">\"Short-term keeps:\"<\/span>, [<span class=\"n\">m<\/span>[<span class=\"s\">\"content\"<\/span>] <span class=\"k\">for<\/span> <span class=\"n\">m<\/span> <span class=\"k\">in<\/span> <span class=\"n\">short<\/span>.<span class=\"n\">messages<\/span>])<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"n\">pad<\/span> = <span class=\"n\">Scratchpad<\/span>()<\/span>\n<span class=\"l\"><span class=\"n\">pad<\/span>.<span class=\"n\">notes<\/span>[<span class=\"s\">\"order_A102\"<\/span>] = <span class=\"s\">\"out for delivery\"<\/span><\/span>\n<span class=\"l\"><span class=\"nb\">print<\/span>(<span class=\"s\">\"Scratchpad:\"<\/span>, <span class=\"n\">pad<\/span>.<span class=\"n\">notes<\/span>)<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"n\">long_term<\/span> = <span class=\"n\">LongTermMemory<\/span>()<\/span>\n<span class=\"l\"><span class=\"n\">long_term<\/span>.<span class=\"n\">remember<\/span>(<span class=\"s\">\"user_city\"<\/span>, <span class=\"s\">\"Pune\"<\/span>)<\/span>\n<span class=\"l\"><span class=\"nb\">print<\/span>(<span class=\"s\">\"Long-term recall:\"<\/span>, <span class=\"n\">LongTermMemory<\/span>().<span class=\"n\">recall<\/span>(<span class=\"s\">\"user_city\"<\/span>))  <span class=\"c\"># new object, same file<\/span><\/span><\/code><\/pre>\n<div class=\"dd-vsc-term\"><div class=\"dd-term-head\"><span>TERMINAL<\/span><span>OUTPUT<\/span><span>PROBLEMS<\/span><span class=\"dd-term-note\">Real output<\/span><\/div><pre><code><span class=\"gp\">$<\/span> python memory.py\nShort-term keeps: ['message 3', 'message 4', 'message 5', 'message 6']\nScratchpad: {'order_A102': 'out for delivery'}\nLong-term recall: Pune<\/code><\/pre><\/div>\n<div class=\"dd-vsc-status\"><span>\u2387 main<\/span><span>Ln 45, Col 1<\/span><span class=\"sp\"><\/span><span>Spaces: 4<\/span><span>UTF-8<\/span><span>LF<\/span><span>Python<\/span><\/div>\n<\/div>\n<\/div>\n<p class=\"dd-vsc-hint\">Press <strong>\u25b6 Run<\/strong> to see which messages survive trimming and what the long-term memory recalls.<\/p>\n<div class=\"dd-note\"><strong>Real-world note:<\/strong> cutting off old messages is the simplest trimming method. Production agents often <em>summarise<\/em> old messages instead, and store long-term facts in a database or a vector store so they can search them. Part 7 of this series is all about agent memory.<\/div>\n\n<h2 id=\"runtime\">The runtime loop that connects everything<\/h2>\n<p>The four parts sit still until something drives them. That &#8220;something&#8221; is the <strong>runtime<\/strong>: the loop in your code. You met it in Part 1. Here is what it does on every step:<\/p>\n<ol>\n<li>Send <strong>instructions + memory + tool schemas<\/strong> to the model.<\/li>\n<li>If the model returns a final answer, stop and return it.<\/li>\n<li>If it returns a tool call, check the tool exists, run it, and catch errors.<\/li>\n<li>Add the tool result to memory.<\/li>\n<li>Repeat, but never more than <code>max_steps<\/code> times.<\/li>\n<\/ol>\n<p>The step limit, the unknown-tool check and the logging are not &#8220;extra&#8221;. They are what keeps an agent safe and debuggable. Anthropic&#8217;s article <a href=\"https:\/\/www.anthropic.com\/engineering\/building-effective-agents\" target=\"_blank\" rel=\"noopener\">Building effective agents<\/a> gives the same advice: keep the loop simple and make every step visible.<\/p>\n<\/div>\n\n\n\n<div class=\"dd-post\">\n<h2 id=\"together\">Build an agent with all four parts<\/h2>\n<p>Now let&#8217;s put the anatomy together in one small <code>Agent<\/code> class. To keep it free and runnable, the &#8220;model&#8221; is a fake function with a few <code>if<\/code> rules. The important thing is its <strong>signature<\/strong>: it receives exactly what a real LLM receives (instructions, memory, tools) and returns either a tool call or an answer. In the next section we swap it for a real model without touching the rest.<\/p>\n<div class=\"dd-vsc\" role=\"group\" aria-label=\"Code editor: anatomy_agent.py, anatomy-agent.mjs\">\n<input type=\"radio\" name=\"v4\" id=\"v4f1\" class=\"f1\" checked>\n<input type=\"radio\" name=\"v4\" id=\"v4f2\" class=\"f2\">\n<input type=\"checkbox\" id=\"v4r1\" class=\"r1\">\n<input type=\"checkbox\" id=\"v4r2\" class=\"r2\">\n<div class=\"dd-vsc-bar\"><span class=\"dd-dots\"><i><\/i><i><\/i><i><\/i><\/span><span class=\"dd-vsc-title\">2 files \u2014 agentic-ai-part2 \u2014 Visual Studio Code<\/span><\/div>\n<div class=\"dd-vsc-tabs\">\n<label for=\"v4f1\" class=\"t1\"><b class=\"ic py\">PY<\/b>anatomy_agent.py<\/label>\n<label for=\"v4f2\" class=\"t2\"><b class=\"ic js\">JS<\/b>anatomy-agent.mjs<\/label>\n<label for=\"v4r1\" class=\"run run1\" title=\"Show the output in the terminal\">\u25b6 Run<\/label>\n<label for=\"v4r2\" class=\"run run2\" title=\"Show the output in the terminal\">\u25b6 Run<\/label>\n<\/div>\n<div class=\"dd-vsc-pane p1\">\n<div class=\"dd-vsc-crumbs\">agentic-ai-part2 \u203a anatomy_agent.py<\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\"><span class=\"c\"># All four parts of an agent in one class. Runs without an API key.<\/span><\/span>\n<span class=\"l\"><span class=\"kn\">from<\/span> <span class=\"n\">dataclasses<\/span> <span class=\"kn\">import<\/span> <span class=\"n\">dataclass<\/span>, <span class=\"n\">field<\/span><\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">def<\/span> <span class=\"nf\">get_order_status<\/span>(<span class=\"n\">order_id<\/span>):<\/span>\n<span class=\"l\">    <span class=\"k\">return<\/span> {<span class=\"s\">\"A101\"<\/span>: <span class=\"s\">\"delivered\"<\/span>, <span class=\"s\">\"A102\"<\/span>: <span class=\"s\">\"out for delivery\"<\/span>}.<span class=\"n\">get<\/span>(<span class=\"n\">order_id<\/span>, <span class=\"s\">\"not found\"<\/span>)<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">def<\/span> <span class=\"nf\">get_refund_policy<\/span>(<span class=\"n\">topic<\/span>):<\/span>\n<span class=\"l\">    <span class=\"k\">return<\/span> <span class=\"s\">\"Refunds are allowed within 7 days of delivery.\"<\/span><\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">def<\/span> <span class=\"nf\">fake_model<\/span>(<span class=\"n\">instructions<\/span>, <span class=\"n\">memory<\/span>, <span class=\"n\">tools<\/span>):<\/span>\n<span class=\"l\">    <span class=\"s\">\"\"\"Stands in for the LLM. A real model reads the same three inputs.\"\"\"<\/span><\/span>\n<span class=\"l\">    <span class=\"n\">last<\/span> = <span class=\"n\">memory<\/span>[-<span class=\"m\">1<\/span>][<span class=\"s\">\"content\"<\/span>]<\/span>\n<span class=\"l\">    <span class=\"k\">if<\/span> <span class=\"n\">memory<\/span>[-<span class=\"m\">1<\/span>][<span class=\"s\">\"role\"<\/span>] == <span class=\"s\">\"user\"<\/span> <span class=\"k\">and<\/span> <span class=\"s\">\"A101\"<\/span> <span class=\"k\">in<\/span> <span class=\"n\">last<\/span>:<\/span>\n<span class=\"l\">        <span class=\"k\">return<\/span> {<span class=\"s\">\"tool\"<\/span>: <span class=\"s\">\"get_order_status\"<\/span>, <span class=\"s\">\"args\"<\/span>: {<span class=\"s\">\"order_id\"<\/span>: <span class=\"s\">\"A101\"<\/span>}}<\/span>\n<span class=\"l\">    <span class=\"k\">if<\/span> <span class=\"n\">memory<\/span>[-<span class=\"m\">1<\/span>][<span class=\"s\">\"role\"<\/span>] == <span class=\"s\">\"tool\"<\/span> <span class=\"k\">and<\/span> <span class=\"n\">last<\/span> == <span class=\"s\">\"delivered\"<\/span>:<\/span>\n<span class=\"l\">        <span class=\"k\">return<\/span> {<span class=\"s\">\"tool\"<\/span>: <span class=\"s\">\"get_refund_policy\"<\/span>, <span class=\"s\">\"args\"<\/span>: {<span class=\"s\">\"topic\"<\/span>: <span class=\"s\">\"refund\"<\/span>}}<\/span>\n<span class=\"l\">    <span class=\"k\">if<\/span> <span class=\"n\">memory<\/span>[-<span class=\"m\">1<\/span>][<span class=\"s\">\"role\"<\/span>] == <span class=\"s\">\"tool\"<\/span>:<\/span>\n<span class=\"l\">        <span class=\"k\">return<\/span> {<span class=\"s\">\"answer\"<\/span>: <span class=\"s\">f\"Your order A101 was delivered. {<\/span><span class=\"n\">last<\/span><span class=\"s\">}\"<\/span>}<\/span>\n<span class=\"l\">    <span class=\"k\">return<\/span> {<span class=\"s\">\"answer\"<\/span>: <span class=\"s\">\"Could you share your order ID?\"<\/span>}<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"nd\">@dataclass<\/span><\/span>\n<span class=\"l\"><span class=\"k\">class<\/span> <span class=\"nc\">Agent<\/span>:<\/span>\n<span class=\"l\">    <span class=\"n\">name<\/span>: <span class=\"nb\">str<\/span><\/span>\n<span class=\"l\">    <span class=\"n\">model<\/span>: <span class=\"nb\">callable<\/span>                      <span class=\"c\"># 1. the brain<\/span><\/span>\n<span class=\"l\">    <span class=\"n\">instructions<\/span>: <span class=\"nb\">str<\/span>                    <span class=\"c\"># 2. the job description<\/span><\/span>\n<span class=\"l\">    <span class=\"n\">tools<\/span>: <span class=\"nb\">dict<\/span>                          <span class=\"c\"># 3. the hands<\/span><\/span>\n<span class=\"l\">    <span class=\"n\">memory<\/span>: <span class=\"nb\">list<\/span> = <span class=\"n\">field<\/span>(<span class=\"n\">default_factory<\/span>=<span class=\"nb\">list<\/span>)  <span class=\"c\"># 4. the notebook<\/span><\/span>\n<span class=\"l\">    <span class=\"n\">max_steps<\/span>: <span class=\"nb\">int<\/span> = <span class=\"m\">5<\/span>                   <span class=\"c\"># safety limit for the loop<\/span><\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\">    <span class=\"k\">def<\/span> <span class=\"nf\">run<\/span>(<span class=\"nb\">self<\/span>, <span class=\"n\">user_message<\/span>):<\/span>\n<span class=\"l\">        <span class=\"nb\">self<\/span>.<span class=\"n\">memory<\/span>.<span class=\"n\">append<\/span>({<span class=\"s\">\"role\"<\/span>: <span class=\"s\">\"user\"<\/span>, <span class=\"s\">\"content\"<\/span>: <span class=\"n\">user_message<\/span>})<\/span>\n<span class=\"l\">        <span class=\"k\">for<\/span> <span class=\"n\">step<\/span> <span class=\"k\">in<\/span> <span class=\"nb\">range<\/span>(<span class=\"m\">1<\/span>, <span class=\"nb\">self<\/span>.<span class=\"n\">max_steps<\/span> + <span class=\"m\">1<\/span>):<\/span>\n<span class=\"l\">            <span class=\"n\">decision<\/span> = <span class=\"nb\">self<\/span>.<span class=\"n\">model<\/span>(<span class=\"nb\">self<\/span>.<span class=\"n\">instructions<\/span>, <span class=\"nb\">self<\/span>.<span class=\"n\">memory<\/span>, <span class=\"nb\">self<\/span>.<span class=\"n\">tools<\/span>)<\/span>\n<span class=\"l\">            <span class=\"k\">if<\/span> <span class=\"s\">\"answer\"<\/span> <span class=\"k\">in<\/span> <span class=\"n\">decision<\/span>:<\/span>\n<span class=\"l\">                <span class=\"nb\">print<\/span>(<span class=\"s\">f\"Step {<\/span><span class=\"n\">step<\/span><span class=\"s\">}: ANSWER\"<\/span>)<\/span>\n<span class=\"l\">                <span class=\"k\">return<\/span> <span class=\"n\">decision<\/span>[<span class=\"s\">\"answer\"<\/span>]<\/span>\n<span class=\"l\">            <span class=\"n\">name<\/span>, <span class=\"n\">args<\/span> = <span class=\"n\">decision<\/span>[<span class=\"s\">\"tool\"<\/span>], <span class=\"n\">decision<\/span>[<span class=\"s\">\"args\"<\/span>]<\/span>\n<span class=\"l\">            <span class=\"k\">if<\/span> <span class=\"n\">name<\/span> <span class=\"k\">not<\/span> <span class=\"k\">in<\/span> <span class=\"nb\">self<\/span>.<span class=\"n\">tools<\/span>:<\/span>\n<span class=\"l\">                <span class=\"n\">result<\/span> = <span class=\"s\">f\"error: unknown tool {<\/span><span class=\"n\">name<\/span><span class=\"s\">}\"<\/span><\/span>\n<span class=\"l\">            <span class=\"k\">else<\/span>:<\/span>\n<span class=\"l\">                <span class=\"n\">result<\/span> = <span class=\"nb\">self<\/span>.<span class=\"n\">tools<\/span>[<span class=\"n\">name<\/span>](**<span class=\"n\">args<\/span>)<\/span>\n<span class=\"l\">            <span class=\"nb\">print<\/span>(<span class=\"s\">f\"Step {<\/span><span class=\"n\">step<\/span><span class=\"s\">}: {<\/span><span class=\"n\">name<\/span><span class=\"s\">}({<\/span><span class=\"n\">args<\/span><span class=\"s\">}) -&gt; {<\/span><span class=\"n\">result<\/span><span class=\"s\">}\"<\/span>)<\/span>\n<span class=\"l\">            <span class=\"nb\">self<\/span>.<span class=\"n\">memory<\/span>.<span class=\"n\">append<\/span>({<span class=\"s\">\"role\"<\/span>: <span class=\"s\">\"tool\"<\/span>, <span class=\"s\">\"content\"<\/span>: <span class=\"n\">result<\/span>})<\/span>\n<span class=\"l\">        <span class=\"k\">return<\/span> <span class=\"s\">\"Stopped: step limit reached\"<\/span><\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"n\">support_agent<\/span> = <span class=\"n\">Agent<\/span>(<\/span>\n<span class=\"l\">    <span class=\"n\">name<\/span>=<span class=\"s\">\"ShopHelp\"<\/span>,<\/span>\n<span class=\"l\">    <span class=\"n\">model<\/span>=<span class=\"n\">fake_model<\/span>,<\/span>\n<span class=\"l\">    <span class=\"n\">instructions<\/span>=<span class=\"s\">\"You are ShopHelp, a polite support agent. Use tools for facts.\"<\/span>,<\/span>\n<span class=\"l\">    <span class=\"n\">tools<\/span>={<span class=\"s\">\"get_order_status\"<\/span>: <span class=\"n\">get_order_status<\/span>, <span class=\"s\">\"get_refund_policy\"<\/span>: <span class=\"n\">get_refund_policy<\/span>},<\/span>\n<span class=\"l\">)<\/span>\n<span class=\"l\"><span class=\"nb\">print<\/span>(<span class=\"n\">support_agent<\/span>.<span class=\"n\">run<\/span>(<span class=\"s\">\"Can I return order A101?\"<\/span>))<\/span>\n<span class=\"l\"><span class=\"nb\">print<\/span>(<span class=\"s\">\"Memory size:\"<\/span>, <span class=\"nb\">len<\/span>(<span class=\"n\">support_agent<\/span>.<span class=\"n\">memory<\/span>), <span class=\"s\">\"messages\"<\/span>)<\/span><\/code><\/pre>\n<div class=\"dd-vsc-term\"><div class=\"dd-term-head\"><span>TERMINAL<\/span><span>OUTPUT<\/span><span>PROBLEMS<\/span><span class=\"dd-term-note\">Real output<\/span><\/div><pre><code><span class=\"gp\">$<\/span> python anatomy_agent.py\nStep 1: get_order_status({'order_id': 'A101'}) -&gt; delivered\nStep 2: get_refund_policy({'topic': 'refund'}) -&gt; Refunds are allowed within 7 days of delivery.\nStep 3: ANSWER\nYour order A101 was delivered. Refunds are allowed within 7 days of delivery.\nMemory size: 3 messages<\/code><\/pre><\/div>\n<div class=\"dd-vsc-status\"><span>\u2387 main<\/span><span>Ln 53, Col 1<\/span><span class=\"sp\"><\/span><span>Spaces: 4<\/span><span>UTF-8<\/span><span>LF<\/span><span>Python<\/span><\/div>\n<\/div>\n<div class=\"dd-vsc-pane p2\">\n<div class=\"dd-vsc-crumbs\">agentic-ai-part2 \u203a anatomy-agent.mjs<\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\"><span class=\"c\">\/\/ All four parts of an agent in one object. Runs without an API key.<\/span><\/span>\n<span class=\"l\"><span class=\"k\">const<\/span> <span class=\"n\">tools<\/span> = {<\/span>\n<span class=\"l\">  <span class=\"n\">get_order_status<\/span>: ({ <span class=\"n\">order_id<\/span> }) =&gt;<\/span>\n<span class=\"l\">    ({ <span class=\"n\">A101<\/span>: <span class=\"s\">\"delivered\"<\/span>, <span class=\"n\">A102<\/span>: <span class=\"s\">\"out for delivery\"<\/span> })[<span class=\"n\">order_id<\/span>] ?? <span class=\"s\">\"not found\"<\/span>,<\/span>\n<span class=\"l\">  <span class=\"n\">get_refund_policy<\/span>: () =&gt; <span class=\"s\">\"Refunds are allowed within 7 days of delivery.\"<\/span>,<\/span>\n<span class=\"l\">};<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">function<\/span> <span class=\"n\">fakeModel<\/span>(<span class=\"n\">instructions<\/span>, <span class=\"n\">memory<\/span>, <span class=\"n\">tools<\/span>) {<\/span>\n<span class=\"l\">  <span class=\"c\">\/\/ Stands in for the LLM. A real model reads the same three inputs.<\/span><\/span>\n<span class=\"l\">  <span class=\"k\">const<\/span> <span class=\"n\">last<\/span> = <span class=\"n\">memory<\/span>.<span class=\"n\">at<\/span>(-<span class=\"m\">1<\/span>);<\/span>\n<span class=\"l\">  <span class=\"k\">if<\/span> (<span class=\"n\">last<\/span>.<span class=\"n\">role<\/span> === <span class=\"s\">\"user\"<\/span> &amp;&amp; <span class=\"n\">last<\/span>.<span class=\"n\">content<\/span>.<span class=\"n\">includes<\/span>(<span class=\"s\">\"A101\"<\/span>))<\/span>\n<span class=\"l\">    <span class=\"k\">return<\/span> { <span class=\"n\">tool<\/span>: <span class=\"s\">\"get_order_status\"<\/span>, <span class=\"n\">args<\/span>: { <span class=\"n\">order_id<\/span>: <span class=\"s\">\"A101\"<\/span> } };<\/span>\n<span class=\"l\">  <span class=\"k\">if<\/span> (<span class=\"n\">last<\/span>.<span class=\"n\">role<\/span> === <span class=\"s\">\"tool\"<\/span> &amp;&amp; <span class=\"n\">last<\/span>.<span class=\"n\">content<\/span> === <span class=\"s\">\"delivered\"<\/span>)<\/span>\n<span class=\"l\">    <span class=\"k\">return<\/span> { <span class=\"n\">tool<\/span>: <span class=\"s\">\"get_refund_policy\"<\/span>, <span class=\"n\">args<\/span>: { <span class=\"n\">topic<\/span>: <span class=\"s\">\"refund\"<\/span> } };<\/span>\n<span class=\"l\">  <span class=\"k\">if<\/span> (<span class=\"n\">last<\/span>.<span class=\"n\">role<\/span> === <span class=\"s\">\"tool\"<\/span>)<\/span>\n<span class=\"l\">    <span class=\"k\">return<\/span> { <span class=\"n\">answer<\/span>: <span class=\"s\">`Your order A101 was delivered. ${<\/span><span class=\"n\">last<\/span>.<span class=\"n\">content<\/span><span class=\"s\">}`<\/span> };<\/span>\n<span class=\"l\">  <span class=\"k\">return<\/span> { <span class=\"n\">answer<\/span>: <span class=\"s\">\"Could you share your order ID?\"<\/span> };<\/span>\n<span class=\"l\">}<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">function<\/span> <span class=\"n\">createAgent<\/span>({ <span class=\"n\">name<\/span>, <span class=\"n\">model<\/span>, <span class=\"n\">instructions<\/span>, <span class=\"n\">tools<\/span>, <span class=\"n\">maxSteps<\/span> = <span class=\"m\">5<\/span> }) {<\/span>\n<span class=\"l\">  <span class=\"k\">const<\/span> <span class=\"n\">memory<\/span> = []; <span class=\"c\">\/\/ the notebook<\/span><\/span>\n<span class=\"l\">  <span class=\"k\">return<\/span> {<\/span>\n<span class=\"l\">    <span class=\"n\">name<\/span>,<\/span>\n<span class=\"l\">    <span class=\"n\">memory<\/span>,<\/span>\n<span class=\"l\">    <span class=\"n\">run<\/span>(<span class=\"n\">userMessage<\/span>) {<\/span>\n<span class=\"l\">      <span class=\"n\">memory<\/span>.<span class=\"n\">push<\/span>({ <span class=\"n\">role<\/span>: <span class=\"s\">\"user\"<\/span>, <span class=\"n\">content<\/span>: <span class=\"n\">userMessage<\/span> });<\/span>\n<span class=\"l\">      <span class=\"k\">for<\/span> (<span class=\"k\">let<\/span> <span class=\"n\">step<\/span> = <span class=\"m\">1<\/span>; <span class=\"n\">step<\/span> &lt;= <span class=\"n\">maxSteps<\/span>; <span class=\"n\">step<\/span>++) {<\/span>\n<span class=\"l\">        <span class=\"k\">const<\/span> <span class=\"n\">decision<\/span> = <span class=\"n\">model<\/span>(<span class=\"n\">instructions<\/span>, <span class=\"n\">memory<\/span>, <span class=\"n\">tools<\/span>);<\/span>\n<span class=\"l\">        <span class=\"k\">if<\/span> (<span class=\"n\">decision<\/span>.<span class=\"n\">answer<\/span>) {<\/span>\n<span class=\"l\">          <span class=\"nb\">console<\/span>.<span class=\"n\">log<\/span>(<span class=\"s\">`Step ${<\/span><span class=\"n\">step<\/span><span class=\"s\">}: ANSWER`<\/span>);<\/span>\n<span class=\"l\">          <span class=\"k\">return<\/span> <span class=\"n\">decision<\/span>.<span class=\"n\">answer<\/span>;<\/span>\n<span class=\"l\">        }<\/span>\n<span class=\"l\">        <span class=\"k\">const<\/span> <span class=\"n\">fn<\/span> = <span class=\"n\">tools<\/span>[<span class=\"n\">decision<\/span>.<span class=\"n\">tool<\/span>];<\/span>\n<span class=\"l\">        <span class=\"k\">const<\/span> <span class=\"n\">result<\/span> = <span class=\"n\">fn<\/span> ? <span class=\"n\">fn<\/span>(<span class=\"n\">decision<\/span>.<span class=\"n\">args<\/span>) : <span class=\"s\">`error: unknown tool ${<\/span><span class=\"n\">decision<\/span>.<span class=\"n\">tool<\/span><span class=\"s\">}`<\/span>;<\/span>\n<span class=\"l\">        <span class=\"nb\">console<\/span>.<span class=\"n\">log<\/span>(<span class=\"s\">`Step ${<\/span><span class=\"n\">step<\/span><span class=\"s\">}: ${<\/span><span class=\"n\">decision<\/span>.<span class=\"n\">tool<\/span><span class=\"s\">}(${<\/span><span class=\"nb\">JSON<\/span>.<span class=\"n\">stringify<\/span>(<span class=\"n\">decision<\/span>.<span class=\"n\">args<\/span>)<span class=\"s\">}) -&gt; ${<\/span><span class=\"n\">result<\/span><span class=\"s\">}`<\/span>);<\/span>\n<span class=\"l\">        <span class=\"n\">memory<\/span>.<span class=\"n\">push<\/span>({ <span class=\"n\">role<\/span>: <span class=\"s\">\"tool\"<\/span>, <span class=\"n\">content<\/span>: <span class=\"n\">result<\/span> });<\/span>\n<span class=\"l\">      }<\/span>\n<span class=\"l\">      <span class=\"k\">return<\/span> <span class=\"s\">\"Stopped: step limit reached\"<\/span>;<\/span>\n<span class=\"l\">    },<\/span>\n<span class=\"l\">  };<\/span>\n<span class=\"l\">}<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">const<\/span> <span class=\"n\">supportAgent<\/span> = <span class=\"n\">createAgent<\/span>({<\/span>\n<span class=\"l\">  <span class=\"n\">name<\/span>: <span class=\"s\">\"ShopHelp\"<\/span>,<\/span>\n<span class=\"l\">  <span class=\"n\">model<\/span>: <span class=\"n\">fakeModel<\/span>,<\/span>\n<span class=\"l\">  <span class=\"n\">instructions<\/span>: <span class=\"s\">\"You are ShopHelp, a polite support agent. Use tools for facts.\"<\/span>,<\/span>\n<span class=\"l\">  <span class=\"n\">tools<\/span>,<\/span>\n<span class=\"l\">});<\/span>\n<span class=\"l\"><span class=\"nb\">console<\/span>.<span class=\"n\">log<\/span>(<span class=\"n\">supportAgent<\/span>.<span class=\"n\">run<\/span>(<span class=\"s\">\"Can I return order A101?\"<\/span>));<\/span>\n<span class=\"l\"><span class=\"nb\">console<\/span>.<span class=\"n\">log<\/span>(<span class=\"s\">\"Memory size:\"<\/span>, <span class=\"n\">supportAgent<\/span>.<span class=\"n\">memory<\/span>.<span class=\"n\">length<\/span>, <span class=\"s\">\"messages\"<\/span>);<\/span><\/code><\/pre>\n<div class=\"dd-vsc-term\"><div class=\"dd-term-head\"><span>TERMINAL<\/span><span>OUTPUT<\/span><span>PROBLEMS<\/span><span class=\"dd-term-note\">Real output<\/span><\/div><pre><code><span class=\"gp\">$<\/span> node anatomy-agent.mjs\nStep 1: get_order_status({\"order_id\":\"A101\"}) -&gt; delivered\nStep 2: get_refund_policy({\"topic\":\"refund\"}) -&gt; Refunds are allowed within 7 days of delivery.\nStep 3: ANSWER\nYour order A101 was delivered. Refunds are allowed within 7 days of delivery.\nMemory size: 3 messages<\/code><\/pre><\/div>\n<div class=\"dd-vsc-status\"><span>\u2387 main<\/span><span>Ln 50, Col 1<\/span><span class=\"sp\"><\/span><span>Spaces: 2<\/span><span>UTF-8<\/span><span>LF<\/span><span>JavaScript<\/span><\/div>\n<\/div>\n<\/div>\n<p class=\"dd-vsc-hint\">Both tabs print the same steps. Click <strong>\u25b6 Run<\/strong> on each to compare.<\/p>\n<p>Read the output step by step. The agent checks the order (tool 1), sees it was delivered, looks up the refund policy (tool 2), then answers using both results from memory. Each part of the anatomy has one clear place in the code, which makes it easy to change one part without breaking the others.<\/p>\n\n<h2 id=\"real-llm\">Plug in a real LLM<\/h2>\n<p>Ready to use a real model? First set up the project. We use the official OpenAI SDK here, but the same four-part design works with any provider.<\/p>\n<div class=\"dd-vsc dd-vsc-termonly\" role=\"group\" aria-label=\"Terminal: agentic-ai-part2 \u2014 bash\">\n<div class=\"dd-vsc-bar\"><span class=\"dd-dots\"><i><\/i><i><\/i><i><\/i><\/span><span class=\"dd-vsc-title\">agentic-ai-part2 \u2014 bash<\/span><\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\"><span class=\"c\"># 1. Create a project folder<\/span><\/span>\n<span class=\"l\"><span class=\"gp\">$<\/span> mkdir agentic-ai-part2 &amp;&amp; cd agentic-ai-part2<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"c\"># 2. Python: virtual environment + OpenAI SDK<\/span><\/span>\n<span class=\"l\"><span class=\"gp\">$<\/span> python -m venv .venv<\/span>\n<span class=\"l\"><span class=\"gp\">$<\/span> source .venv\/bin\/activate      # Windows: .venv\\Scripts\\activate<\/span>\n<span class=\"l\"><span class=\"gp\">$<\/span> pip install openai python-dotenv<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"c\"># 3. Save your key and model in .env (never commit this file)<\/span><\/span>\n<span class=\"l\"><span class=\"gp\">$<\/span> echo \"OPENAI_API_KEY=sk-your-key\" &gt; .env<\/span>\n<span class=\"l\"><span class=\"gp\">$<\/span> echo \"OPENAI_MODEL=gpt-6-luna\" &gt;&gt; .env<\/span>\n<span class=\"l\"><span class=\"gp\">$<\/span> echo \".env\" &gt;&gt; .gitignore<\/span><\/code><\/pre>\n<\/div>\n<p>Now the real agent. Look for the four numbered comments: model, instructions, tools, memory. The memory list is kept between the two <code>run()<\/code> calls, so the second question (&#8220;what was the order ID?&#8221;) can be answered without any tool.<\/p>\n<div class=\"dd-vsc\" role=\"group\" aria-label=\"Code editor: real_agent.py\">\n<input type=\"radio\" name=\"v5\" id=\"v5f1\" class=\"f1\" checked>\n<input type=\"checkbox\" id=\"v5r1\" class=\"r1\">\n<div class=\"dd-vsc-bar\"><span class=\"dd-dots\"><i><\/i><i><\/i><i><\/i><\/span><span class=\"dd-vsc-title\">real_agent.py \u2014 agentic-ai-part2 \u2014 Visual Studio Code<\/span><\/div>\n<div class=\"dd-vsc-tabs\">\n<label for=\"v5f1\" class=\"t1\"><b class=\"ic py\">PY<\/b>real_agent.py<\/label>\n<label for=\"v5r1\" class=\"run run1\" title=\"Show the output in the terminal\">\u25b6 Run<\/label>\n<\/div>\n<div class=\"dd-vsc-pane p1\">\n<div class=\"dd-vsc-crumbs\">agentic-ai-part2 \u203a real_agent.py<\/div>\n<pre class=\"dd-vsc-code\"><code><span class=\"l\"><span class=\"c\"># pip install openai python-dotenv  (needs OPENAI_API_KEY in .env)<\/span><\/span>\n<span class=\"l\"><span class=\"kn\">import<\/span> <span class=\"n\">json<\/span><\/span>\n<span class=\"l\"><span class=\"kn\">import<\/span> <span class=\"n\">os<\/span><\/span>\n<span class=\"l\"><span class=\"kn\">from<\/span> <span class=\"n\">dotenv<\/span> <span class=\"kn\">import<\/span> <span class=\"n\">load_dotenv<\/span><\/span>\n<span class=\"l\"><span class=\"kn\">from<\/span> <span class=\"n\">openai<\/span> <span class=\"kn\">import<\/span> <span class=\"n\">OpenAI<\/span><\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"n\">load_dotenv<\/span>()<\/span>\n<span class=\"l\"><span class=\"n\">client<\/span> = <span class=\"n\">OpenAI<\/span>()<\/span>\n<span class=\"l\"><span class=\"n\">MODEL<\/span> = <span class=\"n\">os<\/span>.<span class=\"n\">getenv<\/span>(<span class=\"s\">\"OPENAI_MODEL\"<\/span>, <span class=\"s\">\"gpt-6-luna\"<\/span>)          <span class=\"c\"># 1. model<\/span><\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"n\">INSTRUCTIONS<\/span> = (                                          <span class=\"c\"># 2. instructions<\/span><\/span>\n<span class=\"l\">    <span class=\"s\">\"You are ShopHelp, a friendly support agent for an online store. \"<\/span><\/span>\n<span class=\"l\">    <span class=\"s\">\"Never guess order details: always use a tool. \"<\/span><\/span>\n<span class=\"l\">    <span class=\"s\">\"Answer in two or three short sentences.\"<\/span><\/span>\n<span class=\"l\">)<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">def<\/span> <span class=\"nf\">get_order_status<\/span>(<span class=\"n\">order_id<\/span>: <span class=\"nb\">str<\/span>):                      <span class=\"c\"># 3. tools<\/span><\/span>\n<span class=\"l\">    <span class=\"n\">orders<\/span> = {<span class=\"s\">\"A101\"<\/span>: <span class=\"s\">\"delivered\"<\/span>, <span class=\"s\">\"A102\"<\/span>: <span class=\"s\">\"out for delivery\"<\/span>}<\/span>\n<span class=\"l\">    <span class=\"k\">return<\/span> <span class=\"n\">orders<\/span>.<span class=\"n\">get<\/span>(<span class=\"n\">order_id<\/span>.<span class=\"n\">upper<\/span>(), <span class=\"s\">\"not found\"<\/span>)<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"n\">TOOL_FUNCTIONS<\/span> = {<span class=\"s\">\"get_order_status\"<\/span>: <span class=\"n\">get_order_status<\/span>}<\/span>\n<span class=\"l\"><span class=\"n\">TOOLS<\/span> = [{<\/span>\n<span class=\"l\">    <span class=\"s\">\"type\"<\/span>: <span class=\"s\">\"function\"<\/span>,<\/span>\n<span class=\"l\">    <span class=\"s\">\"name\"<\/span>: <span class=\"s\">\"get_order_status\"<\/span>,<\/span>\n<span class=\"l\">    <span class=\"s\">\"description\"<\/span>: <span class=\"s\">\"Get the delivery status of an order by its ID, e.g. A102.\"<\/span>,<\/span>\n<span class=\"l\">    <span class=\"s\">\"parameters\"<\/span>: {<\/span>\n<span class=\"l\">        <span class=\"s\">\"type\"<\/span>: <span class=\"s\">\"object\"<\/span>,<\/span>\n<span class=\"l\">        <span class=\"s\">\"properties\"<\/span>: {<span class=\"s\">\"order_id\"<\/span>: {<span class=\"s\">\"type\"<\/span>: <span class=\"s\">\"string\"<\/span>}},<\/span>\n<span class=\"l\">        <span class=\"s\">\"required\"<\/span>: [<span class=\"s\">\"order_id\"<\/span>],<\/span>\n<span class=\"l\">        <span class=\"s\">\"additionalProperties\"<\/span>: <span class=\"kc\">False<\/span>,<\/span>\n<span class=\"l\">    },<\/span>\n<span class=\"l\">    <span class=\"s\">\"strict\"<\/span>: <span class=\"kc\">True<\/span>,<\/span>\n<span class=\"l\">}]<\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"n\">memory<\/span> = []                                               <span class=\"c\"># 4. memory<\/span><\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"k\">def<\/span> <span class=\"nf\">run<\/span>(<span class=\"n\">user_message<\/span>, <span class=\"n\">max_steps<\/span>=<span class=\"m\">5<\/span>):<\/span>\n<span class=\"l\">    <span class=\"n\">memory<\/span>.<span class=\"n\">append<\/span>({<span class=\"s\">\"role\"<\/span>: <span class=\"s\">\"user\"<\/span>, <span class=\"s\">\"content\"<\/span>: <span class=\"n\">user_message<\/span>})<\/span>\n<span class=\"l\">    <span class=\"k\">for<\/span> <span class=\"n\">_<\/span> <span class=\"k\">in<\/span> <span class=\"nb\">range<\/span>(<span class=\"n\">max_steps<\/span>):<\/span>\n<span class=\"l\">        <span class=\"n\">response<\/span> = <span class=\"n\">client<\/span>.<span class=\"n\">responses<\/span>.<span class=\"n\">create<\/span>(<\/span>\n<span class=\"l\">            <span class=\"n\">model<\/span>=<span class=\"n\">MODEL<\/span>, <span class=\"n\">instructions<\/span>=<span class=\"n\">INSTRUCTIONS<\/span>, <span class=\"nb\">input<\/span>=<span class=\"n\">memory<\/span>, <span class=\"n\">tools<\/span>=<span class=\"n\">TOOLS<\/span><\/span>\n<span class=\"l\">        )<\/span>\n<span class=\"l\">        <span class=\"n\">memory<\/span>.<span class=\"n\">extend<\/span>(<span class=\"n\">response<\/span>.<span class=\"n\">output<\/span>)                    <span class=\"c\"># remember the reply<\/span><\/span>\n<span class=\"l\">        <span class=\"n\">calls<\/span> = [<span class=\"n\">o<\/span> <span class=\"k\">for<\/span> <span class=\"n\">o<\/span> <span class=\"k\">in<\/span> <span class=\"n\">response<\/span>.<span class=\"n\">output<\/span> <span class=\"k\">if<\/span> <span class=\"n\">o<\/span>.<span class=\"n\">type<\/span> == <span class=\"s\">\"function_call\"<\/span>]<\/span>\n<span class=\"l\">        <span class=\"k\">if<\/span> <span class=\"k\">not<\/span> <span class=\"n\">calls<\/span>:<\/span>\n<span class=\"l\">            <span class=\"k\">return<\/span> <span class=\"n\">response<\/span>.<span class=\"n\">output_text<\/span><\/span>\n<span class=\"l\">        <span class=\"k\">for<\/span> <span class=\"n\">call<\/span> <span class=\"k\">in<\/span> <span class=\"n\">calls<\/span>:<\/span>\n<span class=\"l\">            <span class=\"n\">result<\/span> = <span class=\"n\">TOOL_FUNCTIONS<\/span>[<span class=\"n\">call<\/span>.<span class=\"n\">name<\/span>](**<span class=\"n\">json<\/span>.<span class=\"n\">loads<\/span>(<span class=\"n\">call<\/span>.<span class=\"n\">arguments<\/span>))<\/span>\n<span class=\"l\">            <span class=\"nb\">print<\/span>(<span class=\"s\">f\"  tool: {<\/span><span class=\"n\">call<\/span>.<span class=\"n\">name<\/span><span class=\"s\">} -&gt; {<\/span><span class=\"n\">result<\/span><span class=\"s\">}\"<\/span>)<\/span>\n<span class=\"l\">            <span class=\"n\">memory<\/span>.<span class=\"n\">append<\/span>({<span class=\"s\">\"type\"<\/span>: <span class=\"s\">\"function_call_output\"<\/span>,<\/span>\n<span class=\"l\">                           <span class=\"s\">\"call_id\"<\/span>: <span class=\"n\">call<\/span>.<span class=\"n\">call_id<\/span>, <span class=\"s\">\"output\"<\/span>: <span class=\"nb\">str<\/span>(<span class=\"n\">result<\/span>)})<\/span>\n<span class=\"l\">    <span class=\"k\">return<\/span> <span class=\"s\">\"Stopped: step limit reached\"<\/span><\/span>\n<span class=\"l\"><\/span>\n<span class=\"l\"><span class=\"nb\">print<\/span>(<span class=\"n\">run<\/span>(<span class=\"s\">\"Where is my order A102?\"<\/span>))<\/span>\n<span class=\"l\"><span class=\"nb\">print<\/span>(<span class=\"n\">run<\/span>(<span class=\"s\">\"And what was the order ID I asked about?\"<\/span>))     <span class=\"c\"># memory at work<\/span><\/span><\/code><\/pre>\n<div class=\"dd-vsc-term\"><div class=\"dd-term-head\"><span>TERMINAL<\/span><span>OUTPUT<\/span><span>PROBLEMS<\/span><span class=\"dd-term-note\">Example output \u2013 the wording will vary<\/span><\/div><pre><code><span class=\"gp\">$<\/span> python real_agent.py\n  tool: get_order_status -&gt; out for delivery\nYour order A102 is out for delivery, so it should reach you soon.\nYou asked about order A102.<\/code><\/pre><\/div>\n<div class=\"dd-vsc-status\"><span>\u2387 main<\/span><span>Ln 55, Col 1<\/span><span class=\"sp\"><\/span><span>Spaces: 4<\/span><span>UTF-8<\/span><span>LF<\/span><span>Python<\/span><\/div>\n<\/div>\n<\/div>\n<p class=\"dd-vsc-hint\">Press <strong>\u25b6 Run<\/strong> to see an example of what this prints. Real answers will be worded a little differently each time.<\/p>\n<div class=\"dd-tip\"><strong>Why we save <code>response.output<\/code> into memory:<\/strong> the Responses API needs to see its own earlier tool call next to the <code>function_call_output<\/code> with the same <code>call_id<\/code>. If you only save the tool result, you get an error about a missing tool call.<\/div>\n<\/div>\n\n\n\n<div class=\"dd-post\">\n<h2 id=\"common-errors\">Common errors and fixes<\/h2>\n<div class=\"dd-tablewrap\"><table>\n<thead><tr><th>Problem<\/th><th>Likely cause<\/th><th>Fix<\/th><\/tr><\/thead>\n<tbody>\n<tr><td>Agent never uses a tool and guesses instead<\/td><td>Vague tool description, or instructions don&#8217;t say &#8220;use tools for facts&#8221;<\/td><td>Describe <em>when<\/em> to use the tool and add a rule like &#8220;Never guess order details&#8221;<\/td><\/tr>\n<tr><td>Agent calls the wrong tool<\/td><td>Two tools with overlapping descriptions<\/td><td>Merge them, or make each description clearly different<\/td><\/tr>\n<tr><td><code>KeyError<\/code> \/ crash on an unknown tool name<\/td><td>The runtime trusts the model blindly<\/td><td>Check the name against your registry and return an error string<\/td><\/tr>\n<tr><td><code>TypeError: unexpected keyword argument<\/code><\/td><td>Model sent an argument your function doesn&#8217;t accept<\/td><td>Use <code>\"strict\": true<\/code> and <code>additionalProperties: false<\/code> in the schema<\/td><\/tr>\n<tr><td>&#8220;No tool call found for function call output&#8221;<\/td><td>Tool result saved without the model&#8217;s tool-call item<\/td><td>Add <code>response.output<\/code> to memory before the outputs<\/td><\/tr>\n<tr><td>Context length exceeded after many steps<\/td><td>Memory grows forever<\/td><td>Trim or summarise old messages; keep tool results short<\/td><\/tr>\n<tr><td>Agent loops forever<\/td><td>No stopping rule and no step limit<\/td><td>Add &#8220;When to stop&#8221; to instructions and <code>max_steps<\/code> in code<\/td><\/tr>\n<\/tbody><\/table><\/div>\n\n<h2 id=\"best-practices\">Best practices for each part<\/h2>\n<ul>\n<li><strong>Model:<\/strong> keep the name in config, test with a strong model first, then try cheaper ones against the same test cases.<\/li>\n<li><strong>Tools:<\/strong> few, focused tools beat many overlapping ones. Return short, useful results (not a 5,000-line JSON dump). Start read-only.<\/li>\n<li><strong>Instructions:<\/strong> use the six-section structure, keep it short, and put important rules near the top.<\/li>\n<li><strong>Memory:<\/strong> decide what must be remembered and for how long. Never store secrets like passwords or API keys in agent memory.<\/li>\n<li><strong>Runtime:<\/strong> always have a step limit, catch tool errors, and log every step so you can see why the agent did something.<\/li>\n<\/ul>\n\n<h2 id=\"practice\">Practice task<\/h2>\n<div class=\"dd-note\">\n<p><strong>Your task:<\/strong> extend the <code>Agent<\/code> class from &#8220;Build an agent with all four parts&#8221;:<\/p>\n<ol>\n<li>Add a third tool, <code>get_delivery_date(order_id)<\/code>, that returns a date for order <code>A102<\/code>.<\/li>\n<li>Update the fake model so that the question &#8220;When will A102 arrive?&#8221; calls <code>get_order_status<\/code> and then <code>get_delivery_date<\/code>.<\/li>\n<li>Add a <code>LongTermMemory<\/code> object (from the memory example) that saves the customer&#8217;s last order ID, and print it after the run.<\/li>\n<li>Bonus: set <code>max_steps=1<\/code> and check that the agent stops with &#8220;Stopped: step limit reached&#8221;.<\/li>\n<\/ol>\n<p>Hint: you only need to change the tools dictionary and the fake model. The loop stays the same. That is the whole point of a clean anatomy.<\/p>\n<\/div>\n\n<h2 id=\"faq\">FAQ: anatomy of an AI agent<\/h2>\n<details><summary>What are the main components of an AI agent?<\/summary><p>Four parts: a model (LLM) that makes decisions, tools that let it act, instructions that define its role and rules, and memory that holds what it knows. A runtime loop in your code connects them.<\/p><\/details>\n<details><summary>Is the system prompt the same as agent instructions?<\/summary><p>Yes, mostly. &#8220;Instructions&#8221; is the name used by many agent SDKs (for example the <code>instructions<\/code> parameter in OpenAI&#8217;s Responses API). It is sent to the model on every step, like a system prompt.<\/p><\/details>\n<details><summary>Does the LLM run the tools itself?<\/summary><p>No. The model only says which tool to call and with which arguments. Your code runs the function and sends the result back. This keeps you in control.<\/p><\/details>\n<details><summary>How many tools should one agent have?<\/summary><p>Start with 2\u20135 focused tools. As the number grows, the model gets more confused about which one to use. When you need many tools, split the work across several agents (Part 17).<\/p><\/details>\n<details><summary>What is the difference between short-term and long-term memory?<\/summary><p>Short-term memory is the current conversation and tool results, sent with each request. Long-term memory is saved outside the model (file, database or vector store) and loaded when needed, so it survives restarts.<\/p><\/details>\n<details><summary>Do I need a vector database for agent memory?<\/summary><p>Not at first. A list for short-term memory and a JSON file or normal database table for long-term facts is enough for most beginner projects. Vector search helps when you have lots of notes to search by meaning.<\/p><\/details>\n\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>You now know the full <strong>anatomy of an AI agent<\/strong>. The <strong>model<\/strong> decides, the <strong>tools<\/strong> act, the <strong>instructions<\/strong> guide and the <strong>memory<\/strong> remembers, and a small runtime loop in your code ties them together with a step limit and error handling. You built a tool registry, structured instructions, three kinds of memory and a complete agent in Python and Node.js, then saw how to connect the same design to a real LLM. When an agent misbehaves, you can now ask a clear question: which of the four parts is the problem?<\/p>\n<p>Missed the basics? Go back to <a href=\"https:\/\/devdojo.co.in\/index.php\/2026\/10\/20\/agentic-ai-series-part-1-what-is-agentic-ai\/\">Agentic AI Part 1<\/a>. Want to see a full SDK in action? Read our <a href=\"https:\/\/devdojo.co.in\/index.php\/2026\/10\/08\/openai-agents-sdk-python\/\">OpenAI Agents SDK tutorial<\/a>.<\/p>\n\n<div class=\"dd-next\"><strong>Next part \u2013 Agentic AI Part 3: Tool Calling for Agents: Letting an LLM Use Your Functions.<\/strong> We will zoom into the &#8220;hands&#8221;: how tool calling works under the hood, parallel tool calls, strict schemas, handling tool errors, and returning results the model can actually use.<\/div>\n\n<div class=\"dd-cta\">\n<h3>New posts every day on DevDojo<\/h3>\n<p>Follow the full <strong>Agentic AI series<\/strong> \u2013 30 parts from &#8220;what is an agent&#8221; to building a multi-agent developer assistant. Bookmark <a href=\"https:\/\/devdojo.co.in\/\">devdojo.co.in<\/a> and tell us in the comments which part of the agent anatomy you want to explore first.<\/p>\n<\/div>\n<\/div>\n\n","protected":false},"excerpt":{"rendered":"<p>Learn the anatomy of an AI agent: models, tools, instructions and memory, with runnable Python and Node.js code. Agentic AI series Part 2 for beginners.<\/p>\n","protected":false},"author":1,"featured_media":247,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38],"tags":[136,119,135,42,52,118,120],"class_list":["post-248","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ai","tag-agent-memory","tag-agentic-ai","tag-ai-agent-anatomy","tag-ai-agents","tag-python","tag-series-agentic-ai","tag-tool-calling"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.4 - aioseo.com -->\n\t<meta name=\"description\" content=\"Learn the anatomy of an AI agent: models, tools, instructions and memory, with runnable Python and Node.js code. Agentic AI series Part 2 for beginners.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Lokendra\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/devdojo.co.in\/index.php\/2026\/10\/27\/agentic-ai-series-part-2-anatomy-of-an-ai-agent\/\" \/>\n\t\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.4\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"DevDojo - Empowering Developers\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Agentic AI Part 2: Anatomy of an AI Agent\" \/>\n\t\t<meta property=\"og:description\" content=\"Learn the anatomy of an AI agent: models, tools, instructions and memory, with runnable Python and Node.js code. Agentic AI series Part 2 for beginners.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/devdojo.co.in\/index.php\/2026\/10\/27\/agentic-ai-series-part-2-anatomy-of-an-ai-agent\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2026-10-27T13:28:30+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2026-10-27T13:28:31+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Agentic AI Part 2: Anatomy of an AI Agent\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Learn the anatomy of an AI agent: models, tools, instructions and memory, with runnable Python and Node.js code. Agentic AI series Part 2 for beginners.\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"BlogPosting\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/27\\\/agentic-ai-series-part-2-anatomy-of-an-ai-agent\\\/#blogposting\",\"name\":\"Anatomy of an AI Agent: Models, Tools, Instructions, Memory\",\"headline\":\"Agentic AI Part 2: Anatomy of an AI Agent: Models, Tools, Instructions and Memory\",\"author\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/author\\\/lokendra\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/#organization\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/devdojo.co.in\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/agentic-ai-series-part-2-anatomy-of-an-ai-agent.jpg\",\"width\":1200,\"height\":630,\"caption\":\"Agentic AI Part 2 banner: anatomy of an AI agent with model, tools, instructions and memory around the agent loop\"},\"datePublished\":\"2026-10-27T13:28:30+00:00\",\"dateModified\":\"2026-10-27T13:28:31+00:00\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/27\\\/agentic-ai-series-part-2-anatomy-of-an-ai-agent\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/27\\\/agentic-ai-series-part-2-anatomy-of-an-ai-agent\\\/#webpage\"},\"articleSection\":\"AI, Agent Memory, Agentic AI, AI Agent Anatomy, AI Agents, Python, Series: Agentic AI, Tool Calling\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/27\\\/agentic-ai-series-part-2-anatomy-of-an-ai-agent\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/devdojo.co.in\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/category\\\/ai\\\/#listItem\",\"name\":\"AI\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/category\\\/ai\\\/#listItem\",\"position\":2,\"name\":\"AI\",\"item\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/category\\\/ai\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/27\\\/agentic-ai-series-part-2-anatomy-of-an-ai-agent\\\/#listItem\",\"name\":\"Agentic AI Part 2: Anatomy of an AI Agent: Models, Tools, Instructions and Memory\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/27\\\/agentic-ai-series-part-2-anatomy-of-an-ai-agent\\\/#listItem\",\"position\":3,\"name\":\"Agentic AI Part 2: Anatomy of an AI Agent: Models, Tools, Instructions and Memory\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/category\\\/ai\\\/#listItem\",\"name\":\"AI\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/#organization\",\"name\":\"DevDojo\",\"description\":\"Empowering Developers\",\"url\":\"https:\\\/\\\/devdojo.co.in\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/author\\\/lokendra\\\/#author\",\"url\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/author\\\/lokendra\\\/\",\"name\":\"Lokendra\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/27\\\/agentic-ai-series-part-2-anatomy-of-an-ai-agent\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/99f102f0cc9080237ce54b91304fd1b3b6fa29925cbaeba93cd796872e58b45c?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Lokendra\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/27\\\/agentic-ai-series-part-2-anatomy-of-an-ai-agent\\\/#webpage\",\"url\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/27\\\/agentic-ai-series-part-2-anatomy-of-an-ai-agent\\\/\",\"name\":\"Anatomy of an AI Agent: Models, Tools, Instructions, Memory\",\"description\":\"Learn the anatomy of an AI agent: models, tools, instructions and memory, with runnable Python and Node.js code. Agentic AI series Part 2 for beginners.\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/27\\\/agentic-ai-series-part-2-anatomy-of-an-ai-agent\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/author\\\/lokendra\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/author\\\/lokendra\\\/#author\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/devdojo.co.in\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/agentic-ai-series-part-2-anatomy-of-an-ai-agent.jpg\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/27\\\/agentic-ai-series-part-2-anatomy-of-an-ai-agent\\\/#mainImage\",\"width\":1200,\"height\":630,\"caption\":\"Agentic AI Part 2 banner: anatomy of an AI agent with model, tools, instructions and memory around the agent loop\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/index.php\\\/2026\\\/10\\\/27\\\/agentic-ai-series-part-2-anatomy-of-an-ai-agent\\\/#mainImage\"},\"datePublished\":\"2026-10-27T13:28:30+00:00\",\"dateModified\":\"2026-10-27T13:28:31+00:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/#website\",\"url\":\"https:\\\/\\\/devdojo.co.in\\\/\",\"name\":\"DevDojo\",\"description\":\"Empowering Developers\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/devdojo.co.in\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Anatomy of an AI Agent: Models, Tools, Instructions, Memory","description":"Learn the anatomy of an AI agent: models, tools, instructions and memory, with runnable Python and Node.js code. Agentic AI series Part 2 for beginners.","canonical_url":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/27\/agentic-ai-series-part-2-anatomy-of-an-ai-agent\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"BlogPosting","@id":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/27\/agentic-ai-series-part-2-anatomy-of-an-ai-agent\/#blogposting","name":"Anatomy of an AI Agent: Models, Tools, Instructions, Memory","headline":"Agentic AI Part 2: Anatomy of an AI Agent: Models, Tools, Instructions and Memory","author":{"@id":"https:\/\/devdojo.co.in\/index.php\/author\/lokendra\/#author"},"publisher":{"@id":"https:\/\/devdojo.co.in\/#organization"},"image":{"@type":"ImageObject","url":"https:\/\/devdojo.co.in\/wp-content\/uploads\/2026\/10\/agentic-ai-series-part-2-anatomy-of-an-ai-agent.jpg","width":1200,"height":630,"caption":"Agentic AI Part 2 banner: anatomy of an AI agent with model, tools, instructions and memory around the agent loop"},"datePublished":"2026-10-27T13:28:30+00:00","dateModified":"2026-10-27T13:28:31+00:00","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/27\/agentic-ai-series-part-2-anatomy-of-an-ai-agent\/#webpage"},"isPartOf":{"@id":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/27\/agentic-ai-series-part-2-anatomy-of-an-ai-agent\/#webpage"},"articleSection":"AI, Agent Memory, Agentic AI, AI Agent Anatomy, AI Agents, Python, Series: Agentic AI, Tool Calling"},{"@type":"BreadcrumbList","@id":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/27\/agentic-ai-series-part-2-anatomy-of-an-ai-agent\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/#listItem","position":1,"name":"Home","item":"https:\/\/devdojo.co.in\/","nextItem":{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/index.php\/category\/ai\/#listItem","name":"AI"}},{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/index.php\/category\/ai\/#listItem","position":2,"name":"AI","item":"https:\/\/devdojo.co.in\/index.php\/category\/ai\/","nextItem":{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/27\/agentic-ai-series-part-2-anatomy-of-an-ai-agent\/#listItem","name":"Agentic AI Part 2: Anatomy of an AI Agent: Models, Tools, Instructions and Memory"},"previousItem":{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/27\/agentic-ai-series-part-2-anatomy-of-an-ai-agent\/#listItem","position":3,"name":"Agentic AI Part 2: Anatomy of an AI Agent: Models, Tools, Instructions and Memory","previousItem":{"@type":"ListItem","@id":"https:\/\/devdojo.co.in\/index.php\/category\/ai\/#listItem","name":"AI"}}]},{"@type":"Organization","@id":"https:\/\/devdojo.co.in\/#organization","name":"DevDojo","description":"Empowering Developers","url":"https:\/\/devdojo.co.in\/"},{"@type":"Person","@id":"https:\/\/devdojo.co.in\/index.php\/author\/lokendra\/#author","url":"https:\/\/devdojo.co.in\/index.php\/author\/lokendra\/","name":"Lokendra","image":{"@type":"ImageObject","@id":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/27\/agentic-ai-series-part-2-anatomy-of-an-ai-agent\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/99f102f0cc9080237ce54b91304fd1b3b6fa29925cbaeba93cd796872e58b45c?s=96&d=mm&r=g","width":96,"height":96,"caption":"Lokendra"}},{"@type":"WebPage","@id":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/27\/agentic-ai-series-part-2-anatomy-of-an-ai-agent\/#webpage","url":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/27\/agentic-ai-series-part-2-anatomy-of-an-ai-agent\/","name":"Anatomy of an AI Agent: Models, Tools, Instructions, Memory","description":"Learn the anatomy of an AI agent: models, tools, instructions and memory, with runnable Python and Node.js code. Agentic AI series Part 2 for beginners.","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/devdojo.co.in\/#website"},"breadcrumb":{"@id":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/27\/agentic-ai-series-part-2-anatomy-of-an-ai-agent\/#breadcrumblist"},"author":{"@id":"https:\/\/devdojo.co.in\/index.php\/author\/lokendra\/#author"},"creator":{"@id":"https:\/\/devdojo.co.in\/index.php\/author\/lokendra\/#author"},"image":{"@type":"ImageObject","url":"https:\/\/devdojo.co.in\/wp-content\/uploads\/2026\/10\/agentic-ai-series-part-2-anatomy-of-an-ai-agent.jpg","@id":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/27\/agentic-ai-series-part-2-anatomy-of-an-ai-agent\/#mainImage","width":1200,"height":630,"caption":"Agentic AI Part 2 banner: anatomy of an AI agent with model, tools, instructions and memory around the agent loop"},"primaryImageOfPage":{"@id":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/27\/agentic-ai-series-part-2-anatomy-of-an-ai-agent\/#mainImage"},"datePublished":"2026-10-27T13:28:30+00:00","dateModified":"2026-10-27T13:28:31+00:00"},{"@type":"WebSite","@id":"https:\/\/devdojo.co.in\/#website","url":"https:\/\/devdojo.co.in\/","name":"DevDojo","description":"Empowering Developers","inLanguage":"en-US","publisher":{"@id":"https:\/\/devdojo.co.in\/#organization"}}]},"og:locale":"en_US","og:site_name":"DevDojo - Empowering Developers","og:type":"article","og:title":"Agentic AI Part 2: Anatomy of an AI Agent","og:description":"Learn the anatomy of an AI agent: models, tools, instructions and memory, with runnable Python and Node.js code. Agentic AI series Part 2 for beginners.","og:url":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/27\/agentic-ai-series-part-2-anatomy-of-an-ai-agent\/","article:published_time":"2026-10-27T13:28:30+00:00","article:modified_time":"2026-10-27T13:28:31+00:00","twitter:card":"summary_large_image","twitter:title":"Agentic AI Part 2: Anatomy of an AI Agent","twitter:description":"Learn the anatomy of an AI agent: models, tools, instructions and memory, with runnable Python and Node.js code. Agentic AI series Part 2 for beginners."},"aioseo_meta_data":{"post_id":"248","title":"Anatomy of an AI Agent: Models, Tools, Instructions, Memory","description":"Learn the anatomy of an AI agent: models, tools, instructions and memory, with runnable Python and Node.js code. Agentic AI series Part 2 for beginners.","keywords":null,"keyphrases":{"focus":{"keyphrase":"anatomy of an AI agent","score":74},"additional":[]},"primary_term":null,"canonical_url":null,"og_title":"Agentic AI Part 2: Anatomy of an AI Agent","og_description":"Learn the anatomy of an AI agent: models, tools, instructions and memory, with runnable Python and Node.js code. Agentic AI series Part 2 for beginners.","og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"BlogPosting","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":0,"frequency":"default","local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":{"faqs":[],"keyPoints":[],"schemas":[],"titles":[],"descriptions":[],"socialPosts":{"email":{"subject":"","preview":"","content":""},"linkedin":[],"twitter":[],"facebook":[],"instagram":[]}},"created":"2026-10-27 13:21:18","updated":"2026-10-27 14:48:56","focus_keyword":"anatomy of an AI agent","additional_keywords":null,"truseo_locale":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/devdojo.co.in\/\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">\u00bb<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/devdojo.co.in\/index.php\/category\/ai\/\" title=\"AI\">AI<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">\u00bb<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tAgentic AI Part 2: Anatomy of an AI Agent: Models, Tools, Instructions and Memory\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/devdojo.co.in\/"},{"label":"AI","link":"https:\/\/devdojo.co.in\/index.php\/category\/ai\/"},{"label":"Agentic AI Part 2: Anatomy of an AI Agent: Models, Tools, Instructions and Memory","link":"https:\/\/devdojo.co.in\/index.php\/2026\/10\/27\/agentic-ai-series-part-2-anatomy-of-an-ai-agent\/"}],"_links":{"self":[{"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/posts\/248","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/comments?post=248"}],"version-history":[{"count":1,"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/posts\/248\/revisions"}],"predecessor-version":[{"id":249,"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/posts\/248\/revisions\/249"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/media\/247"}],"wp:attachment":[{"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/media?parent=248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/categories?post=248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devdojo.co.in\/index.php\/wp-json\/wp\/v2\/tags?post=248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}