Project HandshakeAI
UniChain Gateway Resilient Intelligence for Agentic AI
An adaptable, lightweight LangChain abstraction client engineered to bridge the gap between enterprise security architectures and modern agentic AI workflows.
The problem is not always the model.
In enterprise environments, the network sitting between an agent and an LLM can become the weakest link in the tool-calling protocol.
01 · Challenge
Gateway-induced protocol drift
Enterprises frequently route LLM traffic through unified API firewalls and security gateways to enforce data protection, context filtering, authentication and compliance policies.
These gateways may modify payload metadata, alter message parameters, sanitize structural formats or expose legacy function schemas.
A modern agent may therefore send a valid tool-calling request and receive a response that no longer matches the structure expected by its runtime.
02 · Solution
A resilient abstraction layer
UniChain Gateway acts as a compatibility layer around LangChain's native message model.
It dynamically adapts outbound tool definitions, authentication conventions and inbound function-call structures according to the topology through which the request travels.
The objective is simple: preserve the agent's execution semantics while fitting within enterprise gateway and security constraints.
Three layers of resilience.
Compatibility logic is concentrated in one abstraction instead of being distributed throughout every agent.
Dynamic Payload Fluidity
Converts tool definitions between modern OpenAI-style nested
tools structures and legacy or enterprise-facing
flat functions
schemas.
Authentication Resilience
Supports differing gateway authentication conventions, including standard Bearer-style prefixes and raw token configurations.
State & Flow Normalization
Converts firewall-modified text responses and structured function arrays back into clean LangChain-compatible execution state.
The handshake sits between the agent and the gateway.
UniChain translates the representation at the boundary while allowing the agent runtime to continue working with its native tool and message abstractions.
What is 15 × 27?"] A --> B["LangGraph
create_react_agent"] B --> C["UniversalGatewayChatModel
_generate()"] C --> D["Message Normalization
_convert_messages()"] D --> E{"Gateway Topology"} E -->|Modern API| F["Nested Tools Schema"] E -->|Legacy / Hardened Proxy| G["Flat Functions Schema"] F --> H["Remote API Gateway"] G --> H H --> I["Model Response
tool_call"] I --> J["Gateway Response Parser"] J --> K["Local Python Runtime"] K --> L["multiply(15, 27)
→ 405"] L --> M["ToolMessage"] M --> C C --> N["Final Model Response"] N --> O["15 × 27 is 405."]
What actually happens during execution?
Tool calling is not a single request. It is a two-way interaction between the model and the local agent runtime.
LangGraph receives the natural-language task and prepares the model invocation together with the available tool definitions.
LangChain message objects and tool definitions are converted into raw structures suitable for the target API gateway.
Depending on the configured topology, the tool schema can be represented using modern nested tools or legacy flat function structures.
The remote model returns a structured function or tool-call request describing the operation it wants the local runtime to execute.
LangGraph intercepts the structured request and calls the corresponding local Python function.
The tool result is appended to the execution state and sent back through the same abstraction boundary.
The model consumes the tool result and generates the final natural-language response.
Keep the application code simple.
Gateway-specific compatibility belongs inside the model abstraction, not inside every individual agent.
import json
import requests
from typing import (
List,
Any,
Iterator,
Optional,
Dict,
Sequence,
Union
)
from langchain_core.language_models.chat_models import BaseChatModel
from langchain_core.messages import (
BaseMessage,
AIMessage,
HumanMessage,
SystemMessage
)
from langchain_core.outputs import (
ChatResult,
ChatGeneration
)
from langchain_core.tools import tool
from langgraph.prebuilt import create_react_agent
from pydantic import Field
llm = UniversalGatewayChatModel(
model_name="default",
base_url="https://api.llm7.io/v1",
api_key="YOUR_SECURE_GATEWAY_TOKEN",
is_openai_compatible=True
)
@tool
def multiply(a: int, b: int) -> int:
"""Multiply two integers together accurately."""
return a * b
agent_executor = create_react_agent(
llm,
tools=[multiply]
)
response = agent_executor.invoke({
"messages": [
("user", "What is 15 times 27?")
]
})
print(
"Final Synthesis:",
response["messages"][-1].content
)
Key methods & variables.
Compatibility behavior is concentrated into a small set of explicit controls and translation methods.
Bearer
authorization prefix.
Resilient intelligence through the handshake protocol.
Enterprise AI systems increasingly sit behind authentication layers, security proxies, unified API gateways and context-filtering infrastructure.
These systems are valuable because they enforce organizational controls. But they can also introduce structural differences between the payload an agent expects and the payload a remote model receives.
Modern tool calling depends on precise structured information. A small transformation to that structure can prevent an otherwise capable agent from invoking the correct local function.
UniChain Gateway addresses this boundary problem by treating the gateway as a translation layer rather than forcing every agent to understand every gateway dialect.
The agent continues operating against familiar LangChain and LangGraph abstractions while the gateway client handles serialization, schema adaptation, authentication conventions and response normalization.
The result is a cleaner separation between application intelligence and enterprise transport constraints.
The model does not need to speak every gateway's dialect. The abstraction layer can translate the handshake.
Implementation reference.
The implementation contains the gateway abstraction, message normalization and agent execution logic behind Project HandshakeAI.
No comments:
Post a Comment