Menu
ByteByteGo·July 29, 2026

Optimizing AI Agent Architecture: Harness, API, and Inference Layers

This article dissects the architecture of advanced AI agent applications like ChatGPT Work and Codex, focusing on the three main layers: Harness, API, and Inference. It details critical optimization techniques at each layer, such as persistent WebSockets, stable prompt prefixes, and speculative decoding, to significantly reduce latency and cost in iterative AI agentic loops.

Read original on ByteByteGo

AI agent applications, unlike simple LLM calls, involve complex, multi-step processes to complete tasks. This article from OpenAI engineers provides deep insights into the architectural layers and optimization strategies that enable efficient and cost-effective execution of these agentic loops. Understanding these layers is crucial for designing performant AI systems.

The Three Layers of an Agentic AI Application

An AI agent application is more than just an LLM; it's a sophisticated system comprising several interconnected components, each with distinct responsibilities. The request flow progresses through these layers:

  1. Harness Layer: This is the orchestration layer that interprets user requests, builds the LLM context (instructions, tool definitions, history), and manages the agentic loop. It executes tool calls in a sandbox environment and feeds results back to the LLM.
  2. API Layer: Sitting between the Harness and the Inference layer, this application layer handles essential functionalities like authentication, rate limiting, input tokenization, output detokenization, and parallel safety checks.
  3. Inference Layer: This layer is responsible for running the LLM model on GPU fleets to process tokens and generate responses as efficiently as possible, whether it's a tool call or a final answer.

Key Optimization Techniques Across Layers

ℹ️

The Agentic Loop Challenge

Tasks often require many iterations (over 100) of the agentic loop. Each iteration redoes significant work (resending history, re-tokenizing, reprocessing prompts), creating substantial overhead. Optimizing this repeated work is the primary focus.

  • Harness Layer Optimizations: Persistent WebSockets for reduced connection setup overhead and incremental requests to minimize payload repetition by sending only deltas. Stable prompt prefixes leverage prompt caching, while deferred tool discovery and Code Mode reduce context size.
  • API Layer Optimizations: Tokenizing only the delta, not the entire conversation history, for efficiency. Running safety checks in parallel with inference to minimize latency and abort early if policy violations are detected.
  • Inference Layer Optimizations: Cache-aware routing to send requests to GPUs with relevant KV cache. Efficient KV cache management for long contexts. Speculative decoding for faster token generation and separating prefill from decode phases to optimize GPU utilization and latency.

These techniques highlight a deep understanding of the performance bottlenecks in large-scale AI systems, particularly those with iterative, conversational interactions. The trade-offs involve increased statefulness (e.g., WebSockets, delta tokenization) for reduced network and computation overhead.

Design Takeaways

When designing agentic AI systems, consider a layered architecture, similar to OpenAI's, to separate concerns. Prioritize stateful communication and data delta transmission for iterative processes. Implement caching and speculative execution where possible to reduce latency. Crucially, parallelism (e.g., safety checks during inference) can provide significant performance gains.

AI AgentsLLM ArchitectureSystem OptimizationPerformanceDistributed AIWebSocketsPrompt EngineeringGPU Optimization

Comments

Loading comments...