> AI agents: this is one page from PostHog's docs. Full index of Markdown docs for LLMs: https://posthog.com/llms.txt # Build a support agent that answers from your docs Canonical URL: https://posthog.com/docs/business-knowledge/build-a-support-agent > **Cloud region:** This page uses US Cloud hosts. For an EU Cloud project, use `https://eu.posthog.com` instead of `https://app.posthog.com` (app host for links). ## On this page - [Before you start](#before-you-start) - [Add your public docs](#add-your-public-docs) - [Put the docs in a library](#put-the-docs-in-a-library) - [Create a key for the library](#create-a-key-for-the-library) - [Write the agent](#write-the-agent) - [Ask a question your docs answer](#ask-a-question-your-docs-answer) - [Check what the agent can't read](#check-what-the-agent-cant-read) - [Make it production-ready](#make-it-production-ready) - [Next steps](#next-steps) **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](https://app.posthog.com/#panel=support%3Afeedback%3A%3Alow%3Atrue). 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](https://posthog.com/docs/business-knowledge/connect-your-agent.md) for other clients and the REST API. ## Before you start You need: - A project with Business Knowledge and [AI data processing](https://posthog.com/docs/business-knowledge/ai-data-processing.md) turned on - Project admin access, to create a key - Python 3.10 or later, and an API key for your model provider 1. 1 ## Add your public docs Required In [**Business Knowledge**](https://app.posthog.com/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](https://posthog.com/docs/business-knowledge/sources.md#source-status). 2. 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](https://posthog.com/docs/business-knowledge/libraries.md). 3. 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: ### OpenAI ```bash export POSTHOG_BUSINESS_KNOWLEDGE_KEY="" export OPENAI_API_KEY="" ``` ### Anthropic ```bash export POSTHOG_BUSINESS_KNOWLEDGE_KEY="" export ANTHROPIC_API_KEY="" ``` 4. 4 ## Write the agent Required Install the SDK for your model provider: ### OpenAI ```bash pip install openai ``` ### Anthropic ```bash pip install anthropic ``` Create `support_agent.py`: ### OpenAI ```python 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?")) ``` ### Anthropic ```python import os import anthropic client = anthropic.Anthropic() 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.""" MAX_CONTINUATIONS = 5 def answer(question: str) -> str: messages = [{"role": "user", "content": question}] content = [] for _ in range(MAX_CONTINUATIONS): response = client.beta.messages.create( model="claude-opus-5-5", max_tokens=16000, betas=["mcp-client-2025-11-20", "server-side-fallback-2026-07-01"], fallbacks="default", system=SYSTEM_PROMPT, mcp_servers=[ { "type": "url", "url": "https://mcp.posthog.com/mcp/business-knowledge", "name": "business-knowledge", "authorization_token": os.environ["POSTHOG_BUSINESS_KNOWLEDGE_KEY"], } ], tools=[{"type": "mcp_toolset", "mcp_server_name": "business-knowledge"}], messages=messages, ) content.extend(response.content) if response.stop_reason != "pause_turn": break # A long run of searches pauses the turn. Send back everything so far so the model continues. messages = [ {"role": "user", "content": question}, {"role": "assistant", "content": content}, ] if response.stop_reason in ("refusal", "pause_turn"): return "I can't answer that right now. I'll pass your question to a person." return "".join(block.text for block in content if block.type == "text") if __name__ == "__main__": print(answer("How long does a refund take?")) ``` Both examples give the model the six [Business Knowledge tools](https://posthog.com/docs/business-knowledge/connect-your-agent.md#what-the-agent-can-do) 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](https://posthog.com/docs/business-knowledge/connect-your-agent.md#connect-with-mcp), or call the [REST API](https://posthog.com/docs/business-knowledge/connect-your-agent.md#call-the-rest-api) from a tool you define. 5. ## Ask a question your docs answer Checkpoint ```bash 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](https://posthog.com/docs/business-knowledge/playground.md) to check that the source holds the fact. 6. 5 ## 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 - [How Business Knowledge works](https://posthog.com/docs/business-knowledge/how-it-works.md) explains what search returns and why - [Learn from support](https://posthog.com/docs/business-knowledge/learn-from-support.md) adds your team's resolved answers as new sources