{"templateId":"../@theme/templates/BlogPost","sharedDataIds":{"blog-latest-posts":"blog-latest-posts"},"props":{"metadata":{"markdoc":{"tagList":[]},"redocly_category":"Blog","type":"markdown","template":"../@theme/templates/BlogPost","title":"Code mode for MCP: up to 80% lower LLM costs","description":"Redocly's MCP server lets AI agents combine documentation calls in a script and return only what matters. Less context to process. Lower LLM costs.","seo":{"title":"Code mode for MCP: up to 80% lower LLM costs | Redocly","description":"Code mode brings JavaScript to Redocly's MCP server. Agents combine calls, filter results, and answer API questions with up to 80% lower LLM cost.","image":"./images/mcp-code-mode.png"},"author":{"id":"vasyl-havronskyi","name":"Vasyl Havronskyi","authorBIO":"Software Engineer, Redocly","image":"/assets/havronskyi.7e2eb7cdb1172a19fd1b53cefc64b4bb42ee5582add905a2dce13e1bb4ad081e.978384e4.png"},"publishedDate":"2026-09-17","categories":[{"category":{"id":"redocly","label":"Redocly"},"subcategory":{"id":"product-updates","label":"Product updates"}},{"category":{"id":"technical-documentation","label":"Technical documentation"},"subcategory":{"id":"ai-assisted-docs","label":"AI-assisted documentation"}},{"category":{"id":"api-catalog","label":"API catalog"},"subcategory":{"id":"discovery","label":"Discovery"}}],"image":"/assets/mcp-code-mode.5a405ccc180123f0ba8952796f39d5291c3542ff7af6c2e94e8c63fbe16f955d.978384e4.png","slug":"/blog/mcp-code-mode"},"seo":{"title":"Code mode for MCP: up to 80% lower LLM costs | Redocly","description":"Code mode brings JavaScript to Redocly's MCP server. Agents combine calls, filter results, and answer API questions with up to 80% lower LLM cost.","siteUrl":"https://redocly.com","image":"/assets/mcp-code-mode.5a405ccc180123f0ba8952796f39d5291c3542ff7af6c2e94e8c63fbe16f955d.978384e4.png","lang":"en-US"},"dynamicMarkdocComponents":[],"compilationErrors":[],"ast":{"$$mdtype":"Tag","name":"article","attributes":{},"children":[{"$$mdtype":"Tag","name":"Heading","attributes":{"level":1,"id":"code-mode-for-mcp-up-to-80-lower-llm-costs","__idx":0},"children":["Code mode for MCP: up to 80% lower LLM costs"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Ask an AI agent a question about your API documentation:"]},{"$$mdtype":"Tag","name":"blockquote","attributes":{},"children":[{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Which operations across these three APIs don't document a 400 response?"]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The answer might be five lines."," ","Getting there takes more work: find the APIs, inspect their operations, check the responses, and collect the exceptions."," ","With ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["direct tool calling"]},", the LLM reads the results as the agent works through the task."," ","Most of that information will never appear in the answer."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["Code mode"]}," lets the agent write a JavaScript script to do that work."," ","The script calls the documentation tools, checks their results, and returns the findings."," ","You ask the question in ordinary language."," ","The agent writes and runs the code."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Redocly's MCP server now supports this approach."," ","In our measurements, code mode reduced LLM costs by ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["up to 80%"]},", making it ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["up to 5x cheaper"]},", with equivalent or better answer quality."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Here's what changes, and why it matters for your APIs."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"how-code-mode-works","__idx":1},"children":["How code mode works"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Model Context Protocol connects AI applications to tools and data."," ","A documentation tool can list APIs, describe an operation, or retrieve its security requirements."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["With ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["direct tool calling"]},", the LLM chooses a tool and supplies its arguments."," ","The result enters its context, the information it can use to produce its next response."," ","For a task with several dependent steps, the LLM reads each result and decides what to call next."," ","Those results consume input tokens and can be processed again in later steps."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["Code mode"]}," gives the LLM another way to use those same capabilities."," ","It writes the sequence as code, including loops, conditions, and calculations."," ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://blog.cloudflare.com/code-mode/"},"children":["Cloudflare pioneered code mode for MCP"]},", and ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://www.anthropic.com/engineering/code-execution-with-mcp"},"children":["Anthropic has described the benefits of code execution with MCP"]},"."," ","Redocly brings that approach to your documentation."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Our MCP server exposes two tools for this:"]},{"$$mdtype":"Tag","name":"ul","attributes":{},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["describe-tools"]}]}," returns the signatures of the documentation functions, including inputs and result types."]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["execute"]}]}," runs the agent's JavaScript in a sandbox on the server."]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The documentation functions live inside that sandbox on a ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["tools"]}," object."," ","The script calls them directly, for example ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["tools.getFullApiDescription()"]},"."," ","Only what the script returns reaches the LLM, no matter how much documentation it read."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"code-mode-in-action","__idx":2},"children":["Code mode in action"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Consider that question about missing 400 responses."," ","A direct tool workflow can list each API's endpoints, request the details of each operation, and pass those results to the LLM for inspection."," ","Code mode can combine the retrieval and inspection in one script."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"img","attributes":{"src":"/assets/mcp-code-mode-flow.697625f489df04416c2ae67cfc73935008fb44a0f99f6cc98f64dc6246e96e1c.978384e4.png","alt":"Direct tool calling passes API lists, endpoint lists, and operation details through the LLM. Code mode retrieves and filters the documentation inside a server sandbox, then returns only the findings."},"children":[]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Here is an example script for three APIs, using their full descriptions:"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"javascript","header":{"controls":{"copy":{}}},"source":"const names = ['Redocly Museum API', 'Warp API', 'Portal API Functions'];\nconst methods = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace'];\nconst findings = [];\n\nfor (const name of names) {\n  const { definition } = await tools.getFullApiDescription({ name });\n\n  for (const [path, item] of Object.entries(definition.paths ?? {})) {\n    for (const method of methods) {\n      const operation = item[method];\n      if (operation && !operation.responses?.['400']) {\n        findings.push({\n          api: name,\n          method: method.toUpperCase(),\n          path,\n          operationId: operation.operationId,\n        });\n      }\n    }\n  }\n}\n\nreturn findings;\n","lang":"javascript"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The script reads three API descriptions."," ","Only the matching operations come back."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["For this audit, Claude Sonnet 5 answered correctly in both modes:"]},{"$$mdtype":"Tag","name":"div","attributes":{"className":"md-table-wrapper"},"children":[{"$$mdtype":"Tag","name":"table","attributes":{"className":"md"},"children":[{"$$mdtype":"Tag","name":"thead","attributes":{},"children":[{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"th","attributes":{"data-label":"Average per audit"},"children":["Average per audit"]},{"$$mdtype":"Tag","name":"th","attributes":{"data-label":"Direct tool calling"},"children":["Direct tool calling"]},{"$$mdtype":"Tag","name":"th","attributes":{"data-label":"Code mode"},"children":["Code mode"]}]}]},{"$$mdtype":"Tag","name":"tbody","attributes":{},"children":[{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Client-visible tool calls"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["39"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["3.5"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":["Input tokens"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["114,000"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["22,500"]}]},{"$$mdtype":"Tag","name":"tr","attributes":{},"children":[{"$$mdtype":"Tag","name":"td","attributes":{},"children":["LLM cost"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["$0.27"]},{"$$mdtype":"Tag","name":"td","attributes":{},"children":["$0.06"]}]}]}]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["That is ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["91% fewer client-visible calls, 80% fewer input tokens, and 79% lower cost"]},", with the same or better answer quality."," ","The agent may use more than one script to finish a task, and calls inside each script still run."," ","It no longer needs a separate LLM decision for every step."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"why-code-makes-the-difference","__idx":3},"children":["Why code makes the difference"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["One tool per API operation creates an obvious scaling problem."," ","An API with hundreds of operations brings hundreds of tool definitions for the agent to navigate."," ","Adding APIs expands the list further."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Redocly's documentation tools already cover many operations through a small set of functions."," ","Code mode adds something that reducing the tool count alone cannot: the ability to compose those functions and process their results."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["The LLM can use its coding skills."]}," ","JavaScript gives it familiar ways to express a task: loop through the APIs, check a field, collect the matches."," ","A new question can become a new script without your team building another specialized MCP tool."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["The script controls what enters context."]}," ","An operation's details can include parameters, schemas, examples, and descriptions."," ","A response-code audit needs only a fraction of that information."," ","Filtering it on the server leaves more context available for the developer's actual task."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["Code handles the counting."]}," ","If the question asks how many operations match, the script can return ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["findings.length"]},"."," ","The JavaScript runtime computes the total from the collected results."," ","The LLM can explain the findings without having to tally a long list during inference."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["These benefits apply to inventories, comparisons between API versions, authentication reviews, and other questions that span reference pages."," ","Your documentation becomes something an agent can work through systematically."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"savings-across-five-models","__idx":4},"children":["Savings across five models"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The benefit extends beyond that audit."," ","Across a range of API documentation questions, code mode saved ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["50–90% of input tokens"]}," and reduced LLM costs by ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["roughly 45–80%"]},", depending on the model."," ","Measured answer quality was equivalent or better."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"img","attributes":{"src":"/assets/mcp-code-mode-savings.1b7d9ad6db8b0c5140198d5f06f4d069161cd1b47661bed20f9ec42247fb3270.978384e4.png","alt":"Input-token savings and LLM cost reductions with code mode: Gemini 3 Flash, 60% and 45%; Gemini 3.8 Flash, 90% and 79%; Claude Haiku 4.5, 59% and 51%; Claude Sonnet 5, 50% and 47%; Claude Opus 5, 78% and 72%."},"children":[]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["A simple lookup can cost about the same in either mode."," ","The biggest savings come when a question needs more retrieval and filtering."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Production usage also shows smaller responses."," ","Across a month of real-world usage, code mode sent about a third as much data back into the agent's context as tool calling did."," ","The number of round trips dropped, while the quality of the answers either improved or maintained its baseline."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"how-we-measured-it","__idx":5},"children":["How we measured it"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["We compared token usage, LLM cost, and answer quality across the five models shown above."," ","The questions covered endpoint lookups, authentication, API comparisons, inventories, and response audits."," ","For the chart, each question started a fresh conversation and used the same OpenAPI descriptions in both modes."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["We repeated the questions and checked answers against expected facts using automated checks and LLM judges."," ","The percentages compare totals of the per-question averages."," ","Costs include both input and output tokens at the rates used for the measurements, with cache discounts where reported."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"try-it","__idx":6},"children":["Try it"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Code mode is enabled for all organizations using Redocly's MCP server."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Redocly's AI Assistant also uses code mode to answer API questions directly in your docs."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Open ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["/mcp"]}," on your Redocly project and follow the setup instructions to connect your AI tool."," ","You can also explore ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://redocly.com/mcp"},"children":["Redocly's own MCP server"]}," or read the ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://redocly.com/docs/realm/customization/mcp-server"},"children":["connection guide"]},"."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Then ask something that spans your documentation:"]},{"$$mdtype":"Tag","name":"ul","attributes":{},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":["Which operations are deprecated?"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["What changed between these two API versions?"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["Which operations don't document a 400 response?"]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Your users bring the questions."," ","Code mode gives their agents a cheaper way to work out the answers."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Next, we're extending code mode so agents can call your APIs, too."," ","Stay tuned."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Useful links:"]},{"$$mdtype":"Tag","name":"ul","attributes":{},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://redocly.com/docs/realm/customization/mcp-server"},"children":["Redocly MCP server setup guide"]}]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://redocly.com/mcp"},"children":["Try Redocly's MCP server"]}]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://blog.cloudflare.com/code-mode/"},"children":["Code mode: the better way to use MCP, by Cloudflare"]}]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://www.anthropic.com/engineering/code-execution-with-mcp"},"children":["Code execution with MCP, by Anthropic"]}]}]}]},"headings":[{"value":"Code mode for MCP: up to 80% lower LLM costs","id":"code-mode-for-mcp-up-to-80-lower-llm-costs","depth":1},{"value":"How code mode works","id":"how-code-mode-works","depth":2},{"value":"Code mode in action","id":"code-mode-in-action","depth":2},{"value":"Why code makes the difference","id":"why-code-makes-the-difference","depth":2},{"value":"Savings across five models","id":"savings-across-five-models","depth":2},{"value":"How we measured it","id":"how-we-measured-it","depth":2},{"value":"Try it","id":"try-it","depth":2}],"frontmatter":{"template":"../@theme/templates/BlogPost","title":"Code mode for MCP: up to 80% lower LLM costs","description":"Redocly's MCP server lets AI agents combine documentation calls in a script and return only what matters. Less context to process. Lower LLM costs.","seo":{"title":"Code mode for MCP: up to 80% lower LLM costs | Redocly","description":"Code mode brings JavaScript to Redocly's MCP server. Agents combine calls, filter results, and answer API questions with up to 80% lower LLM cost.","image":"/assets/mcp-code-mode.5a405ccc180123f0ba8952796f39d5291c3542ff7af6c2e94e8c63fbe16f955d.978384e4.png"},"author":"vasyl-havronskyi","publishedDate":"2026-09-17","categories":["redocly:product-updates","technical-documentation:ai-assisted-docs","api-catalog:discovery"],"image":"mcp-code-mode.png"},"lastModified":"2026-09-17T13:57:00.000Z","pagePropGetterError":{"message":"","name":""}},"slug":"/blog/mcp-code-mode","userData":{"isAuthenticated":false,"teams":["anonymous"]},"isPublic":true}