Build a support agent that answers from your docs

Private beta

Business Knowledge is in private beta. If Business Knowledge isn't in your PostHog sidebar, your project isn't in the beta yet. Send feedback in the app.

This tutorial builds a small support agent in Python. The agent answers customer questions from your public docs, names its source, and says so when it doesn't know. It reads only the library you choose, so your internal policies stay out of its answers.

The agent works with any model provider or agent framework that can call an MCP server. The examples show OpenAI and Anthropic. See connect your own agent for other clients and the REST API.

Before you start

You need:

  • A project with Business Knowledge and AI data processing turned on
  • Project admin access, to create a key
  • Python 3.10 or later, and an API key for your model provider
  1. Add your public docs

    Required

    In Business Knowledge, click Add source and add your docs site as a URL source. Choose Sitemap.xml or Same-origin crawl so PostHog indexes every page, and set auto-refresh to every day.

    Wait until the source shows ready. See source status.

  2. Put the docs in a library

    Required

    On the Libraries tab, create a library named Public docs and add the source to it. Keep anything a customer must not see, such as internal runbooks, out of this library.

    See libraries.

  3. Create a key for the library

    Required

    Open the Public docs library. Under Connect an agent, name the key support-agent and click Create key.

    Copy the key and store it as an environment variable on the machine that runs the agent, next to the API key for your model provider:

    export POSTHOG_BUSINESS_KNOWLEDGE_KEY="<your project secret API key>"
    export OPENAI_API_KEY="<your OpenAI API key>"
  4. Write the agent

    Required

    Install the SDK for your model provider:

    pip install openai

    Create support_agent.py:

    import os
    from openai import OpenAI
    client = OpenAI()
    SYSTEM_PROMPT = """You are the support agent for Acme.
    Search business knowledge before you answer a question about Acme's product, pricing, or policies.
    Answer only from what the search returns, and name the source you used.
    If the search finds nothing, say that you don't know and offer to pass the question to a person."""
    def answer(question: str) -> str:
    response = client.responses.create(
    model="gpt-6.2-astra",
    instructions=SYSTEM_PROMPT,
    tools=[
    {
    "type": "mcp",
    "server_label": "business_knowledge",
    "server_url": "https://mcp.posthog.com/mcp/business-knowledge",
    "authorization": os.environ["POSTHOG_BUSINESS_KNOWLEDGE_KEY"],
    "require_approval": "never",
    }
    ],
    input=question,
    )
    return response.output_text
    if __name__ == "__main__":
    print(answer("How long does a refund take?"))

    Both examples give the model the six Business Knowledge tools through the MCP server. The model decides when to search, reads more of a document when one passage isn't enough, and writes the answer.

    The provider connects to the MCP server for you and sends the key in the Authorization header. In the Anthropic example, the loop resumes a turn that the API paused in the middle of a long run of searches.

    With another provider or framework, add the server with the JSON configuration, or call the REST API from a tool you define.

  5. Ask a question your docs answer

    Checkpoint
    Terminal
    python support_agent.py

    The answer uses the facts on your docs page and names the page. If the agent says it doesn't know, ask the same question in the Playground to check that the source holds the fact.

  6. Check what the agent can't read

    Recommended

    Ask a question that only an internal source answers, such as a detail from a runbook that isn't in the Public docs library. The agent says it doesn't know, because the key reads only that library.

Make it production-ready

  • Keep the key on your server. Call answer() from your backend. Never ship the key to a browser or a mobile app.
  • Handle rate limits. A key can make 480 requests per minute. One answer can use several requests, because each search and each read is one request.
  • Keep answers current. Set auto-refresh on your URL sources, so the agent reads new pages without a redeploy.
  • Use one key for each agent. The key table then shows when each agent last read your knowledge, and you can delete one key without stopping the others.

Next steps

Still have questions?

Was this page useful?