how to provide agent role and custom knowledge bas...
# ask-ai
l
how to provide agent role and custom knowledge base to the agent using nodeJS
t
Thanks for reaching out, Anirban. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're asking how to assign an agent role and give that agent a custom knowledge base using Node.js. Below are concise, actionable options with short code examples you can use depending on your needs. 1) Set the agent role (system prompt) • Provide the role via the
instructions
field when constructing
voice.Agent
.
Copy code
import { voice } from '@livekit/agents';

class SupportAgent extends voice.Agent {
  constructor() {
    super({
      instructions: 'You are a helpful support agent. Assist users with account, billing, and troubleshooting.',
    });
  }
}
2) Small/static knowledge base — preload into chat context • Good for short, fixed policies or FAQs. Add messages to
ChatContext
before starting the session.
Copy code
import { voice, llm } from '@livekit/agents';

const initialCtx = llm.ChatContext.empty();
initialCtx.addMessage({
  role: 'assistant',
  content: 'Refund policy: refunds issued within 5 business days. Support hours: 24/7.',
});

const agent = new voice.Agent({ chatCtx: initialCtx });
3) Dynamic/large knowledge base — expose a tool that queries your KB • Define an
llm.tool()
that performs search (vector DB, API, etc.). The agent calls it during the conversation.
Copy code
import { voice, llm } from '@livekit/agents';
import { z } from 'zod';

const searchKB = llm.tool({
  description: 'Search company knowledge base',
  parameters: z.object({ query: z.string() }),
  execute: async ({ query }) => {
    // replace with your search (Pinecone, Supabase, HTTP API, etc.)
    const res = await fetch('<https://your-kb-api/search>', {
      method: 'POST',
      body: JSON.stringify({ query }),
    }).then(r => r.json());
    return JSON.stringify(res);
  },
});

const agent = new voice.Agent({
  instructions: 'You are a support agent. Use tools to retrieve factual info.',
  tools: { searchKB },
});
4) Retrieval-augmented generation (RAG) — inject relevant docs per user turn • Use a hook like
onUserTurnCompleted
to run a vector search and append relevant context to the turn before the agent replies.
Copy code
import { voice, llm } from '@livekit/agents';

class RagAgent extends voice.Agent {
  async onUserTurnCompleted(turnCtx: llm.ChatContext, newMessage: llm.ChatMessage) {
    const docs = await myVectorSearch(newMessage.textContent); // implement your search
    turnCtx.addMessage({
      role: 'assistant',
      content: `Relevant docs:\n${docs.join('\n\n')}`,
    });
  }
}
Which option fits you best? • If your KB is small/static: use option 2. • If your KB is large or updated frequently: use option 3 or 4. If you tell me which knowledge store you plan to use (e.g., Pinecone, Supabase, LlamaIndex, custom HTTP API) and whether you need realtime tool calls or preloaded context, I’ll provide a tailored code example and integration steps. Sources: Agents (Node.js) | livekit/livekit_composite | DeepWiki | External data and RAG | LiveKit Docs | OpenAI Realtime API plugin guide | LiveKit docs | Tool definition and use | LiveKit docs | Pipeline nodes and hooks | LiveKit docs