Skip to main content
Observability & Tracing•6 min read

The Transparent Recursive Proxy: Instrumenting Agent Tool Calls Without Rewriting Code

How AgentRein uses JavaScript Proxy and Python __getattr__ to intercept SDK method calls at any depth while preserving native TypeScript types and IDE autocompletion.

AgentRein Engineering Team

AgentRein Engineering Team

Published: October 2026•Updated: October 2026

Developer adoption of agent safety tools often fails because frameworks demand invasive refactoring: replacing familiar vendor SDKs with proprietary tool wrapper classes. AgentRein takes an alternative approach: the Transparent Recursive Proxy, which intercepts method invocations at any depth while keeping existing SDK code completely intact.

Why Custom SDK Wrappers and DSLs Create Developer Friction

Most agent libraries require developers to translate standard client calls into custom tool schemas or dynamic JSON objects. This strips away native TypeScript type-checking, breaks IDE autocomplete, and complicates library version upgrades.

With AgentRein wrap(), you pass your existing SDK client (such as Octokit, Stripe, or a database connection) along with an active session. The returned object preserves 100% of the original client’s interface.

The Recursive Proxy Pattern Explained

Under the hood, agentrein.wrap() constructs a blind recursive proxy handler:

1. Deep Property Navigation: When your agent accesses octokit.rest.issues.create or stripe.subscriptions.cancel, each property access returns a child proxy accumulating the access path (e.g., ["rest", "issues", "create"]).

2. Parameter Normalization: In TypeScript, args[0] is captured; in Python, kwargs if kwargs else args[0] is extracted to support idiomatic Python keyword arguments.

3. Execution Interception: When invoked, the proxy resolves the target method, verifies approval policies, logs the action to the backend registry, and awaits results without altering return shapes.

examples/proxy-usage.tstypescript
1import { AgentRein } from 'agentrein'
2import { Octokit } from '@octokit/rest'
3
4const agentrein = new AgentRein({ apiKey: process.env.AGENTREIN_API_KEY! })
5const session = await agentrein.newSession({ agentId: 'pr-agent' })
6
7// Native Octokit client wrapped transparently
8const octokit = agentrein.wrap(new Octokit({ auth: process.env.GITHUB_TOKEN }), session, {
9 connector: 'github',
10 retryable: ['issues.create', 'pulls.create'],
11})
12
13// Full TypeScript type safety and autocompletion are preserved
14const issue = await octokit.rest.issues.create({
15 owner: 'agentrein',
16 repo: 'core',
17 title: 'Automated dependency update',
18})

Automatic Operation Classification (CREATE, UPDATE, DELETE)

The proxy inspects the terminal path segment to automatically classify the mutation type using the canonical _OP_MAP:

• CREATE: Methods named create, send, post, append, add.

• UPDATE: Methods named update, patch, modify, move, trash, upsert.

• DELETE: Methods named delete, del, remove, destroy.

This classification determines whether pre-execution snapshots are captured and how policies evaluate the operation.

Configuring Failure Modes: Open vs. Closed

AgentRein allows teams to choose how wrapped clients behave if the AgentRein backend service becomes unreachable:

- failureMode: "open" (Default): Logs warning messages to stdout and permits tool calls to execute without safety guarantees, prioritizing agent availability.

- failureMode: "closed": Throws AgentReinUnavailableError and halts execution, ensuring no unmonitored mutations occur in strict compliance environments.

Summary & Key Invariants

  • ›The recursive proxy pattern preserves native TypeScript types and method signatures without forcing code refactoring.
  • ›Method names are automatically categorized into standardized operations (CREATE, UPDATE, DELETE) for policy evaluation.
  • ›Teams can explicitly configure failure modes (open vs. closed) to balance availability against strict governance.
Tags:#Recursive Proxy#TypeScript#Type Safety#Instrumentation#Architecture
AgentRein Engineering Team

AgentRein Engineering Team

Core Architecture & Platform Reliability

Published by the AgentRein engineering team. AgentRein provides transparent proxy instrumentation, deterministic LIFO rollbacks, runtime intent verification, and human-in-the-loop governance for AI agent fleets.