Example: Using Grok features
Overview
This inbound example showcases Grok tools (function calling, file_search, web_search, x_search) with barge-in for telephony use. The same tool configuration applies to outbound calls—reuse the tools and prompt in your outbound scenario and swap the call entrypoint.
⬇️ Jump to the Full VoxEngine scenario.
Prerequisites
- Set up an inbound entrypoint for the caller:
- Phone number: https://voximplant.com/docs/getting-started/basic-concepts/phone-numbers
- WhatsApp: https://voximplant.com/docs/guides/integrations/whatsapp
- SIP user / SIP registration: https://voximplant.com/docs/guides/calls/sip
- Voximplant user: https://voximplant.com/docs/getting-started/basic-concepts/users (see also https://voximplant.com/docs/guides/calls/scenarios#how-to-call-a-voximplant-user)
- Create a routing rule that points the destination to this scenario: https://voximplant.com/docs/getting-started/basic-concepts/routing-rules
- Store your Grok API key in
ApplicationStorageunderXAI_API_KEY. - If you use
file_search, upload documents and setCOLLECTION_IDaccordingly.
Usage highlights
- Create a
VoiceAgentAPIClientwithGrok.createVoiceAgentAPIClient(...). - Configure the session with
voice,turn_detection,instructions, and tools. - Bridge audio with
VoxEngine.sendMediaBetween(call, client). - Handle function calls with
ResponseFunctionCallArgumentsDone.
Feature summary
- Function calling: Let Grok request server-side actions, then respond with structured outputs. See Function calling. Also see Grok docs: https://docs.x.ai/docs/guides/tools/overview#function-calling
- RAG with file_search: Ground responses in your uploaded documents using vector search. See RAG with file_search. Also see Grok docs: https://docs.x.ai/docs/guides/tools/overview#file-search
- Web search: Allow Grok to fetch current public information when needed. See Web search. Also see Grok docs: https://docs.x.ai/docs/guides/tools/overview#web-search
- X/Twitter search: Constrain Grok to specific X handles for social lookups. See X/Twitter search. Also see Grok docs: https://docs.x.ai/docs/guides/tools/overview#x-search
Turn detection & barge-in
When InputAudioBufferSpeechStarted fires, clear the media buffer so the caller can interrupt the agent:
Function calling
Function calling lets Grok request actions from VoxEngine (transfer, hang up, fetch data) and receive a structured response. For official Grok tool guidance, see https://docs.x.ai/docs/guides/tools/overview#function-calling.
Handle tool calls and return outputs to the agent:
RAG with file_search
Use file_search to ground responses in your documents. Upload files to Grok, then reference the collection (vector store) ID in your session tools. For official Grok tool guidance, see https://docs.x.ai/docs/guides/tools/overview#file-search.
- Keep your collection focused on the topics the agent should answer.
- Tune
max_num_resultsto balance relevance and speed.
Web search
Enable web_search when you want Grok to fetch public web information at runtime. For official Grok tool guidance, see https://docs.x.ai/docs/guides/tools/overview#web-search.
X/Twitter search
Use x_search to limit Grok’s social search to specific X handles. For official Grok tool guidance, see https://docs.x.ai/docs/guides/tools/overview#x-search.
Configure before you run
- Set
XAI_API_KEYinApplicationStorage. - Update
COLLECTION_IDto point at your uploaded documents (or removefile_search). - Adjust the
SYSTEM_PROMPTto match your brand voice and escalation rules.
Try it
Notes
See the VoxEngine API Reference for more details.
Full VoxEngine scenario
This scenario includes barge-in handling, function calling, file search, web search, and X search.