Reducing Agent Costs and Eliminating Rate-Limit Failures with Sandboxed Code Execution

port_of_context · Tech · 2026 · source Agentic: yes · promotional bias: med · source-backed · extracted with prompt 0.5.6 (schema v2)

Generated from extraction.json — do not edit by hand. Every claim cites verbatim source evidence; stated means the quote directly asserts the claim, implied means it supports it indirectly.

Problem

Domain: Production GTM intelligence agents over data-heavy developer sources

Port of Context needed a scheduled GTM intelligence agent to search GitHub and Hacker News across multiple exact terms, aggregate large result sets, and post Slack digests without rate-limit failures or excessive model-context cost.

Constraints:

Architecture

Port of Context built a GTM agent on goose, a custom gtmdata MCP server, Arcade.dev runtime, and pctx Code Mode. The MCP server exposes typed tools for GitHub issue/PR/repository search, Hacker News search, and Slack posting. With Code Mode on, the agent writes one TypeScript program, runs parallel MCP calls inside a permissioned sandbox, keeps intermediate 100k-token payloads out of the model context, and returns only the final digest. The standard loop was evaluated as a baseline and failed more often due to repeated JSON payloads re-entering context.

Diagram confidence: explicit (topology described by the source).

flowchart LR
  A["Scheduled GTM prompt"] --> B["goose agent harness"]
  B --> C["pctx Code Mode sandbox"]
  B --> D["gtmdata MCP server"]
  C --> D
  D --> E["Arcade.dev runtime"]
  E --> F["GitHub APIs"]
  D --> G["Hacker News search"]
  E --> H["Slack channel"]
  B --> I["Model provider"]
Drag to pan · scroll or use +/− to zoom · reconstructed from the source's described component topology

Components

Component Role Interacts with
Scheduled GTM prompt Run request specifying exact terms, sources, time window, and Slack digest delivery. goose agent harness
goose agent harness Open-source agent harness that dispatches standard tool calls or Code Mode execution. Scheduled GTM prompt, pctx Code Mode sandbox, gtmdata MCP server
pctx Code Mode sandbox Deno TypeScript execution environment with type-checking, timeout, permissions, and network allow-list. goose agent harness, gtmdata MCP server
gtmdata MCP server Custom MCP server exposing five GTM search and Slack-posting tools. goose agent harness, pctx Code Mode sandbox, Arcade.dev runtime, GitHub APIs, Hacker News search, Slack channel
Arcade.dev runtime Typed tool runtime, managed OAuth, and policy enforcement layer. gtmdata MCP server, GitHub APIs, Slack channel
GitHub APIs Issues, pull requests, and repositories searched for specific terms. gtmdata MCP server, Arcade.dev runtime
Hacker News search Thread/source search for GTM signals. gtmdata MCP server
Slack channel Destination for structured GTM digests. gtmdata MCP server, Arcade.dev runtime
Model provider Configurable model backend used in the same harness across Sonnet, Opus, and GPT. goose agent harness

Data flow

A scheduled prompt asks the agent to find exact-term mentions across sources and post a digest. In Code Mode, goose has the model emit a TypeScript program. pctx type-checks and runs it inside a Deno sandbox, calling gtmdata's typed MCP tools, often in parallel with Promise.all. GitHub and Hacker News result payloads stay inside the sandbox; the program reduces them into a Slack-ready digest and calls the Slack tool. Only the reduced final result returns to model context.

3 supporting quotes
“The agent writes a small TypeScript program, calls MCP tools as typed function calls inside the sandbox, and only the final result returns to the conversation.”article, stated
“In Code Mode, the agent writes a single sandboxed TypeScript program that batches every MCP call into one Promise.all and chains the result straight into a Slack post.”article, stated
“Tool result payloads stay inside the sandbox; only the final summary returns to the model.”article, stated

Why this architecture

Code Mode was chosen because the workload is data-heavy, multi-tool, and scheduled. Moving intermediate tool payloads into a typed sandbox avoids replaying every JSON result through the model context, which directly reduces tokens, cost, and rate-limit failures while preserving the same MCP tool surface.

3 supporting quotes
“We had a hypothesis that the sandboxed (Code Mode) execution we built would be more reliable and more token-efficient under exactly this kind of multi-source, continuous workload.”article, stated
“four search payloads totaling 100k+ tokens never reach the model’s context window. The sandbox consumes them.”article, stated
“Code Mode doesn’t change what the agent does. It changes where the data lives during the run”article, stated

Models used: Sonnet 4-5 · Opus 4-6 · GPT-5-4

Scale

Alternatives considered (roads not taken)

Goose's standard tool-use loop

Rejected because: Each tool result re-entered the model context as a large JSON message, driving token growth and per-minute rate-limit failures.

3 supporting quotes
“the same task plays out as a back-and-forth tool-use loop instead.”article, stated
“each tool call returning a 15–30 KB JSON payload that re-enters the model’s context as a message.”article, stated
“That’s where most OFF runs hit the per-minute rate cap.”article, stated

Anthropic Programmatic Tool Calling

Rejected because: The experiment needed BYO-LLM comparison across Claude and GPT; Anthropic PTC is Claude-only.

“Anthropic’s PTC is structurally Claude-only, so we could not have measured GPT-5-4’s framing bug or Sonnet’s phrase-search miss in the same harness.”article, stated

Cloudflare Code Mode or Pydantic Monty

Rejected because: Those implementations are tied to specific platforms/agent abstractions, while pctx kept goose as the harness and made the model a config knob.

“With pctx the agent harness is goose, the execution is pctx, and the model is a config knob.”article, stated
“pctx is the right pick when you need BYO-LLM (any model, any harness that speaks MCP)”article, stated

Patterns

tool-use-orchestration (architecture)

The agent calls MCP tools for GitHub issues, PRs, repositories, Hacker News, and Slack, either through a classic loop or through one sandboxed TypeScript program.

“A custom-built MCP server (“gtmdata”) which exposes five tools the agent can call to search GitHub issues, pull requests, and repos”article, stated
“The model emits one program that calls tools as typed functions inside a sandbox. The sandbox swallows the intermediate payloads.”article, stated

structured-output-contract (implementation)

Typed MCP tool schemas and TypeScript pre-flight checks let Code Mode chain calls as typed function calls and surface diagnostics before runtime.

“The typed-output-schema design baked into the server (full input and output schemas, returned through Arcade’s runtime) is what lets Code Mode chain calls together as cleanly as it does”article, stated
“Typescript type-checks the program before any tool fires. Errors come back as Typescript diagnostics, not as runtime crashes.”article, stated

cost-aware-routing (serving)

The execution mode choice routes data-heavy workloads into a sandbox to cut model input tokens and per-run cost.

“* Average cost per run: $0.20 ON vs $0.41 OFF, roughly 2× cheaper.”article, stated
“* Average input tokens per run: 31.8k vs 93.6k, roughly 3× more efficient.”article, stated

context-engineering (context)

Port of Context positions itself as building the context engineering and execution layer, and Code Mode keeps large tool payloads out of model context by moving them into sandboxed execution.

“Port of Context (portofcontext.com) builds the context engineering and execution layer for production agentic AI.”article, stated
“Tool result payloads stay inside the sandbox; only the final summary returns to the model.”article, stated

latency-optimization (serving)

Parallel Promise.all execution and sandbox-side reduction avoid serial model/tool turns and remove the rate-limit cliff caused by repeated large context payloads.

“In Code Mode, the agent writes a single sandboxed TypeScript program that batches every MCP call into one Promise.all and chains the result straight into a Slack post.”article, stated
“Every implementation cuts input tokens 2 to 3 times and removes the rate-limit cliff the classic tool-use loop hits on data-heavy workloads.”article, stated

Tradeoffs

Execution mode

Chose Code Mode sandboxed TypeScript over standard tool-use loop.

Rationale: The sandbox holds intermediate payloads and executes tools in parallel, making runs cheaper and more reliable.

“Code Mode ON (sandbox execution) Model writes one program. Sandbox runs all tools, often in parallel”article, stated
“* 100% delivery with Code Mode ON. 56% with it off. Every rate-limit failure in the experiment was an OFF-mode run.”article, stated

Runtime portability

Chose pctx BYO-LLM in goose over Claude-only or platform-specific Code Mode.

Rationale: The team needed to compare model families and run the same workload without changing the harness.

“A core motivation for the case study was running the same workload across three model families. Anthropic’s PTC is structurally Claude-only, so we could not have measured GPT-5-4’s framing bug or Sonnet’s phrase-search miss in the same harness.”article, stated
“BYO-LLM. Any model that speaks MCP. We ran the same workload across Sonnet 4-5, Opus 4-6, and GPT-5-4 without changing a line.”article, stated

Authorization/tool runtime

Chose Arcade.dev managed OAuth and policies over custom token management.

Rationale: Arcade handled GitHub and Slack scopes through per-request OAuth primitives without the team writing token-management code.

“managed authorization, and policy enforcement through per-request OAuth primitives (GitHub(scopes=…), Slack(scopes=…))”article, stated
“handled auth without us writing a single token-management line.”article, stated

Gotchas (what bit them)

Classic tool loops hit rate limits because every large payload re-entered context.

Root cause: Four 15-30KB search results accumulated in the model's input context before the final Slack post.

Resolution: Code Mode kept intermediate payloads inside the sandbox and returned only a short final result.

“By that final turn, the model is reasoning over all four search payloads at once, and every prior tool result comes along for the ride. That’s where the 3× input-token gap comes from, and where exceeding the per-minute rate cap stops being avoidable.”article, stated
“The sandbox swallows the intermediate payloads. Only the final reduce returns to the model’s context.”article, stated

Sonnet misread phrase-search intent and produced confident broad counts.

Root cause: It passed "code mode" unquoted to GitHub Search, which ANDed the words instead of searching the phrase.

Resolution: The experiment exposed this as a model-specific failure profile; Opus and GPT handled phrase quoting differently.

“Sonnet 4-5 consistently failed to grasp the prompt’s phrase-search intent. In every delivery, it passed “code mode” through unquoted to GitHub Search, which ANDs the two words and returned ~25,000 issues — not “code mode” mentions.”article, stated
“The model reported the broad totals as if they were the real count. None of sonnet’s four deliveries flagged the distinction. One run summed the four broad totals into a single headline figure of 208,107 mentions.”article, stated

GPT got the API call right but mis-framed counts.

Root cause: Two GPT-5-4 Code Mode runs reported the safe-limit array length instead of the real total_count field.

Resolution: Typed fields help, but the evaluation surfaced that count framing still needs explicit validation.

“GPT-5-4 always sent phrase-quoted queries to the API, but two of three GPT-5-4 ON runs had a downstream framing bug”article, stated
“GPT-5-4 always sent phrase-quoted queries to the API, but two of three GPT-5-4 ON runs had a downstream framing bug: the agent reported the array length (50, the server’s safe-limit cap) instead of the real total count, so the digest claimed “Total Mentions: 122” instead of the actual ~4,640.”article, stated

Model families had different failure profiles on the same prompt.

Root cause: The same agent/tool setup behaved differently across Sonnet, Opus, and GPT.

Resolution: BYO-LLM execution allowed isolating the model as the only changing variable.

“Three models, three different failure profiles on the same prompt. Sonnet built confident lists from broken inputs. Opus self-corrected mid-flight when it spotted the noise. GPT got the API call right but mis-framed the counts twice.”article, stated
“The three failure modes we surfaced (Sonnet confidently summing broken counts, Opus catching its own noise mid-flight, GPT mis-framing array length as total) are only visible because the model was the only thing changing across runs.”article, stated

Evaluation

Port of Context ran the same prompt in fresh goose sessions across three models, with Code Mode on and off, holding the architecture, MCP server, upstream APIs, and Slack channel constant.

Metrics: 3 models tested: Sonnet 4-5, Opus 4-6, GPT-5-4 · 6 runs per model: 3 Code Mode on, 3 off · 100% delivery with Code Mode on versus 56% off · $0.20 average cost on versus $0.41 off · 31.8k input tokens on versus 93.6k off · $0.056 cheapest correct result

3 supporting quotes
“Same prompt, run using three different models in a fresh goose session each time:”article, stated
“Everything else (the architecture, the MCP server, the upstream APIs, the Slack channel) was held constant.”article, stated
“100% delivery with Code Mode ON. 56% with it off. Every rate-limit failure in the experiment was an OFF-mode run.”article, stated

Outcomes

The case is a controlled experiment over 18 runs, not a long-term production SLO report, but it uses real GitHub/Hacker News/Slack workloads and production-style tool/runtime constraints.

3 supporting quotes
“Code Mode is now the GTM Agent’s production execution path. Next on the roadmap: more sources beyond GitHub and Hacker News, more keyword sets, and scheduled runs through goose.”article, stated
“Next on the roadmap: more sources beyond GitHub and Hacker News, more keyword sets, and scheduled runs through goose.”article, stated
“The three failure modes we surfaced (Sonnet confidently summing broken counts, Opus catching its own noise mid-flight, GPT mis-framing array length as total)”article, stated