Documentation
/
AgentAuth
/
Examples
/
LangChain Integration

LangChain Integration

Give your LangChain agents their own authenticated identity

When a LangChain agent calls an external API, it typically uses a static API key or the deploying user's OAuth token. That means no per-agent identity, no scoped permissions, and no audit trail.

With Hawcx, each LangChain tool call is authenticated with the agent's own cryptographic identity and a single-use encrypted token.

Create an Authenticated Tool

import { HawcxAgent } from "@hawcx/agent-sdk";
import { Tool } from "langchain/tools";

class AuthenticatedAPITool extends Tool {
  name = "authenticated_api";
  description = "Makes authenticated API calls using Hawcx agent identity";

  private agent: HawcxAgent;

  constructor() {
    super();
    this.agent = new HawcxAgent({
      agentInstanceId: "langchain-agent-01",
      configId: process.env.HAWCX_CONFIG_ID,
    });
  }

  async _call(input: string): Promise<string> {
    // Registers on first call, authenticates automatically
    await this.agent.ensureAuthenticated();

    // Each fetch uses a single-use token scoped to this action
    const response = await this.agent.fetch(
      `https://api.yourapp.com/${input}`
    );
    return JSON.stringify(await response.json());
  }
}

Use It in a Chain

import { ChatOpenAI } from "@langchain/openai";
import { AgentExecutor, createOpenAIFunctionsAgent } from "langchain/agents";

const tools = [new AuthenticatedAPITool()];
const llm = new ChatOpenAI({ model: "gpt-4" });

const agent = await createOpenAIFunctionsAgent({ llm, tools, prompt });
const executor = new AgentExecutor({ agent, tools });

const result = await executor.invoke({
  input: "Get the latest orders"
});

Every API call the LangChain agent makes now has its own cryptographic identity, per-request authorization, and a full audit trail, instead of a shared API key or borrowed OAuth token.