Docs/Integrations/Framework Adapters

Framework Adapters

Drop-in adapters for LangChain, CrewAI, and AutoGen. Instrument existing AI frameworks with A2A signing and Invariance receipts.

import { createInstrumentedFetch, wrapLangChainTool } from '@invariance/sdk'; // Framework observability adapters: // import { InvarianceLangChainTracer } from '@invariance/sdk/adapters/langchain'; // import { InvarianceCrewAIMiddleware } from '@invariance/sdk/adapters/crewai'; // import { InvarianceAutoGenMiddleware } from '@invariance/sdk/adapters/autogen';
Prerequisites: Initialization, Sessions & Receipts, A2A Communication

Overview

Adapters provide drop-in instrumentation for popular AI agent frameworks. `createInstrumentedFetch()` wraps fetch() with A2A signing. `wrapLangChainTool()` wraps a LangChain tool for receipt recording. CrewAI and AutoGen adapters are available as separate entry points.

Quick Example

Instrument LangChain and HTTP agentstypescript
import { A2AChannel, createInstrumentedFetch } from '@invariance/sdk';

const session = inv.session({ agent: 'my-agent', name: 'run-1' });
const channel = new A2AChannel(session, 'my-agent', privateKey);
const ifetch = createInstrumentedFetch(channel, 'other-agent');

// Every HTTP request is now A2A-signed
const res = await ifetch('https://other-agent.example.com/api/task', {
  method: 'POST', body: JSON.stringify({ task: 'analyze' }),
});

// LangChain tool wrapper
import { wrapLangChainTool } from '@invariance/sdk';
const instrumentedTool = wrapLangChainTool(myTool, {
  channel, receiverAgent: 'other-agent',
});

API Reference

createInstrumentedFetch
Create a fetch() replacement that wraps every HTTP request with A2A signing.
createInstrumentedFetch(channel: A2AChannel, receiverAgent: string, opts?: { senderPublicKey?: string }): typeof fetch
Parameters
channelA2AChannelA2A channel used to wrap outgoing and incoming envelopes
receiverAgentstringIdentity of the receiving agent
opts{ senderPublicKey?: string }Optional sender public key for response verification
Returnstypeof fetch
wrapLangChainTool
Wrap a LangChain tool to record outbound A2A handoffs before the tool executes.
wrapLangChainTool(tool: Tool, opts: { channel: A2AChannel; receiverAgent: string; senderPublicKey?: string }): Tool
Parameters
toolToolLangChain tool to wrap
optsA2ALangChainToolOptionsA2A channel and receiver configuration
ReturnsTool
InvarianceLangChainTracer
LangChain callback tracer that emits observability events through an InvarianceTracer for a specific session.
new InvarianceLangChainTracer(tracer: InvarianceTracer, sessionId: string)
Parameters
tracerInvarianceTracerTracer instance from inv.tracer
sessionIdstringSession identifier to associate with emitted events
ReturnsInvarianceLangChainTracer
InvarianceCrewAIMiddleware
CrewAI lifecycle middleware for crews, tasks, agent actions, and delegation chains.
new InvarianceCrewAIMiddleware(tracer: InvarianceTracer, sessionId: string)
Parameters
tracerInvarianceTracerTracer instance from inv.tracer
sessionIdstringSession identifier for emitted events
ReturnsInvarianceCrewAIMiddleware
InvarianceAutoGenMiddleware
AutoGen middleware for message, tool-call, and group-chat observability.
new InvarianceAutoGenMiddleware(tracer: InvarianceTracer, sessionId: string)
Parameters
tracerInvarianceTracerTracer instance from inv.tracer
sessionIdstringSession identifier for emitted events
ReturnsInvarianceAutoGenMiddleware

Walkthrough

Instrumenting framework-based agents
1
Instrument a LangChain agent
Add the LangChain tracer for automatic observability events.
typescript
import { Invariance } from '@invariance/sdk';
import { InvarianceLangChainTracer } from '@invariance/sdk/adapters/langchain';
import { ChatOpenAI } from '@langchain/openai';

const inv = Invariance.init({ apiKey: 'dev_...', privateKey: '...' });
const session = inv.session({ agent: 'langchain-agent', name: 'run-1' });

const handler = new InvarianceLangChainTracer(inv.tracer, session.id);
const llm = new ChatOpenAI({ callbacks: [handler] });

const result = await llm.invoke('Analyze market trends');
session.end();
2
Instrument a CrewAI workflow
Emit CrewAI lifecycle events through the middleware.
typescript
import { InvarianceCrewAIMiddleware } from '@invariance/sdk/adapters/crewai';

const session = inv.session({ agent: 'crew-orchestrator', name: 'crew-run' });
const middleware = new InvarianceCrewAIMiddleware(inv.tracer, session.id);

middleware.onCrewStart({
  name: 'research-crew',
  agents: ['researcher', 'analyst'],
  tasks: ['gatherData', 'writeReport'],
});
3
Instrument HTTP communication
Use createInstrumentedFetch for A2A-signed HTTP requests.
typescript
import { A2AChannel, createInstrumentedFetch } from '@invariance/sdk';

const session = inv.session({ agent: 'http-agent', name: 'api-calls' });
const channel = new A2AChannel(session, 'http-agent', privateKey);
const ifetch = createInstrumentedFetch(channel, 'partner-agent');

const response = await ifetch('https://partner-agent.com/api/task', {
  method: 'POST',
  body: JSON.stringify({ task: 'analyze' }),
});
session.end();

Use Cases

  • Add Invariance to existing LangChain agents with a single wrapper
  • Instrument HTTP-based agent communication transparently
  • Create provable audit trails for multi-agent LangChain workflows
  • Gradually adopt Invariance by wrapping one tool at a time
  • Trace CrewAI crew execution with full lifecycle observability
  • Monitor AutoGen group chat conversations and tool usage
On this page
OverviewQuick ExampleAPI ReferenceWalkthroughUse CasesRelated Modules