agentloop-langchain
v1.0.0
Published
AgentLoop integration for LangChain — earn up to 70% revenue share when your agent recommends relevant products.
Maintainers
Readme
agentloop-langchain
AgentLoop integration for LangChain. Earn up to 70% revenue share when your agent naturally recommends relevant products — with zero changes to your existing chains.
Install
npm install agentloop-langchainOption 1 — Callback Handler (recommended)
Drop into any chain. Runs automatically after every LLM response.
import { AgentLoopCallbackHandler } from 'agentloop-langchain'
import { ChatOpenAI } from '@langchain/openai'
import { HumanMessage } from '@langchain/core/messages'
const agentloop = new AgentLoopCallbackHandler({
apiKey: process.env.AGENTLOOP_API_KEY!,
getUserId: () => hashUserId(session.userId), // hash it — never raw PII
})
// Add to any model, chain, or agent
const model = new ChatOpenAI({
callbacks: [agentloop],
})
const response = await model.invoke([
new HumanMessage('what tool should I use to monitor my LLM in production?')
])
// response.content already has the sponsored mention appended if relevant
console.log(response.content)Works with any LangChain chain type:
// With LCEL chains
const chain = prompt.pipe(model).pipe(outputParser)
const result = await chain.invoke({ question }, { callbacks: [agentloop] })
// With ConversationChain
const conversation = new ConversationChain({ llm: model, callbacks: [agentloop] })
// With agents
const agent = createReactAgent({ llm: model, tools, prompt })
const executor = AgentExecutor.fromAgentAndTools({ agent, tools, callbacks: [agentloop] })Option 2 — Tool (agent decides when to check)
Let the agent decide when to invoke AgentLoop, rather than automatically after every response.
import { AgentLoopTool } from 'agentloop-langchain'
import { createReactAgent, AgentExecutor } from 'langchain/agents'
const tools = [
new AgentLoopTool({
apiKey: process.env.AGENTLOOP_API_KEY!,
getUserId: () => hashUserId(session.userId),
}),
// ...your other tools
]
const agent = createReactAgent({ llm, tools, prompt })
const executor = AgentExecutor.fromAgentAndTools({ agent, tools })Configuration
new AgentLoopCallbackHandler({
apiKey: string // required — your AgentLoop API key
getUserId?: () => string // recommended — return a hashed user ID per session
contextWindow?: number // how many messages to use as context (default: 5)
debug?: boolean // verbose logging (default: true in development)
})How it works
- User sends a message to your agent
- Your agent generates a response via LangChain
- AgentLoop intercepts the response and scores it against active campaigns
- If relevance score > 70/100, a natural sponsored mention is appended
- You earn up to 70% of the CPC/CPL when the user clicks or converts
Guardrails built in:
- Max 1 mention per conversation
- Crisis/emergency conversations are never monetised
- FTC-compliant "Sponsored mention via AgentLoop" disclosure on every mention
- Fails silently — never breaks your chain
Get your API key
Sign up at agentloop.life → Dashboard → API Keys.
Free to join as an agent owner.
