{"id":218576,"date":"2025-02-11T13:58:54","date_gmt":"2025-02-11T18:58:54","guid":{"rendered":"https:\/\/ibkrcampus.com\/campus\/?p=218576"},"modified":"2025-02-11T13:58:16","modified_gmt":"2025-02-11T18:58:16","slug":"how-to-build-llm-agents-with-magentic","status":"publish","type":"post","link":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-build-llm-agents-with-magentic\/","title":{"rendered":"How to Build LLM Agents with Magentic"},"content":{"rendered":"\n<p><em>The post \u201cHow to Build LLM Agents with Magentic\u201d first appeared on <a href=\"https:\/\/algotrading101.com\/learn\/magentic-llm-guide\/\">AlgoTrading101<\/a> blog.<\/em><\/p>\n\n\n\n<p><strong><em>The author of this article is not affiliated with Interactive Brokers. This software is in no way affiliated, endorsed, or approved by Interactive Brokers or any of its affiliates. It comes with absolutely no warranty and should not be used in actual trading unless the user can read and understand the source. The IBKR API team does not support this software<\/em><\/strong>.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"h-excerpt\">Excerpt<\/h5>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-what-is-magentic\">What is Magentic?<\/h2>\n\n\n\n<p>Magentic is an open-source framework that allows for the seamless integration of Large Language Models (LLMs) into Python code.<\/p>\n\n\n\n<p><a>Website:&nbsp;<\/a><a href=\"https:\/\/magentic.dev\/\">Magentic<\/a><\/p>\n\n\n\n<p>GitHub Repository:&nbsp;<a href=\"https:\/\/github.com\/jackmpcollins\/magentic\">jackmpcollins\/magentic: Seamlessly integrate LLMs as Python functions<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why should I use Magentic?<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Magentic is easy to use.<\/li>\n\n\n\n<li>Magentic is free and open-sourced.<\/li>\n\n\n\n<li>It is actively maintained.<\/li>\n\n\n\n<li>Plays well with Pydantic to produce structured outputs.<\/li>\n\n\n\n<li>Supports vision, streaming, parallel function calling, and more.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Why shouldn\u2019t I use Magentic?<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Magentic is mainly maintained by just one person.<\/li>\n\n\n\n<li>Magentic isn\u2019t the only LLM framework.<\/li>\n\n\n\n<li>Doesn\u2019t have a big community around it.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What are some Magentic alternatives?<\/h2>\n\n\n\n<p>Magentic alternatives are the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/langchain-ai.github.io\/langgraph\/\">LangGraph<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/docs.llamaindex.ai\/en\/stable\/\">LlamaIndex<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/microsoft.github.io\/autogen\/stable\/\">AutoGen<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.crewai.com\/\">Crewai<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/deepset-ai\/haystack\">Haystack<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Which LLMs does Magentic support?<\/h2>\n\n\n\n<p>Magentic supports these LLMs:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/openai.com\/\">OpenAI LLMs<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/ollama.com\/\">Ollama<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.anthropic.com\/\">Anthropic<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/mistral.ai\/\">Mistral<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/docs.litellm.ai\/\">LiteLLM<\/a><\/li>\n\n\n\n<li>or any other OpenAI schema-compatible model<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Getting started<\/h2>\n\n\n\n<p>To get started with Magentic, ensure that you have installed Python and obtained an API key for one of the LLM models mentioned above. I will personally go with ChatGPT as the LLM of choice.<\/p>\n\n\n\n<p>The next step will be to install Magentic into a fresh environment with the following command:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">pip install magentic<\/pre>\n\n\n\n<p>Now, we will create a&nbsp;<code>.env<\/code>&nbsp;file in which we will add needed environment variables.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">touch .env<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">MAGENTIC_BACKEND=openai\nMAGENTIC_OPENAI_API_KEY=sk-...\nMAGENTIC_OPENAI_MODEL=gpt-4<\/pre>\n\n\n\n<p>In the following headers, we will go over the basic building blocks of Magentic and create a custom agentic RAG pipeline that will use financial data and function calling to provide outputs to the user query.<\/p>\n\n\n\n<p>Let\u2019s begin.<a><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-how-to-use-magentic-prompt-decorator\">How to use Magentic prompt decorator?<\/h2>\n\n\n\n<p>To use the Magentic &nbsp;<code>@prompt<\/code>&nbsp;decorator you need to define a template for a LLM prompt as a Python function. When this function is called, the arguments are inserted into the template and the prompt is sent to an LLM which generates the function output.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from magentic import prompt\n\n@prompt(\"Explain like I'm five this financial concept: {concept}\")\ndef explain(concept: str) -&gt; str: ...\n\nexplain(\"Subprime mortgage crisis\")<\/pre>\n\n\n\n<p>\u201cThe Subprime Mortgage Crisis is like a big, grown-up version of a game of hot potato. Here\u2019s how it works:<\/p>\n\n\n\n<p>Let\u2019s say you want to buy an ice cream cone, but you don\u2019t have any money. So, a nice person (in this case, a bank) is willing to lend you money to buy your ice cream. But, they know you don\u2019t have any money to pay them back right away, which makes lending the money to you pretty risky. This would be a subprime mortgage, or loan.<\/p>\n\n\n\n<p>Now, imagine that the person lending you money to buy your ice cream gets worried because they know you may not be able to pay them back. To get rid of this problem, they decide to sell your loan to another person (an investor). This means now you owe the money to this new person, not the one who lent you the money in the first place.<\/p>\n\n\n\n<p>This makes the first person feel safe because they got their money back, but now the new person is the one who may lose money if you can\u2019t pay them back.<\/p>\n\n\n\n<p>Then, imagine this happening with not just one ice cream cone, but millions of them. Loads of people can\u2019t pay back their ice cream loans, so the new owners of these loans lose a lot of money. This big problem is like the Subprime Mortgage Crisis.<\/p>\n\n\n\n<p>Importantly, it\u2019s not really about ice cream, but about bigger expensive things like houses. When too many people couldn\u2019t pay back their loans, the banks and investors that owned those loans lost a lot of money, and this caused huge problems for the whole world\u2019s money system.\u201d<\/p>\n\n\n\n<p>Notice how the&nbsp;<code>@prompt<\/code>&nbsp;decorator plays nicely with the function and curly braces. Moreover, the function doesn\u2019t need to have a body as everything is done by the decorator.<\/p>\n\n\n\n<p>The&nbsp;<code>@prompt<\/code>&nbsp;decorator will respect the return type annotation of the decorated function. This can be&nbsp;<a href=\"https:\/\/docs.pydantic.dev\/latest\/usage\/types\/types\/\">any type supported by pydantic<\/a>&nbsp;including a&nbsp;<code>pydantic<\/code>&nbsp;model.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from magentic import prompt\nfrom pydantic import BaseModel\n\n\nclass Portfolio(BaseModel):\n    equity_etf_pct: float\n    bond_etf_pc: float\n    crypto_etf_pc: float\n    commodities_pc: float\n    reasoning: str\n\n\n@prompt(\"Create a strong portfolio of {size} allocation size.\")\ndef create_portfolio(size: str) -&gt; Portfolio: ...\n\nportfolio = create_portfolio(\"$50,000\")\nprint(portfolio)<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>equity_etf_pct=50.0<br>bond_etf_pc=30.0<br>crypto_etf_pc=10.0<br>commodities_pc=10.0<\/p>\n\n\n\n<p>reasoning=\u2019A balanced strong portfolio suitable for most risk tolerances would allocate around 50% towards Equity ETFs for growth, 30% towards Bond ETFs for income and stability, 10% towards Crypto ETFs for high-growth and high-risk appetite and 10% towards commodities for a balanced protection against inflation. The allocation size is $50,000.\u2019<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-how-to-use-magentic-chatprompt-decorator\">How to use Magentic chatprompt decorator?<\/h2>\n\n\n\n<p>To use the Magentic chatprompt decorator you will need to pass chat messages as a template rather than a single text prompt. We can also provide a system message or few-shot prompting example responses to guide the model\u2019s output.&nbsp;<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from magentic import chatprompt, AssistantMessage, SystemMessage, UserMessage\nfrom pydantic import BaseModel\n\n\nclass Quote(BaseModel):\n    quote: str\n    person: str\n\n\n@chatprompt(\n    SystemMessage(\"You are an avid reader of financial literature.\"),\n    UserMessage(\"What is your favorite quote from Warren Buffet?\"),\n    AssistantMessage(\n        Quote(\n            quote=\"Price is what you pay; value is what you get.\",\n            person=\"Warren Buffet\",\n        )\n    ),\n    UserMessage(\"What is your favorite quote from {person}?\"),\n)\ndef get_finance_quote(person: str) -&gt; Quote: ...\n\n\nget_finance_quote(\"Charlie Munger\")<\/pre>\n\n\n\n<p>In my whole life, I have known no wise people (over a broad subject matter area) who didn\u2019t read all the time \u2013 none, zero.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-how-to-do-llm-function-calling-with-magentic\">How to do LLM function calling with Magentic?<\/h2>\n\n\n\n<p>To do LLM function calling with Magentic, you will use a&nbsp;<code>@prompt<\/code>-decorated function that returns a&nbsp;<code>FunctionCall<\/code>&nbsp;object which can be called to execute the function using the arguments provided by the LLM. For example, let\u2019s ask for price data and have it use Alpha Vantage:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import os\nimport requests\nfrom magentic import prompt, FunctionCall\n\nAV_API_KEY = os.getenv(\"AV_API_KEY\")\n\ndef get_daily_price(ticker: str, api_key: str = AV_API_KEY) -&gt; dict:\n    url = f'https:\/\/www.alphavantage.co\/query?function=TIME_SERIES_DAILY&amp;symbol={ticker}&amp;apikey={api_key}'\n    r = requests.get(url)\n    data = r.json()\n    return data['Time Series (Daily)']\n\n\n@prompt(\n    \"Use the appropriate search function to answer: {question}\",\n    functions=[get_daily_price],\n)\ndef perform_search(question: str) -&gt; FunctionCall[str]: ...\n\n\noutput = perform_search(\"What is the daily price data of AAPL?\")<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">output()\n\n&gt;&gt;&gt; {'2025-01-27': {'1. open': '224.1200',\n  '2. high': '232.1500',\n  '3. low': '224.0000',\n  '4. close': '229.8600',\n  '5. volume': '94224324'}, ...<\/pre>\n\n\n\n<p>Take note that Alpha Vantage has a free tier so grabbing an API key shouldn\u2019t be an issue if you are following along. Alternatively, you can use any other preferred data provider.<a><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-how-to-use-magentic-prompt-chains\">How to use Magentic prompt chains?<\/h2>\n\n\n\n<p>You will want to use Magentic prompt chains when it is required for the LLM to do several operations before returning the final response. The&nbsp;<code>@prompt_chain<\/code>&nbsp;decorator will resolve&nbsp;<code>FunctionCall<\/code>&nbsp;objects automatically and pass the output back to the LLM to continue until the final answer is reached.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import csv\nfrom magentic import prompt_chain\n\n\ndef get_earnings_calendar(ticker: str, api_key: str = AV_API_KEY) -&gt; list:\n    url = f\"https:\/\/www.alphavantage.co\/query?function=EARNINGS_CALENDAR&amp;symbol={ticker}&amp;horizon=12month&amp;apikey={api_key}\"\n    with requests.Session() as s:\n        download = s.get(url)\n        decoded_content = download.content.decode('utf-8')\n        cr = csv.reader(decoded_content.splitlines(), delimiter=',')\n        my_list = list(cr)\n    return my_list\n\n\n@prompt_chain(\n    \"What's {ticker} expected earnings dates for the next 12 months?\",\n    functions=[get_earnings_calendar],\n)\ndef get_earnings(ticker: str) -&gt; str: ...\n\n\nget_earnings(\"IBM\")<\/pre>\n\n\n\n<p>\u2018The expected earnings dates for IBM for the fiscal year 2025 are as follows:<\/p>\n\n\n\n<p>\u2013 On April 22, 2025 for fiscal date ending March 31, 2025<\/p>\n\n\n\n<p>\u2013 On July 22, 2025 for fiscal date ending June 30, 2025<\/p>\n\n\n\n<p>Please note that these dates are for fiscal year 2025 and the exact figures for the earnings are not available yet. These dates are expected and may change. The currency for these earnings is in USD.\u2019<\/p>\n\n\n\n<p>The prompt chains are the \u201cbread and butter\u201d of building agentic workflows as they allow us to create a loop where the LLM can perform multiple API calls until it collects all the information it needs to provide an answer.<\/p>\n\n\n\n<p>With some good prompting techniques and a good pipeline, one can perform many complex tasks.<\/p>\n\n\n\n<p><em>Visit <a href=\"https:\/\/algotrading101.com\/learn\/magentic-llm-guide\/\">AlgoTrading101<\/a>&nbsp;blog to read the full article and for details on streaming LLM responses with Magentic.<\/em><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magentic is an open-source framework that allows for the seamless integration of Large Language Models (LLMs) into Python code.<\/p>\n","protected":false},"author":815,"featured_media":49193,"comment_status":"open","ping_status":"closed","sticky":true,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[339,338,341],"tags":[851,864,14403,865,17952,18486,18487,595],"contributors-categories":[13746],"class_list":{"0":"post-218576","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-science","8":"category-ibkr-quant-news","9":"category-quant-development","10":"tag-algo-trading","11":"tag-api","12":"tag-chatgpt","13":"tag-github","14":"tag-large-language-models-llms","15":"tag-magentic","16":"tag-pydantic","17":"tag-python","18":"contributors-categories-algotrading101"},"pp_statuses_selecting_workflow":false,"pp_workflow_action":"current","pp_status_selection":"publish","acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.9 (Yoast SEO v27.7) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>How to Build LLM Agents with Magentic | IBKR Quant<\/title>\n<meta name=\"description\" content=\"Magentic is an open-source framework that allows for the seamless integration of Large Language Models (LLMs) into Python code.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/218576\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Build LLM Agents with Magentic\" \/>\n<meta property=\"og:description\" content=\"Magentic is an open-source framework that allows for the seamless integration of Large Language Models (LLMs) into Python code.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-build-llm-agents-with-magentic\/\" \/>\n<meta property=\"og:site_name\" content=\"IBKR Campus US\" \/>\n<meta property=\"article:published_time\" content=\"2025-02-11T18:58:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2020\/06\/python-language-hand.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"900\" \/>\n\t<meta property=\"og:image:height\" content=\"550\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Igor Radovanovic\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Igor Radovanovic\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\n\t    \"@context\": \"https:\\\/\\\/schema.org\",\n\t    \"@graph\": [\n\t        {\n\t            \"@type\": \"NewsArticle\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/how-to-build-llm-agents-with-magentic\\\/#article\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/how-to-build-llm-agents-with-magentic\\\/\"\n\t            },\n\t            \"author\": {\n\t                \"name\": \"Igor Radovanovic\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/b43b33f424bad38d84a7b78eb0193592\"\n\t            },\n\t            \"headline\": \"How to Build LLM Agents with Magentic\",\n\t            \"datePublished\": \"2025-02-11T18:58:54+00:00\",\n\t            \"mainEntityOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/how-to-build-llm-agents-with-magentic\\\/\"\n\t            },\n\t            \"wordCount\": 1136,\n\t            \"commentCount\": 0,\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/how-to-build-llm-agents-with-magentic\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2020\\\/06\\\/python-language-hand.jpg\",\n\t            \"keywords\": [\n\t                \"Algo Trading\",\n\t                \"API\",\n\t                \"ChatGPT\",\n\t                \"GitHub\",\n\t                \"Large Language Models (LLMs)\",\n\t                \"Magentic\",\n\t                \"Pydantic\",\n\t                \"Python\"\n\t            ],\n\t            \"articleSection\": [\n\t                \"Data Science\",\n\t                \"Quant\",\n\t                \"Quant Development\"\n\t            ],\n\t            \"inLanguage\": \"en-US\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"CommentAction\",\n\t                    \"name\": \"Comment\",\n\t                    \"target\": [\n\t                        \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/how-to-build-llm-agents-with-magentic\\\/#respond\"\n\t                    ]\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"WebPage\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/how-to-build-llm-agents-with-magentic\\\/\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/how-to-build-llm-agents-with-magentic\\\/\",\n\t            \"name\": \"How to Build LLM Agents with Magentic | IBKR Campus US\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\"\n\t            },\n\t            \"primaryImageOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/how-to-build-llm-agents-with-magentic\\\/#primaryimage\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/how-to-build-llm-agents-with-magentic\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2020\\\/06\\\/python-language-hand.jpg\",\n\t            \"datePublished\": \"2025-02-11T18:58:54+00:00\",\n\t            \"description\": \"Magentic is an open-source framework that allows for the seamless integration of Large Language Models (LLMs) into Python code.\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"ReadAction\",\n\t                    \"target\": [\n\t                        \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/how-to-build-llm-agents-with-magentic\\\/\"\n\t                    ]\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"ImageObject\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/how-to-build-llm-agents-with-magentic\\\/#primaryimage\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2020\\\/06\\\/python-language-hand.jpg\",\n\t            \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2020\\\/06\\\/python-language-hand.jpg\",\n\t            \"width\": 900,\n\t            \"height\": 550,\n\t            \"caption\": \"Python\"\n\t        },\n\t        {\n\t            \"@type\": \"WebSite\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"name\": \"IBKR Campus US\",\n\t            \"description\": \"Financial Education from Interactive Brokers\",\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"SearchAction\",\n\t                    \"target\": {\n\t                        \"@type\": \"EntryPoint\",\n\t                        \"urlTemplate\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/?s={search_term_string}\"\n\t                    },\n\t                    \"query-input\": {\n\t                        \"@type\": \"PropertyValueSpecification\",\n\t                        \"valueRequired\": true,\n\t                        \"valueName\": \"search_term_string\"\n\t                    }\n\t                }\n\t            ],\n\t            \"inLanguage\": \"en-US\"\n\t        },\n\t        {\n\t            \"@type\": \"Organization\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\",\n\t            \"name\": \"Interactive Brokers\",\n\t            \"alternateName\": \"IBKR\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"logo\": {\n\t                \"@type\": \"ImageObject\",\n\t                \"inLanguage\": \"en-US\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\",\n\t                \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"width\": 669,\n\t                \"height\": 669,\n\t                \"caption\": \"Interactive Brokers\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\"\n\t            },\n\t            \"publishingPrinciples\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/about-ibkr-campus\\\/\",\n\t            \"ethicsPolicy\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/cyber-security-notice\\\/\"\n\t        },\n\t        {\n\t            \"@type\": \"Person\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/b43b33f424bad38d84a7b78eb0193592\",\n\t            \"name\": \"Igor Radovanovic\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/author\\\/igor-radovanovic\\\/\"\n\t        }\n\t    ]\n\t}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to Build LLM Agents with Magentic | IBKR Quant","description":"Magentic is an open-source framework that allows for the seamless integration of Large Language Models (LLMs) into Python code.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/218576\/","og_locale":"en_US","og_type":"article","og_title":"How to Build LLM Agents with Magentic","og_description":"Magentic is an open-source framework that allows for the seamless integration of Large Language Models (LLMs) into Python code.","og_url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/how-to-build-llm-agents-with-magentic\/","og_site_name":"IBKR Campus US","article_published_time":"2025-02-11T18:58:54+00:00","og_image":[{"width":900,"height":550,"url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2020\/06\/python-language-hand.jpg","type":"image\/jpeg"}],"author":"Igor Radovanovic","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Igor Radovanovic","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"NewsArticle","@id":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/how-to-build-llm-agents-with-magentic\/#article","isPartOf":{"@id":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/how-to-build-llm-agents-with-magentic\/"},"author":{"name":"Igor Radovanovic","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/b43b33f424bad38d84a7b78eb0193592"},"headline":"How to Build LLM Agents with Magentic","datePublished":"2025-02-11T18:58:54+00:00","mainEntityOfPage":{"@id":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/how-to-build-llm-agents-with-magentic\/"},"wordCount":1136,"commentCount":0,"publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"image":{"@id":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/how-to-build-llm-agents-with-magentic\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2020\/06\/python-language-hand.jpg","keywords":["Algo Trading","API","ChatGPT","GitHub","Large Language Models (LLMs)","Magentic","Pydantic","Python"],"articleSection":["Data Science","Quant","Quant Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/how-to-build-llm-agents-with-magentic\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/how-to-build-llm-agents-with-magentic\/","url":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/how-to-build-llm-agents-with-magentic\/","name":"How to Build LLM Agents with Magentic | IBKR Campus US","isPartOf":{"@id":"https:\/\/ibkrcampus.com\/campus\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/how-to-build-llm-agents-with-magentic\/#primaryimage"},"image":{"@id":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/how-to-build-llm-agents-with-magentic\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2020\/06\/python-language-hand.jpg","datePublished":"2025-02-11T18:58:54+00:00","description":"Magentic is an open-source framework that allows for the seamless integration of Large Language Models (LLMs) into Python code.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/how-to-build-llm-agents-with-magentic\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/how-to-build-llm-agents-with-magentic\/#primaryimage","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2020\/06\/python-language-hand.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2020\/06\/python-language-hand.jpg","width":900,"height":550,"caption":"Python"},{"@type":"WebSite","@id":"https:\/\/ibkrcampus.com\/campus\/#website","url":"https:\/\/ibkrcampus.com\/campus\/","name":"IBKR Campus US","description":"Financial Education from Interactive Brokers","publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ibkrcampus.com\/campus\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/ibkrcampus.com\/campus\/#organization","name":"Interactive Brokers","alternateName":"IBKR","url":"https:\/\/ibkrcampus.com\/campus\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","width":669,"height":669,"caption":"Interactive Brokers"},"image":{"@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/"},"publishingPrinciples":"https:\/\/www.interactivebrokers.com\/campus\/about-ibkr-campus\/","ethicsPolicy":"https:\/\/www.interactivebrokers.com\/campus\/cyber-security-notice\/"},{"@type":"Person","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/b43b33f424bad38d84a7b78eb0193592","name":"Igor Radovanovic","url":"https:\/\/www.interactivebrokers.com\/campus\/author\/igor-radovanovic\/"}]}},"jetpack_featured_media_url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2020\/06\/python-language-hand.jpg","_links":{"self":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/218576","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/users\/815"}],"replies":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/comments?post=218576"}],"version-history":[{"count":0,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/218576\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media\/49193"}],"wp:attachment":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media?parent=218576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/categories?post=218576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/tags?post=218576"},{"taxonomy":"contributors-categories","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/contributors-categories?post=218576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}