AI Agents 9 min read

Build a LangChain Voice Agent with Privocio Speech-to-Text

Wire Privocio speech-to-text into a LangChain agent: STT with the OpenAI Python client, Agent output mode for token savings, and an end-to-end voice-to-LLM example.

Developer speaking into a microphone with voice-to-agent workflow sketch on desk
Quick answer: Use Privocio as the speech-to-text step in a LangChain voice agent by pointing the OpenAI Python client at https://api.privocio.com/v1, transcribing audio to text, then passing that text into your LangChain chain or agent. Enable Agent output mode to reduce downstream LLM tokens.

This is a pattern guide — not an official LangChain plugin. It shows a production-shaped flow: microphone → Privocio STT → LangChain → LLM. Overview also on our LangChain integration page.

Architecture: STT → transcript → LangChain chain

Audio file or stream
    ↓
Privocio /v1/audio/transcriptions (Whisper-compatible)
    ↓
Transcript (prefer Agent output mode)
    ↓
LangChain prompt / tools / agent executor
    ↓
LLM response (OpenAI, Anthropic, etc.)

Keep STT and LLM providers separate. Privocio handles transcription; LangChain orchestrates reasoning with whichever chat model you configure.

Related: speech-to-text for AI agents, voice pipeline architecture.

Prerequisites

    • Python 3.9+
    • Privocio API key (authentication)
    • pip install openai langchain-openai langchain-core
    • Sample audio file (user_voice.wav)

Step 1: Transcribe with Privocio (OpenAI-compatible client)

import os
from openai import OpenAI

stt = OpenAI(
    api_key=os.environ["PRIVOCIO_API_KEY"],
    base_url="https://api.privocio.com/v1",
)

def transcribe(path: str, language: str = "en") -> str:
    with open(path, "rb") as audio:
        result = stt.audio.transcriptions.create(
            model="whisper-1",
            file=audio,
            language=language,
            extra_body={"output_mode": "agent"},
        )
    return result.text

output_mode=agent asks Privocio for token-optimized text suited for LLM prompts. See output modes. Use clean or raw when you need readable prose or verbatim fidelity.

Full Python STT tutorial: Python speech-to-text API.

Step 2: Pass transcript into LangChain

from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate

llm = ChatOpenAI(model="gpt-4o", temperature=0)

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant. Answer based on the user's spoken request."),
    ("human", "{transcript}"),
])

chain = prompt | llm

transcript = transcribe("user_voice.wav")
response = chain.invoke({"transcript": transcript})
print(response.content)

The LLM client here is independent — it can stay on OpenAI, Azure, or another provider while STT runs through Privocio.

Step 3: Optional tool-calling agent

For agents with tools, wrap transcription before the agent executor:

from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain_core.tools import tool

@tool
def lookup_docs(query: str) -> str:
    """Search internal documentation."""
    return f"Results for: {query}"

tools = [lookup_docs]
agent = create_tool_calling_agent(llm, tools, prompt)
executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

spoken = transcribe("user_voice.wav")
executor.invoke({"transcript": spoken})

Adapt tools and prompts to your product. This snippet shows where STT plugs in — before agent logic runs.

Error handling and latency

    • Timeouts — batch files may take minutes; set generous HTTP timeouts on the OpenAI client
    • 401 — invalid API key; rotate keys from the dashboard
    • 413 — file too large; compress or split audio
    • Retries — retry 502/503 with exponential backoff in production

For live voice, consider streaming transcription and a separate worker — batch transcriptions.create is best for recorded prompts.

Latency tips: designing voice-to-agent pipelines, real-time vs batch.

Security notes

    • Store PRIVOCIO_API_KEY in secrets, not source control
    • Do not send STT API keys to the browser — proxy through your backend
    • Review security controls for production agent deployments

Use case context: AI agents.

Frequently Asked Questions

Is this an official LangChain integration?

No. It is a compatible pattern using standard OpenAI client + LangChain. No Privocio LangChain package is required.

Can I use JavaScript instead of Python?

Yes. Transcribe with JavaScript STT, then call LangChain.js with the transcript string.

Why Agent output mode?

Verbatim STT is wordy. Agent mode trims filler and structure noise so your LLM chain consumes fewer tokens — see how clean transcripts cut LLM costs.

Can Privocio also run my LLM?

Privocio focuses on speech-to-text infrastructure. Keep your existing LLM provider in LangChain.

How is this different from the integration page?

/integrations/langchain is the short pattern reference. This post is the full build walkthrough with agent executor example and ops notes.

Conclusion

A LangChain voice agent needs reliable STT before the chain runs. Privocio slots in with a Whisper-compatible API, private hosting options, and Agent output mode for efficient prompts.

Next steps

langchainAI Agentsspeech-to-textpython