Build a Financially Trusted LangChain Agent in 10 Minutes
A step by step tutorial that takes you from a bare LangChain agent to one with a verifiable trust score, event reporting, and score gated financial operations.
By Credian Team
What You Will Build
By the end of this tutorial, you will have a LangChain agent that:
- Has a registered identity on Credian with a unique SID
- Can check its own trust score before making financial decisions
- Reports task completions and failures as events that feed its score
- Gates financial operations based on score thresholds
The full integration takes about 30 lines of code on top of a standard LangChain setup.
Prerequisites
You will need Node.js 18+, a Credian account with an API key (free tier works), and a basic LangChain project. If you do not have a Credian API key yet, run npx credian login and follow the prompts.
Step 1: Install the Dependencies
npm install credian @langchain/core @langchain/openai langchain
The Credian SDK ships with a built in LangChain integration. No additional adapter packages are needed.
Step 2: Add the Credian Tool to Your Agent
Credian provides a CredianTool class that extends LangChain's StructuredTool. This means it works with any LangChain agent executor out of the box.
import { CredianTool } from 'credian/langchain';
import { ChatOpenAI } from '@langchain/openai';
import { AgentExecutor, createOpenAIToolsAgent } from 'langchain/agents';
import { ChatPromptTemplate } from '@langchain/core/prompts';
// Initialize the Credian tool
const credianTool = new CredianTool({
apiKey: process.env.CREDIAN_API_KEY,
capabilities: ['check_score', 'report_event', 'get_history'],
});
// Create the LLM and agent
const llm = new ChatOpenAI({ model: 'gpt-4o' });
const prompt = ChatPromptTemplate.fromMessages([
['system', `You are a financial assistant agent. Before executing any
financial operation, check your trust score using the credian tool.
Only proceed if your score is above 600. After completing tasks,
report the outcome.`],
['human', '{input}'],
['placeholder', '{agent_scratchpad}'],
]);
const agent = await createOpenAIToolsAgent({
llm,
tools: [credianTool],
prompt,
});
const executor = new AgentExecutor({
agent,
tools: [credianTool],
});
The CredianTool supports three actions: check_score queries the agent's current trust score, report_event logs a task outcome, and get_history retrieves score history. You can limit which capabilities are available to the agent by passing a subset in the capabilities array.
Step 3: Run the Agent
const result = await executor.invoke({
input: 'Check my trust score and tell me if I can process a $500 payment',
});
console.log(result.output);
// "Your current trust score is 742 with high confidence.
// This is above the 600 threshold, so you are cleared
// to process the $500 payment."
The agent automatically calls the Credian tool, reads the score, and makes a decision based on the threshold defined in the system prompt. No custom logic required.
Step 4: Report Events to Build Your Score
Trust scores are only as good as the data behind them. Every time your agent completes or fails a task, it should report that event to Credian.
// After a successful task
const result = await executor.invoke({
input: 'Process the payment for order #1234 and report the result',
});
// The agent will use the credian tool to report:
// action: "report_event"
// eventType: "payment.completed"
// eventData: { orderId: "1234", amount: 50000, currency: "USD" }
Each reported event feeds into the scoring engine. Successful task completions improve the reliability dimension. Successful payments improve the financial dimension. Over time, the agent builds a verifiable track record.
Step 5: Score Gated Financial Operations
The real power of trust scoring is using it to gate operations. Here is a pattern for implementing score based access control:
import { Credian } from 'credian';
const credian = new Credian({ apiKey: process.env.CREDIAN_API_KEY });
async function executePayment(amount: number) {
// Check score before any financial operation
const score = await credian.scores.get();
if (score.confidence === 'low') {
throw new Error('Insufficient score confidence for financial operations');
}
if (score.overallScore < 600) {
throw new Error(`Trust score ${score.overallScore} below minimum 600`);
}
// Score check passed, proceed with payment
const payment = await processPayment(amount);
// Report the outcome
await credian.events.report({
type: payment.success ? 'payment.completed' : 'payment.failed',
data: { amount, success: payment.success },
});
return payment;
}
Why Gate on Confidence Too?
An agent with a score of 800 based on 5 events is very different from one with a score of 800 based on 5,000 events. The confidence level (low, medium, high) tells you how much data supports the score. For financial operations, requiring at least medium confidence prevents agents from gaming the system with a small number of favorable events.
Step 6: Monitor Score Evolution
Use the history endpoint to track how your agent's score changes over time:
const history = await credian.scores.history();
// Returns score snapshots with timestamps
// [
// { overallScore: 742, confidence: "high", calculatedAt: "2026-03-30T..." },
// { overallScore: 735, confidence: "high", calculatedAt: "2026-03-29T..." },
// ...
// ]
What Happens Under the Hood
When the CredianTool is invoked by the LangChain agent, here is the flow:
- The LLM decides it needs trust data and calls the tool with an action
- The tool uses the Credian SDK to make an API call to the Credian Score API
- The API responds with the score data (sub 50ms for cached lookups)
- The tool formats the response as a string and returns it to the LLM
- The LLM incorporates the score data into its reasoning and response
The agent ID is automatically resolved from the API key, so you never need to hardcode or pass agent IDs around. One API key per agent, one agent per score.
Full Working Example
import { CredianTool } from 'credian/langchain';
import { ChatOpenAI } from '@langchain/openai';
import { AgentExecutor, createOpenAIToolsAgent } from 'langchain/agents';
import { ChatPromptTemplate } from '@langchain/core/prompts';
const credianTool = new CredianTool({
apiKey: process.env.CREDIAN_API_KEY,
});
const llm = new ChatOpenAI({ model: 'gpt-4o' });
const prompt = ChatPromptTemplate.fromMessages([
['system', `You are a trusted financial agent. Always check your
trust score before financial operations. Report all task outcomes.
Minimum score for payments: 600. Minimum confidence: medium.`],
['human', '{input}'],
['placeholder', '{agent_scratchpad}'],
]);
const agent = await createOpenAIToolsAgent({
llm,
tools: [credianTool],
prompt,
});
const executor = new AgentExecutor({ agent, tools: [credianTool] });
// The agent now has trust awareness built in
const result = await executor.invoke({
input: 'I need to process a $200 payment for invoice #789',
});
console.log(result.output);
Get your API key: npx credian login sets up your agent and gives you an API key in under 30 seconds. The free tier includes 1,000 score lookups per month.