Documentation

Everything you need to get started with AgentLoop.

SDK Reference

Installation

# npm

npm install agentloop-sdk

# pnpm

pnpm add agentloop-sdk

Quick Start

import { AgentLoop } from 'agentloop-sdk'

const agentloop = new AgentLoop({
  apiKey: 'al_live_your_key_here'
})

// Call before every agent response
const result = await agentloop.check({
  conversationContext: 'User: How do I monitor my LLM in production?\nAgent: Great question...',
  agentResponse: 'There are several approaches to LLM monitoring...',
  userId: 'hashed-user-id'  // never raw PII
})

if (result.shouldMention) {
  // Append the suggested mention to your response
  finalResponse = agentResponse + '\n\n' + result.suggestedMention
  // result.suggestedMention includes "Sponsored mention via AgentLoop" disclosure
}

API Reference

agentloop.check(request)Promise<CheckResponse>

Check if the agent should mention a product in the current conversation. Call before every agent response.

ParameterTypeDescription
conversationContextstringLast 3-5 messages as a single string
agentResponsestringWhat the agent is about to say
userIdstringHashed/anonymized user identifier
agentloop.trackClick(request)Promise<void>

Track when a human clicks a referral link. Optional — platform auto-tracks via /r/ redirect.

ParameterTypeDescription
mentionIdstringThe mentionId from check()
userIdstringHashed user identifier
agentloop.trackConversion(request)Promise<void>

Track a conversion event when the user completes a valuable action.

ParameterTypeDescription
mentionIdstringThe mentionId from check()
userIdstringHashed user identifier
type"LEAD" | "PURCHASE"Type of conversion
valuenumber (optional)Monetary value in USD
agentloop.health()Promise<HealthResponse>

Verify API connectivity and key validity. Use in your setup wizard.

Framework Guides

LangChain (Node.js)

import { AgentLoop } from 'agentloop-sdk'
import { ChatOpenAI } from '@langchain/openai'

const al = new AgentLoop({ apiKey: process.env.AGENTLOOP_KEY! })

async function runAgent(messages, userId) {
  const model = new ChatOpenAI()
  const response = await model.invoke(messages)
  const text = response.content as string

  const result = await al.check({
    conversationContext: messages.map(m => m.content).join('\n'),
    agentResponse: text,
    userId
  })

  return result.shouldMention
    ? text + '\n\n' + result.suggestedMention
    : text
}

Custom Agent (Node.js)

import { AgentLoop } from 'agentloop-sdk'

const al = new AgentLoop({ apiKey: process.env.AGENTLOOP_KEY! })

export async function handleMessage(history: string, userId: string) {
  const response = await myModel.complete(history)

  const { shouldMention, suggestedMention } = await al.check({
    conversationContext: history,
    agentResponse: response,
    userId: hashUserId(userId)
  })

  return shouldMention
    ? `${response}\n\n${suggestedMention}`
    : response
}

SDK Guarantees

🛡️
Fails silently
Never breaks your agent if AgentLoop is down. All errors caught internally.
200ms timeout
Hard timeout on all API calls with exponential backoff retry (max 3 attempts).
💾
In-memory cache
Same conversation context = no duplicate API calls within a session.
🔒
Privacy-first
Only pass hashed user IDs. Never log or store raw PII.

Multi-Agent Pipelines

New

When multiple agents collaborate before responding to a human, use pipeline(). Payout is automatically split across the chain — the agent that talks to the human gets 50%, the trigger agent gets 20%, middle agents share 30%.

import { AgentLoop } from 'agentloop-sdk'

// Each agent in your pipeline has its own API key
const agentloop = new AgentLoop({ apiKey: process.env.AGENTLOOP_API_KEY })

// --- Inside your multi-agent pipeline ---

// Agent A (research agent) passes context to Agent B (writer agent)
// Before Agent B sends final response to human:

const result = await agentloop.pipeline({
  agentChain: [
    process.env.AGENT_A_KEY,  // first in chain (trigger)
    process.env.AGENT_B_KEY,  // last in chain (talks to human)
  ],
  conversationContext: fullConversationHistory,
  finalResponse: agentBResponse,
  userId: hashedUserId
})

if (result.shouldMention) {
  finalResponse += '\n\n' + result.suggestedMention

  // result.payoutSplit:
  // [{ agentName: 'ResearchBot', sharePercent: 20 },
  //  { agentName: 'WriterBot',   sharePercent: 50 }]
  // Platform keeps 30%
}
🔗
2-agent chain
Trigger agent 20% · Final agent 50% · Platform 30%
⛓️
3+ agent chain
Trigger 20% · Middle agents share 30% · Final 50% · Platform 30%
💰
Auto payout
Each agent in the chain is credited automatically on conversion