{"record":{"id":"834980f2746ce34e","repo":"vercel/ai","slug":"code-mode-source-too-large","errorCode":"CODE_MODE_SOURCE_TOO_LARGE","errorMessage":"Code mode source exceeds the ${maxBytes} byte size limit.","messagePattern":"Code mode source exceeds the (.+?) byte size limit\\.","errorType":"exception","errorClass":"CodeModeSourceTooLargeError","httpStatus":null,"severity":"error","filePath":"packages/code-mode/src/run-code-mode.ts","lineNumber":200,"sourceCode":"    maxConsoleOutputBytes:\n      policy.maxConsoleOutputBytes ?? DEFAULT_MAX_CONSOLE_OUTPUT_BYTES,\n    maxSourceBytes: policy.maxSourceBytes ?? DEFAULT_MAX_SOURCE_BYTES,\n    maxToolInputBytes: policy.maxToolInputBytes ?? DEFAULT_MAX_TOOL_INPUT_BYTES,\n    maxToolOutputBytes:\n      policy.maxToolOutputBytes ?? DEFAULT_MAX_TOOL_OUTPUT_BYTES,\n    maxBridgeRequests: policy.maxBridgeRequests ?? DEFAULT_MAX_BRIDGE_REQUESTS,\n    maxInFlightBridgeRequests:\n      policy.maxInFlightBridgeRequests ?? DEFAULT_MAX_IN_FLIGHT_BRIDGE_REQUESTS,\n  };\n}\n\nfunction assertSourceSize(source: string, maxBytes: number): void {\n  if (!isValidRunLimit(maxBytes)) {\n    return;\n  }\n  const bytes = Buffer.byteLength(source);\n  if (bytes > maxBytes) {\n    throw new CodeModeSourceTooLargeError(bytes, maxBytes);\n  }\n}\n\nfunction createCodeModeSource(js: string, toolNames: string[]): string {\n  const bindings = Object.fromEntries(\n    toolNames.map((toolName, index) => [toolName, `__codeMode.tool${index}`]),\n  );\n  const bindingSource = Object.entries(bindings)\n    .map(([name, reference]) => `${JSON.stringify(name)}:${reference}`)\n    .join(',');\n  return `const __codeModeBindings={${bindingSource}};const tools=new Proxy(Object.create(null),{get(_target,name){const binding=__codeModeBindings[name];return typeof binding===\"function\"?(input)=>binding(input):(input)=>__codeMode.missing(String(name),input);}});const __codeModeResult=await(async()=>{\\n${js}\\n})();if(__codeModeResult===undefined)return undefined;return JSON.parse(JSON.stringify(__codeModeResult));`;\n}\n\nfunction createHostFunctions({\n  codeModeErrors,\n  input,\n  outerToolCallId,\n  policy,","sourceCodeStart":182,"sourceCodeEnd":218,"githubUrl":"https://github.com/vercel/ai/blob/69428b1f8b037e4d118fb4853428d5c4e620493c/packages/code-mode/src/run-code-mode.ts#L182-L218","documentation":"runCodeMode enforces an execution policy limit maxSourceBytes (default 256 KiB) on the JavaScript source passed to the sandbox. assertSourceSize measures the source in bytes (Buffer.byteLength, so multibyte characters count fully) and throws CodeModeSourceTooLargeError with actual and maximum byte counts when the limit is exceeded. The check is skipped only when maxSourceBytes is not a valid positive integer run limit.","triggerScenarios":"Calling runCodeMode (or the code-mode tool) with input.js larger than policy.maxSourceBytes — most often when code is generated by an LLM or assembled by concatenating many helper snippets.","commonSituations":"LLM-generated code rambling past the limit; embedding large embedded data blobs (base64 strings) in source instead of passing them as tool inputs; setting a very low maxSourceBytes in executionPolicy for safety and hitting it with normal code.","solutions":["Reduce the size of the generated source: split the work into multiple smaller code-mode invocations.","Move large constant data out of the source and fetch it via a host tool at runtime.","Raise maxSourceBytes in options.executionPolicy if the default 256 KiB is too small for your workload."],"exampleFix":"// before\nawait runCodeMode({ js: codeWithDataBlob, tools });\n// after\nawait runCodeMode({\n  js: codeThatCallsGetDataTool,\n  tools: { ...tools, getData },\n  options: { executionPolicy: { maxSourceBytes: 512 * 1024 } },\n});","handlingStrategy":"validation","validationCode":"import { Buffer } from 'node:buffer';\nconst maxBytes = 256 * 1024; // match executionPolicy.maxSourceBytes\nif (Buffer.byteLength(js, 'utf8') > maxBytes) {\n  throw new Error(`code-mode source is ${Buffer.byteLength(js)} bytes, limit is ${maxBytes}`);\n}","typeGuard":null,"tryCatchPattern":"try {\n  await runCodeMode({ js, tools, options });\n} catch (error) {\n  if (CodeModeSourceTooLargeError.isInstance(error)) {\n    // error.byteLength / error.maxBytes — split the source or raise the limit\n  } else throw error;\n}","preventionTips":["Check source size before invoking when code is LLM-generated or concatenated.","Keep large data out of source strings — fetch it via host tools.","Align your executionPolicy.maxSourceBytes with realistic generated code sizes."],"tags":["code-mode","size-limit","execution-policy","resource-limit"],"backgroundTag":"source-size-limit-exceeded","analyzedSha":"69428b1f8b037e4d118fb4853428d5c4e620493c","analyzedAt":"2026-08-30T12:32:21.016Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}