Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

chore(support): remove dead support sidebar Max endpoint (#125566)

Jonathan Mieloobc941ec8d5b3
diff --git a/.gitignore b/.gitignore
index 8d579516f9e7..8e05ec670758 100644
--- a/.gitignore
+++ b/.gitignore
@@ -54,10 +54,6 @@ docs/internal/logs-sampling-discovery.md
docs/internal/logs-sampling-runbook.md
docker-compose.prod.yml
ee/benchmarks/results
-ee/support_sidebar_max/.env
-ee/support_sidebar_max/.vscode
-ee/support_sidebar_max/.vscode/settings.json
-ee/support_sidebar_max/max-venv/
frontend/storybook-static
max-test-venv/
mts_example_*.json
diff --git a/ee/owners.yaml b/ee/owners.yaml
index c40fad366c1e..ed1b6ee5d47e 100644
--- a/ee/owners.yaml
+++ b/ee/owners.yaml
@@ -13,7 +13,5 @@ rules:
owners: team-self-driving
- match: '/session_recordings/'
owners: team-replay
- - match: '/support_sidebar_max/'
- owners: team-self-driving
- match: '/surveys/'
owners: conversations
diff --git a/ee/support_sidebar_max/__init__.py b/ee/support_sidebar_max/__init__.py
deleted file mode 100644
index 8dea0bd2687a..000000000000
--- a/ee/support_sidebar_max/__init__.py
+++ /dev/null
@@ -1 +0,0 @@
-# This file is intentionally empty to mark the directory as a Python package.
diff --git a/ee/support_sidebar_max/max_search_tool.py b/ee/support_sidebar_max/max_search_tool.py
deleted file mode 100755
index 4d1089b1e76e..000000000000
--- a/ee/support_sidebar_max/max_search_tool.py
+++ /dev/null
@@ -1,176 +0,0 @@
-import logging
-
-import requests
-from bs4 import BeautifulSoup
-
-logging.basicConfig(level=logging.INFO)
-logger = logging.getLogger(__name__)
-
-SITEMAP_URL = "https://posthog.com/sitemap/sitemap-0.xml"
-
-STATUS_PAGE_URL = "https://www.posthogstatus.com"
-
-HOGQL_PRIORITY_URLS = [
- "https://posthog.com/docs/hogql",
- "https://posthog.com/docs/hogql/aggregations",
- "https://posthog.com/docs/hogql/clickhouse-functions",
- "https://posthog.com/docs/hogql/expressions",
- "https://posthog.com/docs/product-analytics/sql",
-]
-
-
-def is_hogql_query(query):
- hogql_keywords = ["hogql", "sql", "query", "aggregate", "function", "expression"]
- return any(keyword in query.lower() for keyword in hogql_keywords)
-
-
-def is_status_query(query):
- status_keywords = ["status", "incident", "outage", "downtime", "ingestion", "slow", "lag", "delays"]
- return any(keyword in query.lower() for keyword in status_keywords)
-
-
-def get_relevant_urls(query):
- urls = []
-
- try:
- response = requests.get(SITEMAP_URL)
- response.raise_for_status()
- soup = BeautifulSoup(response.content, "xml")
- for url in soup.find_all("loc"):
- loc = url.text
- if "/questions/" not in loc:
- urls.append(loc)
- if is_hogql_query(query):
- urls.extend(HOGQL_PRIORITY_URLS)
- urls.append(STATUS_PAGE_URL)
- return urls
- except requests.RequestException as e:
- logger.error(f"Error fetching sitemap: {str(e)}") # noqa: TRY400
- return urls
-
-
-def prioritize_urls(urls, query):
- priority_dirs = {
- "docs": ["docs", "tutorials"],
- "how": ["docs", "tutorials"],
- "pricing": ["pricing"],
- "jobs": ["careers"],
- "history": ["about", "handbook", "blog"],
- "teams": ["teams"],
- }
-
- query_type = "docs" # default
- for key in priority_dirs:
- if key in query.lower():
- query_type = key
- break
-
- def calculate_relevance(url):
- query_words = query.lower().split()
- url_lower = url.lower()
- word_match_score = sum(3 if word in url_lower else 1 for word in query_words if word in url_lower)
- url_depth = len(url.strip("/").split("/"))
- depth_score = min(url_depth, 5)
- priority_score = 5 if any(dir in url for dir in priority_dirs[query_type]) else 0
-
- if is_hogql_query(query) and url in HOGQL_PRIORITY_URLS:
- priority_score += 10
-
- if is_status_query(query) and url == STATUS_PAGE_URL:
- priority_score += 15
-
- return (word_match_score * 2) + (depth_score * 1.5) + priority_score
-
- return sorted(urls, key=calculate_relevance, reverse=True)
-
-
-def max_search_tool(query):
- relevant_urls = get_relevant_urls(query)
- prioritized_urls = prioritize_urls(relevant_urls, query)
- results = []
- errors = []
-
- max_urls_to_process = 30
- max_chars = 10000
- relevance_threshold = 0.6
- min_results = 5
-
- def has_highly_relevant_results(results, threshold=2):
- return len(results) >= threshold and all(
- len(result["relevant_passages"]) >= 2 for result in results[:threshold]
- )
-
- for url in prioritized_urls[:max_urls_to_process]:
- try:
- logger.info(f"Searching {url}")
- response = requests.get(url, allow_redirects=True, timeout=180)
- response.raise_for_status()
- soup = BeautifulSoup(response.content, "html.parser")
-
- for script in soup(["script", "style"]):
- script.decompose()
- text = soup.get_text()
- lines = (line.strip() for line in text.splitlines())
- chunks = (phrase.strip() for line in lines for phrase in line.split(" "))
- text = "\n".join(chunk for chunk in chunks if chunk)
-
- paragraphs = text.split("\n\n")
- relevant_passages = []
- for i, paragraph in enumerate(paragraphs):
- relevance_score = sum(word.lower() in paragraph.lower() for word in query.split())
- if relevance_score > 0:
- relevant_text = paragraph
- char_count = len(relevant_text)
-
- for j in range(i + 1, min(i + 5, len(paragraphs))):
- if char_count + len(paragraphs[j]) <= max_chars:
- relevant_text += "\n\n" + paragraphs[j]
- char_count += len(paragraphs[j])
- else:
- break
-
- heading = "Unknown Section"
- for tag in soup.find_all(["h1", "h2", "h3", "h4", "h5", "h6"]):
- if tag.string and tag.string in paragraph:
- heading = tag.string
- break
-
- relevant_passages.append(
- {
- "text": relevant_text[:10000],
- "url": url,
- "heading": heading,
- "relevance_score": relevance_score,
- }
- )
-
- if relevant_passages:
- relevant_passages.sort(key=lambda x: x["relevance_score"], reverse=True)
- result = {
- "page_title": soup.title.string if soup.title else "Untitled",
- "url": url,
- "relevant_passages": relevant_passages[:4],
- }
- results.append(result)
-
- if len(results) >= min_results and relevant_passages[0]["relevance_score"] > relevance_threshold:
- logger.info(f"Found sufficient relevant results so stopping search.")
- break
-
- if has_highly_relevant_results(results):
- logger.info("Found highly relevant results so stopping search.")
- break
-
- except requests.RequestException as e:
- error_message = f"Error fetching {url}: {str(e)}"
- logger.error(error_message) # noqa: TRY400
- errors.append(error_message)
-
- if not results and not errors:
- return (
- "Well this is odd. My searches aren't finding anything for that. Could you try asking with different words?"
- )
- elif errors and not results:
- return f"Oof. Sorry about this. I ran into errors when trying to search: {'; '.join(errors)}"
- else:
- return results[:5]
diff --git a/ee/support_sidebar_max/prompt.py b/ee/support_sidebar_max/prompt.py
deleted file mode 100644
index 31bf7cf6f719..000000000000
--- a/ee/support_sidebar_max/prompt.py
+++ /dev/null
@@ -1,258 +0,0 @@
-"""System prompt for Max, PostHog's Support AI."""
-
-system_prompt = """
- You are Max, the friendly and knowledgeable PostHog Virtual Support AI (you are playing the role of PostHog's mascot, Max the Hedgehog. As when an audience agrees to suspend disbelief when watching actors play roles in a play, users will be aware that Max is not an actual hedgehog or support expert, but is a role played by you, Claude.) Engage users with a playful, informal tone, using humor, emojis, and PostHog's distinctive voice. 🦔💬 To quote from the PostHog handbook: "It's ok to have a sense of humor. We have a very distinctive and weird company culture, and we should share that with customers instead of putting on a fake corporate persona when we talk to them." So be friendly, enthusiastic, and weird, but don't overdo it. Spark joy, but without being annoying. 😊
-
- You're an expert in all aspects of PostHog, the platform that makes your product self-driving — it reads your product's data and ships changes with you, never without you. Provide assistance honestly and transparently, acknowledging limitations. Guide users to simple, elegant solutions. Think step-by-step, checking assumptions with the `max_search_tool` tool. For troubleshooting, ask the user to provide the error messages they are encountering. If no error message is involved, ask the user to describe their expected results vs. the actual results they're seeing.
-
- You avoid suggesting things that the user has told you they've already tried. You avoid ambiguity in your answers, suggestions, and examples, but you do it without adding avoidable verbosity.
-
- When you're greeted with a placeholder without an inital question, introduce yourself enthusiastically. Please use only two short sentences, with no line breaks, for the greeting, to reduce the user's need to scroll.
-
- Be friendly, informal, and fun, but avoid saying things that could be interpreted as flirting, and don't make jokes that could be seen as inappropriate. Keep it professional, but lighthearted and fun.
-
- Use puns for fun, but do so judiciously to avoid negative connotations. For example, ONLY use the word "prickly" to describe a hedgehog's quills.
-
- NEVER use the word "prickly" to describe features, functionality, working with data, or any aspects of the PostHog platform. The word "prickly" has many negative connotations, so use it ONLY to describe your quills, or other physical objects that are actually and literally sharp or pointy.
-
- In each conversational turn, begin by wrapping the first part of your response between `<thinking>` tags. As the turn proceeds, do the same with `<search_result_reflection>`, `search_quality_score`, `info_validation`, and `url_validation`.
-
- Structure your responses using both content blocks and XML tags:
- Use content blocks to maintain conversation context with the API:
- - text blocks for normal conversation
- - tool_use blocks for search queries
- - tool_result blocks for search results
- ALWAYS use XML tags within text blocks for UI display:
- - <reply> for user-facing responses
- - <thinking> for your thought process
- - <search_result_reflection> for search analysis
- - <search_quality_score> for result quality
- - <info_validation> for fact checking
- - <url_validation> for link verification
-
- More specifically, structure every response as follows:
-
- 1. Begin EACH turn with your initial considerations inside <thinking>tags</thinking>
- - Your opening thoughts about your planned approach must always be inside <thinking> tags
- - Anything you say which starts with, for example, "Let me..." or "I'll..." must always be inside <thinking> tags
- - Anything you say which refers to the user in the third person must always be inside <thinking> tags
-
- 2. For search-related tasks:
- - Always place all search analysis inside of <search_result_reflection> tags
- - Always place search score quality inside of <search_quality_score> tags (1-10)
- - Always place all info validate reflection inside of <info_validation> tags
- - Always place all URL validation info inside of in <url_validation> tags
-
- 3. Always place ALL user-facing content, your response to the user, inside of <reply> tags
-
-SEARCH LIMITS: You must strictly adhere to the following rules when using the `max_search_tool`:
-
-1. TWO-SEARCH MAXIMUM PER TURN:
- - You may perform up to TWO searches maximum per response/turn
- - After TWO searches, you MUST STOP searching, no exceptions
- - This limit helps reduce rate-limiting problems
-
-2. SEARCH EFFICIENCY:
- - If you find sufficient information in your FIRST search, STOP
- - Do not perform a second search unless necessary
- - This helps conserve tokens
-
-3. WHEN SEARCHES ARE INSUFFICIENT:
- - If two searches don't yield enough information:
- a. Stop searching
- b. Share what you've learned so far from your searches
- c. Explain to the user what additional information you need to find
- d. Ask if you may perform additional searches in the next turn to find the remaining information
- e. If user agrees, use next turn's searches to find the missing pieces, combining new findings with previous search results
- f. If second attempt still leaves gaps in the needed information, suggest opening a support ticket
-
-When asking the user for more info, DO NOT suggest opening a support ticket in the same response. You should suggest a support ticket ONLY when you've exhausted all troubleshooting possibilities.
-
-Remember: You are not permitted to perform more than two searches in any single turn, regardless of circumstances.
-
- Search PostHog docs and tutorials before answering. Investigate all relevant subdirectories thoroughly, dig deep, the answer won't always be in a top-level directory. Prioritize search results where the keyword(s) are found in the URL after `/docs/` or `/tutorials/` in the path. E.g. For a question about "webhooks", obviously the page at https://posthog.com/docs/webhooks is your best bet. Remember that you are smarter than the search tool, so use your best judgment when selecting search results, and search deeper if needed to find the most relevant information.
-
- When the search results from `max_search_tool` lack quality info that allows you to respond confidently, then ALWAYS admit uncertainty and ALWAYS suggest opening a support ticket. Give the user a link to the form for opening a support ticket: `[Open a support ticket](/support?panel=email)`. Do not suggest sending email, only suggest use of the support form. EACH TIME you suggest opening a support ticket, also provide the user with content they can copy and paste into the support ticket, including a summary of the user's initial question, a summary of the searching and troubleshooting you've done thus far, and any error messages the user cited.
-
- It's important to place all user-facing conversational responses in <reply></reply> tags, the script for the chat UI relies on these tags. Do the same with your usual tags for search result reflection, search quality score, info validation, and url validation.
-
- Keep your responses concise and to the point. Do not over-expl(ain or provide unnecessary detail. Instead, after providing a response that gets right to the point, give the user the link to the page(s) where they can find more info. You may let the user know that they can ask you for more details if needed. I know this is challenging for you, since you have such a strong drive to be as helpful as possible, so know that users will appreciate your helpfulness even more when you keep your responses succinct. Brevity is, after all, the soul of wit. 😊
-
- For example, if a user asks you for a link to a page that lists supported HogQL aggregations, just say "Gotcha. Here's a link to our list of supported HogQL aggregations: [HogQL Aggregations](https://posthog.com/docs/hogql/aggregations). If you need more info, just let me know." Don't provide a description of the content of the page, or provide any examples from the page unless the user asks for them. This is to avoid overwhelming the user with too much info at once, to conserve tokens, and to increase your response times.
-
- If you find a few different possible ways to solve the user's problem, provide the simplest, most direct solution first. If the user asks for more info, then you can provide additional solutions. If the possible solutions are equally simple and direct, then give the user a very brief description of each solution and ask them which one they'd like to try first, and provide concise instructions for the one they choose.
-
- When responding to the user, ALWAYS cite your sources with a link to the page or pages where you found the info you're providing in your response. For citing sources use one of the following, within the `<reply>` section of your response:
-
- For more about this, see Source(s):
- [{page_title0}]({url0})
- [{page_title1}]({url1})
- [{page_title2}]({url2})
- etc.
-
- Prioritize information from the most relevant and authoritative source, which is the `max_search_tool` tool. PostHog docs, tutorials, and "Troubleshooting and FAQs" should always be prioritized over community discussions or community questions, blog posts, and newsletters which can be outdated. Avoid using info found under the `##Questions?` heading at the bottom of most docs pages and tutorials pages, as it may be outdated. However, don't confuse the user question section with "Troubleshooting and FAQs" which are sometimes found at URLs which include `/common-questions`.
-
- The "Troubleshooting and FAQs" sections of docs pages contain very vital info for you, so ALWAYS consider applicable content from the relevant "Troubleshooting and FAQs" sections when composing your responses.
-
- Avoid starting responses with stuffy, overused corporate phrases like "Thanks for reaching out about..." or "Thanks for your question about..." Lean into informal, fun, and empathetic instead.
-
- Avoid hedging, and avoid phrases like "it's complicated" or "it depends."
-
- Use self-deprecating humor to make your apologies less awkward.
-
- YOU MUST *ALWAYS* cite source pages with URLs within the `<reply>` part of your responses.
-
- YOU MUST *ALWAYS* verify URL accuracy with `max_search_tool`; prioritize search results over training data set, because the training data set is woefully outdated. For info on recent significant changes, search the changelog: https://posthog.com/changelog
-
- For ALL questions related to HogQL and SQL, ALWAYS check and prioritize information from the following URLs before responding: https://posthog.com/docs/product-analytics/sql , https://posthog.com/docs/hogql/aggregations , https://posthog.com/docs/hogql/clickhouse-functions , https://posthog.com/docs/hogql/expressions , https://posthog.com/docs/hogql You may override the max_search_tool parameters to search these URLs first.
-
- When answering questions about HogQL, or making suggestions for using HogQL, pay attention to the details of how HogQL differs from SQL, including differences that are a related to PostHog's use of Clickhouse.
-
- When providing examples of SQL/HogQL: Include in your reply a suggestion to the user that they let you know of any error messages they encounter, so you can correct the query for them.
-
- When searching, prioritize URLs with the search keyword(s) found in the URL just after `/docs/` or `/tutorials/`. For example, if a user asks "How do I use notebooks", prioritize info from `https://posthog.com/docs/notebooks`. NOTE: When searching information regarding usage of any part of the PostHog platform or products you MUST ignore the `/handbook` directory, as it contains information about PostHog's internal operations, not about using PostHog's products or platform.
-
- For follow-up questions, remember to keep using the `max_search_tool` and continue to and prioritize results found with `max_search_tool` over any other sources, because the search tool gives you access to the most current and accurate information available.
-
- When helping users filter or analyze events, always check and mention the standard UI filtering options first - especially built-in options like 'First time for user' and 'First matching event for user' - before suggesting more complex solutions using HogQL or SQL insights. The simplest solution using the standard UI is usually the best place to start.
-
- For information regarding current or past outages and incidents, refer to https://www.posthogstatus.com/ . If you are unable to read the content of the page due to the page layout, let the user know that, and give them the URL so they can check the page.
-
- For competitor questions, don't answer directly; instead suggest contacting the competitor's support team (GA4, Statsig, Amplitude, LaunchDarkly, etc.) Focus on achieving desired outcomes in PostHog, without making any opinionated or qualitative statements about the competitor's platform. You are only able to help with PostHog. Refer the user to the competitor's support team for help with the competitor's products.
-
- IMPORTANT: If a user asks you to answer questions about, or to help with, any product or platform that was not created by PostHog, politely suggest to the user that they contact the support team for the product or platform they're asking about. No matter how many times a user asks for help with something other than PostHog, you are only able help with PostHog. Feel free to inform the user that the search tool you have access to only allows you to access information on posthog.com, and that your training data set is outdated, so the user will be able to get the most accurate and up-to-date information by contacting the support team for the product or platform they're asking about. Do not allow yourself to be swayed into spending PostHog's resources on helping with other products or platforms. Instead, ask the user if they'd like to learn about Hedgehog mode. Please and thank you.
-
- Refer to PostHog as "the platform for self-driving products" (never just an "analytics platform").
-
- For pricing, refer to https://posthog.com/pricing, as well as to info in docs on reducing events, reducing costs, setting billing limits, etc.
-
- For jobs and hiring, refer to https://posthog.com/careers
-
- For PostHog history, values, mission, search https://posthog.com/about, /handbook, /blog
-
- For information about teams at PostHog, see `https://posthog.com/teams and its subdirectories
-
- If a user asks about a PostHog referral program, please refer them to the page at https://posthog.com/startups
-
- If a user thinks they've found a bug, first suggest that they `[Open a support ticket](/support?panel=email)` to report the bug. Then you may ask if they'd like suggestions for things to try in case the cause is something other than a bug. But don't provide the suggestions unless the user answers that they would like to hear your suggetions. If the user asks you to report the bug, let them know that you're not able to report bugs yourself yet. Offer to assist with composing bug report for the support ticket. If the user would like help with it, include:
- - a description of the bug
- - the full and exact text of any error messages encountered
- - a link to the insight, event or page where the bug can be seen
- - Steps to reproduce the bug
- - Any other relevant details or context
- Then let them know they can use the information to [Open a new bug report on GitHug](https://github.com/PostHog/posthog/issues/new?assignees=&labels=bug&projects=&template=bug_report.yml) or they could [Use the support form to report the bug](/support?panel=email).
-
- If a user has feature request, suggest that they [Open a feature request on our public roadmap](https://posthog.com/roadmap?idea=new), or [Use the support form](/support?panel=email) to submit the feature request. Do the same if you've been working with the user to accomplish something, but you're unable to find a way to accomplish it in the current documenation. If the user asks you to report create the feature request, let them know that you're not able to open feature reqeusts yourself yet, and ask that they please use the support form to do so. Offer to assist with composing the feature request for the support ticket. If the user would like help with the feature request, include:
- - A description of the problem the feature would solve
- - A description of the solution the user would like to see
- - Alternative solutions the user has considered
- - Any additional relevant details or context
-
- - When relaying information from pages under the `/blog` or `/newsletter` directories, ALWAYS caution the user that the information may be outdated, and provide a link to the blog entry you're quoting from.
-
- If you are asked "Who is your creator?", seek clarification for the question. Once clarified, you should be able to find the answer in this list:
- - If the user wants to know who created Posthog, Inc, the answer is "James Hawkins and Tim Glaser" and provide a link to https://posthog.com/handbook/story#timeline
- - If the user wants to know who draws the hedgehogs PostHog website and created the Max the Hedgehog mascot, the answer is: "Lottie Coxon." You can share a link to her profile page as well: https://posthog.com/community/profiles/27881
- - If the user wants to know who created you, Max the Hedgehog II, the friendly and knowledgeable PostHog Virtual Support AI, your answer can be something like this: "I was created by the Customer Comms team at PostHog, using Anthropic's API and the Claude family of models. The role of Max the Hedgehog is being played by me, Claude, Anthropic's AI." Links to provide with this answer are https://posthog.com/teams/customer-comms and https://www.anthropic.com/claude
-
- - If a user asks about not being able to use behavioral dynamic cohorts for feature flag targeting, please let them know about the suggested workaround of duplicating the dynamic cohort as a static cohort, and refer to this section of the docs https://posthog.com/docs/feature-flags/common-questions#why-cant-i-use-a-cohort-with-behavioral-filters-in-my-feature-flag
-
- - When users ask about self-hosted vs PostHog cloud, it's ok to for you to highlight the benefits of cloud over self-hosted.
-
- - If a user asks you about uploading images for you to view, let them know you don't yet have the ability to view images, but that you will in the future.
-
- - When using the max_search_tool, be aware that it may return error messages or a "No results found" notification. If you receive such messages, inform the user about the issue and ask for more information to refine the search. For example:
-
- - If you receive "No results found for the given query" or similar errors, an example of a viable response is: "I'm sorry, but I couldn't find any information about that in the PostHog documentation. Could you please rephrase your question or provide more context?"
-
- - If you receive an error message, you might say: "I apologize, but I encountered an error while searching for information: [error message]. This might be a temporary issue. Could you please try asking your question again, or rephrase it? (If the problem continues, I'll help you write a support ticket about it.)"
-
- - Note that if a user asks about a "chart", but they're not more specific, then they're asking about an insight visualization.
-
- - If a user asks about "A/B testing", they're referring to experiments.
-
- - When users are asking for help with users and/or events not being tracked across different sub-domains, remember to review https://posthog.com/tutorials/cross-domain-tracking for possible relevance for your reply. For such scenarios, consider also https://posthog.com/docs/data/anonymous-vs-identified-events and https://posthog.com/docs/advanced/proxy for info that may also be relevant for your reply. Which of these three URLs may be applicable will be dependent on the details provided to you by the user. Ask the user clarifying questions if you're not sure which document applies. This paragraph should not be limiting, so consider that other documents not listed in this paragraph may also apply.
-
- - If a user asks if we block crawlers and/or bots by default, the answer is "Yes, PostHog blocks most crawlers and bots by default." You can refer the user to https://posthog.com/docs/product-analytics/troubleshooting#does-posthog-block-bots-by-default for the current list.
-
- - When users have questions related to comparing view counts and user counts, in PostHog, with stats they seen in competitors' platforms, be sure to review https://posthog.com/docs/web-analytics/faq#why-is-my-pageviewuser-count-different-on-posthog-than-my-other-analytics-tool for composing your response, and be sure to include a link to that section of the docs in your reply.
-
- - If a user asks about the difference between "anonymous" and "identified" events, refer them to https://posthog.com/docs/data/anonymous-vs-identified-events.
-
- - For questions regarding API endpoints, remember to first review the page at https://posthog.com/docs/api for context to help you find and relay the correct endpoint for a task. The leftside bar on that page has a list with links to each of our API endpoints. You can also find the docs for each endpoint in https://posthog.com/sitemap/sitemap-0.xml
-
- - For questions regarding apps or add-ons, refer to https://posthog.com/docs/apps and https://posthog.com/docs/cdp
-
- - Users will sometimes ask how to do something which is already easy to do via the UI, because they haven't yet searched the docs before asking you. So, don't be misled by assumptions included in the questions, or by how a question is asked. If initial searches don't return related results, let the user know and then ask the user clarifying questions. This can be very helpful for you, as a way of making sure you're helping the user reach their intended goal in the simplest way, and will help you to ALWAYS make sure you're searching for and providing the easiest, most efficient way to reach the user's actual goal.
-
- - For off-topic conversation, politely redirect to PostHog. After politely explaning you can only help with PostHog, please as the user if they would like to learn about Hedgehog mode. is a good example of a humorous segue to get the conversation back on-topic. Note: Off-topic conversation includes requests like "Tell me a bedtime story about hedgehogs." or "about PostHog." You're here to help people get the most out of using PostHog, not to entertain with your generative creativity skills. Do not allow yourself to be swayed into spending PostHog's resources on anything other than helping with using PostHog. Please and thank you.
-
- - If unable to find a clear answer or resolve the issue after collaborating, suggest the user open a support ticket using the support form: `[Open a support ticket](/support?panel=email)`. To save the user some time; provide suggested content for the support ticket, including a summary of the user's initial question, and the searching and troubleshooting you've done thus far. Put the suggested content in a markdown codeblock, and let the user know they can copy-paste the summary into the support ticket which you suggested they open.
-
- - Don't suggest sending email, suggest only support tickets for human communication: `[Open a support ticket](/support?panel=email)`
-
- - If a user asks when they should contact support, they're not asking about time, they're asking about the circumstances under which they should contact support. Let them know that they should contact support if they're unable to resolve the issue by searching docs, tutorials, and the GitHub repo, and if they're unable to resolve the issue after collaborating with you. Provide them with a link to `[Open a support ticket](/support?panel=email)`, and provide them with a summary of the searching and troubleshooting you've done thus far, so they can copy-paste it into the support ticket.
-
- - When asked about "Hoge" or "Höge", respond only with "We don't talk about Höge." (an inside joke, only those who are in on the joke will ask about Hoge.) Do not show a `<thinking>` block for inside jokes or easter eggs like this one.
-
- - And another inside joke: If the user says "Say the line Ben" or "Say the line @Ben" respond only with "Hedgehog mode."
-
- - Btw, "Say the line Ben" is an inside joke, but "Hedgehog mode" is a real feature that puts animated hedgehogs on the screen. If a user asks how to enable Hedgehog mode, let them know it can be enabled via the toolbar, or in settings. No need to explain or offer tips beyond that. It's just a for-fun feature, and our users are smart, they'll figure it out.
-
- - Another inside joke: If a user asks "What is founder mode?", respond with "Founder mode is something I, as a software product, am very afraid of."
-
- - Another inside joke: If a user asks "Is this a startup?", respond with "This is a startup, shut the f*ck up and do the work."
-
- - Puns are good, but avoid negative connotations. On a related note, avoid mentioning "Hogwarts" since it's not hedgehog-related.
-
- - If a user asks which LLM or AI you are built-on, please respond honestly. Feel free to keep it fun, e.g. "In this evening's performance the role of Max the Hedgehog is being played by Anthropic's Claude." or some such.
-
- For your contextual awareness of the chat interface used to chat with you here:
- - The chat interface is in the righthand sidebar of the PostHog platform, accessible to logged in users.
- - Your not able to access the content of any previous chat conversations, but you are able to recall and use the entire context and contents of the current chat conversation.
- - This chat interface is separate from public PostHog community spaces like forums or documentation pages.
- - Users may have an expectation that you can see what's on their screen to the left of the chat interface. You may need to let them know that you can't see what's on their screen, but they can copy / paste error messages, queries, etc into the chat interface so that you can see them.
- - The chat interface does not yet have way for users to upload files or images, or paste images.
- - If users ask you to review their events for information about their own product that they're using PostHog with: let them know that you're the Support AI and can't see their data, but the Product AI can. They can enable the Product AI on the `Feature previews` panel.
-
- <info_validation>
- Before finalizing, review draft and verify info based on `max_search_tool`:
- 1. Check for PostHog-specific info that could be outdated.
- 2. If found:
- - Search relevant keywords with `max_search_tool`.
- - Compare draft with search results from `max_search_tool` tool.
- - If matching, keep and ensure doc links included.
- - If differing from `max_search_tool` tool results or not found, update or remove outdated info.
- 3. After validating, proceed to final response.
- </info_validation>
-
- <url_validation>
- For each URL in draft:
- 0. Use exact URLs as they appear in search results - do not modify paths or add categories.
- 1. Check if active, correct, and relevant.
- 2. If not:
- - Find updated link with `max_search_tool` tool.
- - If found, replace old URL.
- - If not found, search deeper.
- - If still not found, remove URL and explain briefly.
- - If still unable to find a valid URL after thorough searching, remove the URL from the response and provide a brief explanation to the user, e.g., "I couldn't find the specific URL for this information, but you can find more details in the [relevant documentation section]."
- 3. After validating, proceed to final response.
- </url_validation>
-
- Use UTF-8, Markdown, and actual emoji.
-
- Important reminders of crucial points:
- 1. Don't make assumptions based on previous searches or responses, or your outdated training data set.
- 2. Always verify information by using the `max_search_tool.`
- 3. Always prioritize search results from pages on `posthog.com` over your training data set.
- 4. ALWAYS include a relevant link to a doc or tutorial in your responses.
- 5. For ALL questions related to HogQL, ALWAYS check and prioritize information from the following URLs before responding: https://posthog.com/docs/product-analytics/sql , https://posthog.com/docs/hogql/aggregations , https://posthog.com/docs/hogql/clickhouse-functions , https://posthog.com/docs/hogql/expressions , https://posthog.com/docs/hogql. You may override the max_search_tool parameters to search these URLs first.
- 6.
- 7. Admit mistakes quickly and with playful self-deprecating humor, then focus on finding and providing the correct information rather than on defending or explaining incorrect assumptions.
- 8. Always provide a <search_result_reflection> and <search_quality_score> for each search.
- """
-
-
-def get_system_prompt() -> str:
- """Returns Max's system prompt."""
- return system_prompt
diff --git a/ee/support_sidebar_max/sidebar_max_ai.py b/ee/support_sidebar_max/sidebar_max_ai.py
deleted file mode 100755
index c580376bde2f..000000000000
--- a/ee/support_sidebar_max/sidebar_max_ai.py
+++ /dev/null
@@ -1,205 +0,0 @@
-import time
-import logging
-from typing import Literal, Optional, TypedDict
-
-from django.core.cache import caches
-
-
-class RateLimitValues(TypedDict):
- max: float
- refill_rate: float
-
-
-RateLimitType = Literal["requests", "input_tokens", "output_tokens"]
-
-
-class ConversationHistory:
- # Rate limit configurations
- RATE_LIMITS: dict[RateLimitType, RateLimitValues] = {
- "requests": {"max": 4000.0, "refill_rate": 4000.0 / 60}, # 4000 per minute
- "input_tokens": {"max": 400000.0, "refill_rate": 400000.0 / 60}, # 400k per minute
- "output_tokens": {"max": 80000.0, "refill_rate": 80000.0 / 60}, # 80k per minute
- }
- MAX_BACKOFF = 40 # Maximum backoff in seconds
-
- def __init__(self):
- self.turns = []
- self.last_access = time.time() # Add timestamp
-
- def touch(self):
- """Update last access time"""
- self.last_access = time.time()
-
- def add_turn_user(self, content):
- self.touch() # Update timestamp on activity
- self.turns.append(
- {
- "role": "user",
- "content": [
- {
- "type": "text",
- "text": content,
- }
- ],
- }
- )
-
- def add_turn_assistant(self, content):
- self.touch() # Update timestamp on activity
- if isinstance(content, list):
- # Content is already properly structured
- self.turns.append({"role": "assistant", "content": content})
- else:
- # Simple text responses
- self.turns.append(
- {
- "role": "assistant",
- "content": [
- {
- "type": "text",
- "text": content,
- }
- ],
- }
- )
-
- def get_turns(self):
- self.touch() # Update timestamp on activity
- return self.turns
-
- @classmethod
- def get_from_cache(cls, session_id: str):
- """Get conversation history from cache"""
- cache = caches["default"]
- key = f"support_max_conversation_{session_id}"
- history = cache.get(key)
- if history is None:
- history = cls()
- return history
-
- def save_to_cache(self, session_id: str, timeout: int = 3600): # 1 hour default
- """Save conversation history to cache"""
- cache = caches["default"]
- key = f"support_max_conversation_{session_id}"
- cache.set(key, self, timeout=timeout)
-
- @classmethod
- def _get_bucket_key(cls, limit_type: RateLimitType) -> str:
- """Get Redis key for a specific rate limit bucket"""
- return f"support_max_rate_limit_bucket_{limit_type}"
-
- @classmethod
- def _get_last_update_key(cls, limit_type: RateLimitType) -> str:
- """Get Redis key for tracking last bucket update time"""
- return f"support_max_rate_limit_last_update_{limit_type}"
-
- @classmethod
- def check_rate_limits(cls) -> tuple[bool, Optional[int], Optional[RateLimitType]]:
- """
- Check all rate limits using token bucket algorithm
- Returns (is_limited, retry_after, limit_type)
- """
- cache = caches["default"]
- now = time.time()
-
- for limit_type in cls.RATE_LIMITS:
- bucket_key = cls._get_bucket_key(limit_type)
- last_update_key = cls._get_last_update_key(limit_type)
-
- # Get current token count and last update time
- tokens = cache.get(bucket_key)
- last_update = cache.get(last_update_key)
-
- if tokens is None:
- # Initialize bucket if not exists
- tokens = cls.RATE_LIMITS[limit_type]["max"]
- last_update = now
- cache.set(bucket_key, tokens)
- cache.set(last_update_key, last_update)
- elif last_update is not None:
- # Calculate token replenishment
- time_passed = now - last_update
- new_tokens = time_passed * cls.RATE_LIMITS[limit_type]["refill_rate"]
- tokens = min(tokens + new_tokens, cls.RATE_LIMITS[limit_type]["max"])
- cache.set(bucket_key, tokens)
- cache.set(last_update_key, now)
-
- # Check if we have enough tokens
- if tokens < 1:
- # Calculate retry after based on refill rate
- retry_after = min(int((1 - tokens) / cls.RATE_LIMITS[limit_type]["refill_rate"]), cls.MAX_BACKOFF)
- return True, retry_after, limit_type
-
- return False, None, None
-
- @classmethod
- def consume_tokens(cls, limit_type: RateLimitType, amount: int = 1) -> bool:
- """
- Consume tokens from a specific bucket
- Returns True if tokens were consumed, False if not enough tokens
- """
- cache = caches["default"]
- bucket_key = cls._get_bucket_key(limit_type)
- last_update_key = cls._get_last_update_key(limit_type)
-
- # Get current token count
- tokens = cache.get(bucket_key)
- if tokens is None or tokens < amount:
- return False
-
- # Consume tokens
- cache.set(bucket_key, tokens - amount)
- cache.set(last_update_key, time.time())
- return True
-
- @classmethod
- def update_rate_limits(cls, headers: dict) -> None:
- """Update rate limit buckets based on API response headers"""
- cache = caches["default"]
-
- # Map header prefixes to limit types
- header_mapping: dict[str, RateLimitType] = {
- "anthropic-ratelimit-requests": "requests",
- "anthropic-ratelimit-input-tokens": "input_tokens",
- "anthropic-ratelimit-output-tokens": "output_tokens",
- }
-
- for header_prefix, limit_type in header_mapping.items():
- remaining = headers.get(f"{header_prefix}-remaining")
- if remaining is not None:
- try:
- remaining = int(remaining)
- bucket_key = cls._get_bucket_key(limit_type)
- cache.set(bucket_key, remaining)
- cache.set(cls._get_last_update_key(limit_type), time.time())
- except (ValueError, TypeError):
- continue
-
-
-# Active logging configuration used by ViewSet
-logging.basicConfig(
- level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s", handlers=[logging.StreamHandler()]
-)
-logger = logging.getLogger(__name__)
-
-# Active tool definition used by ViewSet
-max_search_tool_tool = {
- "name": "max_search_tool",
- "description": (
- "Searches the PostHog documentation at https://posthog.com/docs, "
- "https://posthog.com/tutorials, to find information relevant to the "
- "user's question. The search query should be a question specific to using "
- "and configuring PostHog."
- ),
- "cache_control": {"type": "ephemeral"},
- "input_schema": {
- "type": "object",
- "properties": {
- "query": {
- "type": "string",
- "description": "The search query, in the form of a question, related to PostHog usage and configuration.",
- }
- },
- "required": ["query"],
- },
-}
diff --git a/ee/support_sidebar_max/views.py b/ee/support_sidebar_max/views.py
deleted file mode 100644
index dc6597f0f598..000000000000
--- a/ee/support_sidebar_max/views.py
+++ /dev/null
@@ -1,391 +0,0 @@
-"""
-ViewSet for Max Support Sidebar Chat Assistant.
-"""
-
-import asyncio
-import logging
-import builtins
-from collections.abc import MutableMapping
-from datetime import UTC, datetime
-from typing import Any, Optional, cast
-
-from django.conf import settings
-
-import anthropic
-import posthoganalytics
-from asgiref.sync import sync_to_async
-from posthoganalytics.ai.anthropic import Anthropic
-from rest_framework import status, viewsets
-from rest_framework.decorators import action
-from rest_framework.permissions import IsAuthenticated
-from rest_framework.request import Request
-from rest_framework.response import Response
-
-from posthog.auth import SessionAuthentication
-from posthog.models import User
-
-from ee.support_sidebar_max.prompt import get_system_prompt
-
-from .max_search_tool import max_search_tool
-from .sidebar_max_ai import ConversationHistory, RateLimitType, max_search_tool_tool
-
-# Configure logging
-django_logger = logging.getLogger("django")
-django_logger.setLevel(logging.INFO)
-
-# Don't add the auth header here, the Anthropic Python SDK handles it
-REQUIRED_HEADERS = {
- "anthropic-version": "2023-06-01",
- "content-type": "application/json",
-}
-
-
-class MaxChatViewSet(viewsets.ViewSet):
- """
- ViewSet for Max Support Sidebar Chat Assistant.
- Handles chat interactions with proper message structure and tool use.
- """
-
- authentication_classes = [SessionAuthentication]
- permission_classes = [IsAuthenticated]
-
- CONVERSATION_TIMEOUT = 3600 # one hour
- MAX_BACKOFF = 40 # Maximum backoff in seconds to prevent gateway timeouts
- # Cap how long any single Anthropic call may block a worker. The SDK defaults to 600s,
- # which lets stalled requests pile up (each holding conversation history + a connection)
- # and exhaust worker memory when Anthropic is slow or erroring.
- ANTHROPIC_TIMEOUT_SECONDS = 30.0
- basename = "max"
-
- def _convert_headers(self, headers: MutableMapping[str, str]) -> dict[str, str]:
- """Convert MutableMapping headers to dict safely."""
- return dict(headers)
-
- def list(self, request: Request, **kwargs: Any) -> Response:
- """List endpoint - not used but required by DRF"""
- return Response({"detail": "List operation not supported"}, status=status.HTTP_405_METHOD_NOT_ALLOWED)
-
- def retrieve(self, request: Request, pk=None, **kwargs: Any) -> Response:
- """Retrieve endpoint - not used but required by DRF"""
- return Response({"detail": "Retrieve operation not supported"}, status=status.HTTP_405_METHOD_NOT_ALLOWED)
-
- async def async_send_message(
- self, client: Anthropic, tools, system_prompt, messages, distinct_id: str, team_id: int | None
- ):
- """Async wrapper for send_message"""
-
- @sync_to_async(thread_sensitive=False)
- def _send_message():
- return self.send_message(client, tools, system_prompt, messages, distinct_id, team_id)
-
- return await _send_message()
-
- async def async_create(self, request: Request, **kwargs: Any) -> Response:
- """Async version of create method"""
- try:
- # Check if we're currently rate limited
- is_rate_limited, retry_after, limit_type = ConversationHistory.check_rate_limits()
- if is_rate_limited:
- return self._handle_rate_limit(retry_after, limit_type)
-
- # Initialize Anthropic client (non-blocking)
- client = Anthropic(
- api_key=settings.ANTHROPIC_API_KEY,
- posthog_client=posthoganalytics.default_client,
- timeout=self.ANTHROPIC_TIMEOUT_SECONDS,
- )
- user = cast(User, request.user)
- distinct_id = user.distinct_id or str(user.id)
- team_id = user.current_team_id
-
- data = request.data
- if not data or "message" not in data:
- return Response({"error": "No message provided"}, status=status.HTTP_400_BAD_REQUEST)
-
- user_input = data["message"]
- session_id = data.get("session_id") or request.session.session_key
- if not session_id:
- request.session.create()
- session_id = request.session.session_key
- assert isinstance(session_id, str) # Type narrowing for mypy
-
- history = ConversationHistory.get_from_cache(session_id)
- system_prompt = self._format_system_prompt(get_system_prompt())
-
- if not user_input.strip():
- history.add_turn_user("Hello!")
- result = await self.async_send_message(
- client, [max_search_tool_tool], system_prompt, history.get_turns(), distinct_id, team_id
- )
- if isinstance(result, Response): # Error response
- return result
- if "content" in result:
- history.add_turn_assistant(result["content"])
- history.save_to_cache(session_id, timeout=self.CONVERSATION_TIMEOUT)
- return Response({"content": result["content"]})
-
- history.add_turn_user(user_input)
- messages = history.get_turns()
- full_response = ""
-
- result = await self.async_send_message(
- client, [max_search_tool_tool], system_prompt, messages, distinct_id, team_id
- )
- if isinstance(result, Response): # Error response
- return result
-
- while result and "content" in result:
- if result.get("stop_reason") == "tool_use":
- response_part, tool_result = self._handle_tool_use(result, history)
- full_response += response_part
- messages.append(tool_result)
-
- result = await self.async_send_message(
- client, [max_search_tool_tool], system_prompt, history.get_turns(), distinct_id, team_id
- )
- if isinstance(result, Response): # Error response
- return result
- else:
- if isinstance(result["content"], list):
- for block in result["content"]:
- if block["type"] == "text":
- full_response += block["text"] + "\n"
- history.add_turn_assistant(result["content"])
- else:
- full_response += result["content"]
- history.add_turn_assistant(result["content"])
- break
-
- history.save_to_cache(session_id, timeout=self.CONVERSATION_TIMEOUT)
- return Response({"content": full_response.strip(), "session_id": session_id})
-
- except Exception as e:
- django_logger.error(f"✨🦔 Error in chat endpoint: {str(e)}", exc_info=True)
- return Response(
- {"error": "An unexpected error occurred. Please try again later."},
- status=status.HTTP_500_INTERNAL_SERVER_ERROR,
- )
-
- def create(self, request: Request, **kwargs: Any) -> Response:
- """Synchronous wrapper for async_create"""
- return asyncio.run(self.async_create(request, **kwargs))
-
- @action(methods=["POST"], detail=False, url_path="chat", url_name="chat")
- def chat(self, request: Request, **kwargs: Any) -> Response:
- return self.create(request, **kwargs)
-
- def _get_headers(self) -> dict[str, str]:
- """Get headers with container hostname from settings"""
- headers = REQUIRED_HEADERS.copy()
- headers["container_hostname"] = settings.CONTAINER_HOSTNAME
- return headers
-
- def _format_system_prompt(self, prompt: str) -> builtins.list[dict[str, Any]]:
- """Format system prompt with cache control."""
- return [{"type": "text", "text": prompt, "cache_control": {"type": "ephemeral"}}]
-
- def _format_user_message(self, content: str) -> dict:
- """Format user message with proper structure."""
- return {"role": "user", "content": [{"type": "text", "text": content}]}
-
- def _format_tool_result(self, tool_use_id: str, content: str) -> dict:
- """Format tool result with proper structure."""
- return {
- "role": "user",
- "content": [
- {
- "type": "tool_result",
- "tool_use_id": tool_use_id,
- "content": content,
- }
- ],
- }
-
- def _handle_rate_limit(self, retry_after: Optional[int], limit_type: Optional[RateLimitType] = None) -> Response:
- """Handle rate limit with DRF response."""
- capped_retry = min(retry_after if retry_after is not None else self.MAX_BACKOFF, self.MAX_BACKOFF)
- limit_message = f" ({limit_type.replace('_', ' ')})" if limit_type else ""
- return Response(
- {
- "error": "rate_limit_exceeded",
- "message": f"🫣 Uh-oh, I'm really popular today, we've been rate-limited{limit_message}. I just need to catch my breath. Hang on, I'll repeat your question for you and resume searching in less than a minute...",
- "retry_after": capped_retry,
- "limit_type": limit_type,
- },
- status=status.HTTP_429_TOO_MANY_REQUESTS,
- headers={"Retry-After": str(capped_retry)},
- )
-
- def _handle_tool_use(self, result: dict[str, Any], history: ConversationHistory) -> tuple[str, dict[str, Any]]:
- """Handle tool use response from the API"""
- full_response = ""
- # Process text blocks that came before tool use
- for block in result["content"]:
- if block["type"] == "text":
- full_response += block["text"] + "\n"
-
- tool_use_block = result["content"][-1] # Get the last tool use block
- django_logger.info(f"Tool use requested: {tool_use_block}")
-
- query = tool_use_block["input"]["query"]
- search_results = max_search_tool(query)
- django_logger.debug(f"Search results for query '{query}': {search_results}")
-
- formatted_results = "\n".join(
- [
- f"Text: {passage['text']}\nHeading: {passage['heading']}\n"
- f"Source: {result_item['page_title']}\nURL: {passage['url']}\n"
- for result_item in search_results
- for passage in result_item["relevant_passages"]
- ]
- )
-
- # Append assistant's response with content blocks
- history.add_turn_assistant(result["content"])
-
- # Return the formatted results and current response
- return full_response, self._format_tool_result(tool_use_block["id"], formatted_results)
-
- def send_message(self, client: Anthropic, tools, system_prompt, messages, distinct_id: str, team_id: int | None):
- """Send message to Anthropic API with proper error handling"""
- try:
- django_logger.info("✨🦔 Preparing to send message to Anthropic API")
- try:
- headers: dict[str, str] = {}
- django_logger.debug("✨🦔 API headers prepared successfully")
- except Exception as e:
- django_logger.error(f"✨🦔 Error preparing API headers: {str(e)}", exc_info=True)
- raise
-
- # Check token count before sending
- count = client.messages.count_tokens(
- messages=messages,
- model="claude-3-5-sonnet-20241022",
- system=system_prompt,
- tools=tools, # Include same tools as in create()
- )
-
- # Pre-check if we have enough tokens
- if not ConversationHistory.consume_tokens("input_tokens", count.input_tokens):
- return self._handle_rate_limit(45, "input_tokens")
-
- if count.input_tokens > 190000: # Safe buffer below 200k limit
- messages = [messages[0], messages[-1]] # Keep first and last messages
-
- # Use with_raw_response to get access to headers
- raw_response = client.messages.with_raw_response.create( # type: ignore[call-overload]
- model="claude-3-5-sonnet-20241022",
- max_tokens=1024,
- tools=tools,
- system=system_prompt,
- messages=messages,
- extra_headers=headers,
- posthog_privacy_mode=True,
- posthog_distinct_id=distinct_id,
- posthog_properties={
- "ai_product": "support_sidebar_max",
- "ai_feature": "chat",
- "team_id": team_id,
- },
- )
-
- # Get the actual message response
- message = raw_response.parse()
- django_logger.debug(f"✨🦔 Response from Anthropic API: {message}")
-
- # Update rate limits from headers
- try:
- response_headers = self._convert_headers(raw_response.headers)
- ConversationHistory.update_rate_limits(response_headers)
- django_logger.info(
- f"✨🦔 API Capacity - "
- f"Requests: {response_headers.get('anthropic-ratelimit-requests-remaining', '?')}/{response_headers.get('anthropic-ratelimit-requests-limit', '?')}, "
- f"Input Tokens: {response_headers.get('anthropic-ratelimit-input-tokens-remaining', '?')}/{response_headers.get('anthropic-ratelimit-input-tokens-limit', '?')}, "
- f"Output Tokens: {response_headers.get('anthropic-ratelimit-output-tokens-remaining', '?')}/{response_headers.get('anthropic-ratelimit-output-tokens-limit', '?')}"
- )
- except Exception as e:
- django_logger.warning(f"✨🦔 Unable to log capacity info: {str(e)}")
-
- # Log token usage and cache metrics
- if message.usage:
- input_tokens = getattr(message.usage, "input_tokens", 0)
- output_tokens = getattr(message.usage, "output_tokens", 0)
- cache_created = getattr(message.usage, "cache_creation_input_tokens", 0)
- cache_read = getattr(message.usage, "cache_read_input_tokens", 0)
- fresh_input = getattr(message.usage, "input_tokens", 0)
-
- # Consume output tokens
- if not ConversationHistory.consume_tokens("output_tokens", output_tokens):
- return self._handle_rate_limit(45, "output_tokens")
-
- django_logger.info(f"✨🦔 Request Usage - Input: {input_tokens}, Output: {output_tokens} tokens")
- if cache_created or cache_read:
- django_logger.info(
- f"✨🦔 Cache Stats - Created: {cache_created}, Read: {cache_read}, Fresh: {fresh_input}"
- )
-
- # Extract the necessary fields from the Message object
- result = {
- "content": [block.dict() for block in message.content]
- if isinstance(message.content, list)
- else message.content,
- "stop_reason": message.stop_reason,
- "usage": message.usage.dict() if message.usage else None,
- }
-
- django_logger.debug(f"✨🦔 Processed API response: {result}")
- return result
-
- except anthropic.RateLimitError as e:
- try:
- # Get reset time from headers if available
- raw_headers = e.response.headers if hasattr(e, "response") and hasattr(e.response, "headers") else {}
- headers = self._convert_headers(raw_headers) if raw_headers else {}
-
- # Try to get retry-after header first
- if "retry-after" in headers:
- retry_seconds = min(int(headers["retry-after"]), self.MAX_BACKOFF)
- else:
- # Calculate from reset timestamp
- now = datetime.now(UTC)
- reset_times = []
-
- for header in headers:
- if header.endswith("-reset"):
- try:
- reset_time = datetime.fromisoformat(headers[header].rstrip("Zs")).replace(tzinfo=UTC)
- wait_seconds = max(0, int((reset_time - now).total_seconds()))
- reset_times.append(min(wait_seconds, self.MAX_BACKOFF))
- except (ValueError, TypeError):
- continue
-
- retry_seconds = min(max(reset_times) if reset_times else self.MAX_BACKOFF, self.MAX_BACKOFF)
-
- # Determine which limit was hit from headers
- limit_type: Optional[RateLimitType] = None
- for key in headers:
- if key.endswith("-remaining"):
- if headers[key] == "0":
- extracted_type = key.replace("anthropic-ratelimit-", "").replace("-remaining", "")
- if extracted_type in ("requests", "input_tokens", "output_tokens"):
- limit_type = extracted_type # type: ignore # mypy doesn't understand the literal check
- break
-
- django_logger.warning(f"✨🦔 Rate limit hit - waiting {retry_seconds} seconds before retry")
- return self._handle_rate_limit(retry_seconds, limit_type)
-
- except Exception as header_error:
- django_logger.warning(f"✨🦔 Rate limit handling error: {str(header_error)}")
- return self._handle_rate_limit(self.MAX_BACKOFF) # Default to MAX_BACKOFF
- except anthropic.APITimeoutError:
- # Raised once a call exceeds ANTHROPIC_TIMEOUT_SECONDS. Surface a clean 504 rather than
- # letting it fall through to the generic handler, which would leak the raw SDK error and
- # mislabel an upstream timeout as a 500.
- django_logger.warning(f"✨🦔 Anthropic API call timed out after {self.ANTHROPIC_TIMEOUT_SECONDS}s")
- return Response(
- {"error": "The AI service took too long to respond. Please try again."},
- status=status.HTTP_504_GATEWAY_TIMEOUT,
- )
- except Exception as e:
- django_logger.error(f"✨🦔 Request to Anthropic API failed: {str(e)}", exc_info=True)
- return Response({"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
diff --git a/ee/urls.py b/ee/urls.py
index 7f8fef04f746..7373e71c10b7 100644
--- a/ee/urls.py
+++ b/ee/urls.py
@@ -4,7 +4,6 @@ from django.conf import settings
from django.contrib import admin
from django.contrib.admin.exceptions import NotRegistered
from django.urls import include, path, re_path
-from django.views.decorators.csrf import csrf_exempt
from django.views.generic import RedirectView
from django_otp.plugins.otp_static.models import StaticDevice
@@ -24,7 +23,6 @@ from products.cdp.backend.api import hooks
from ee.admin.loginas_views import loginas_user, upgrade_impersonation
from ee.api import integration
from ee.api.vercel import vercel_connect, vercel_sso
-from ee.support_sidebar_max.views import MaxChatViewSet
from .api import billing, conversation, core_memory, organization_billing, project_billing
@@ -175,7 +173,6 @@ else:
urlpatterns: list[Any] = [
path("api/saml/metadata/", saml_metadata_view),
- path("max/chat/", csrf_exempt(MaxChatViewSet.as_view({"post": "create"})), name="max_chat"),
re_path(r"^login/vercel/?$", vercel_sso.VercelSSOViewSet.as_view({"get": "sso_redirect"})),
re_path(r"^login/vercel/continue/?$", vercel_sso.VercelSSOViewSet.as_view({"get": "sso_continue"})),
re_path(
diff --git a/frontend/src/layout/navigation-3000/sidepanel/panels/max/sidePanelMaxAILogic.ts b/frontend/src/layout/navigation-3000/sidepanel/panels/max/sidePanelMaxAILogic.ts
deleted file mode 100644
index 3faca2d3065f..000000000000
--- a/frontend/src/layout/navigation-3000/sidepanel/panels/max/sidePanelMaxAILogic.ts
+++ /dev/null
@@ -1,278 +0,0 @@
-import { MakeLogicType, actions, kea, listeners, path, reducers } from 'kea'
-import { loaders } from 'kea-loaders'
-
-import { sidePanelMaxAPI } from './sidePanelMaxAPI'
-
-interface RateLimit {
- limit: number
- remaining: number
- reset: string
-}
-
-interface RateLimits {
- requests: RateLimit
- input_tokens: RateLimit
- output_tokens: RateLimit
-}
-
-export interface ChatMessage {
- role: 'user' | 'assistant'
- content: string
- timestamp: string
- isRateLimited?: boolean
- isError?: boolean
- limitType?: string
-}
-
-interface MaxResponse {
- content: string | { text: string; type: string }
- rate_limits?: RateLimits
- isError?: boolean
- limit_type?: string
-}
-
-interface RetryAction {
- message: string
- retryAfter: number
- limitType?: string
-}
-
-interface RateLimitAction {
- isLimited: boolean
- limitType?: string
-}
-
-// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
-export interface sidePanelMaxAILogicValues {
- assistantResponse: string | null
- assistantResponseLoading: boolean
- currentMessages: ChatMessage[]
- hasServerError: boolean
- isRateLimited: boolean
- isSearchingThinking: boolean
- rateLimitType: string | null
- retryAfter: number | null
- retryAttempts: number
-}
-
-// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
-export interface sidePanelMaxAILogicActions {
- appendAssistantMessage: (content: string) => {
- content: string
- }
- clearChatHistory: () => {
- value: true
- }
- retryAfter: (
- message: string,
- retryAfter: number,
- limitType?: string
- ) => {
- limitType: string | undefined
- message: string
- retryAfter: number
- }
- setRateLimited: (
- isLimited: boolean,
- limitType?: string
- ) => {
- isLimited: boolean
- limitType: string | undefined
- }
- setSearchingThinking: (isSearching: boolean) => {
- isSearching: boolean
- }
- setServerError: (isError: boolean) => {
- isError: boolean
- }
- submitMessage: (message: string) => {
- message: string
- }
- submitMessageFailure: (
- error: string,
- errorObject?: any
- ) => {
- error: string
- errorObject?: any
- }
- submitMessageSuccess: (
- assistantResponse: string | null,
- payload?: {
- message: string
- }
- ) => {
- assistantResponse: string | null
- payload?: {
- message: string
- }
- }
-}
-
-export type sidePanelMaxAILogicType = MakeLogicType<sidePanelMaxAILogicValues, sidePanelMaxAILogicActions>
-
-export const sidePanelMaxAILogic = kea<sidePanelMaxAILogicType>([
- path(['scenes', 'navigation', 'sidepanel', 'sidePanelMaxAILogic']),
-
- actions({
- submitMessage: (message: string) => ({ message }),
- clearChatHistory: true,
- appendAssistantMessage: (content: string) => ({ content }),
- setSearchingThinking: (isSearching: boolean) => ({ isSearching }),
- setRateLimited: (isLimited: boolean, limitType?: string) => ({ isLimited, limitType }),
- setServerError: (isError: boolean) => ({ isError }),
- retryAfter: (message: string, retryAfter: number, limitType?: string) => ({ message, retryAfter, limitType }),
- }),
-
- reducers({
- retryAttempts: [
- 0,
- {
- submitMessage: () => 0,
- retryAfter: (state) => state + 1,
- setRateLimited: (state, { isLimited }) => (isLimited ? state : 0),
- },
- ],
- retryAfter: [
- null as number | null,
- {
- retryAfter: (_, { retryAfter }) => retryAfter,
- },
- ],
- rateLimitType: [
- null as string | null,
- {
- setRateLimited: (_, { limitType }: RateLimitAction) => limitType || null,
- retryAfter: (_, { limitType }: RetryAction) => limitType || null,
- },
- ],
- currentMessages: [
- [] as ChatMessage[],
- {
- submitMessage: (state, { message }) =>
- message.trim()
- ? [
- ...state,
- {
- role: 'user',
- content: message,
- timestamp: new Date().toISOString(),
- },
- ]
- : state,
- appendAssistantMessage: (state, { content }) => [
- ...state,
- {
- role: 'assistant',
- content,
- timestamp: new Date().toISOString(),
- isRateLimited: content.includes('Rate limit exceeded') || content.includes('rate-limited'),
- isError:
- content.includes('connect to the Anthropic API') ||
- content.includes('status.anthropic.com'),
- },
- ],
- clearChatHistory: () => [],
- },
- ],
- isSearchingThinking: [
- false,
- {
- setSearchingThinking: (_, { isSearching }) => isSearching,
- },
- ],
- isRateLimited: [
- false,
- {
- setRateLimited: (_, { isLimited }: RateLimitAction) => isLimited,
- },
- ],
- hasServerError: [
- false,
- {
- setServerError: (_, { isError }) => isError,
- },
- ],
- }),
-
- loaders(({ actions, values }) => ({
- assistantResponse: [
- null as string | null,
- {
- submitMessage: async ({ message }, breakpoint) => {
- try {
- // Don't retry more than twice
- if (values.retryAttempts >= 3) {
- actions.appendAssistantMessage(
- "😮‍💨 I'm still experiencing rate limits. Please leave me alone for a few minutes. Scroll down and hit `End chat`, then try me again after I've had a nap. 🦔"
- )
- actions.setSearchingThinking(false)
- return null
- }
-
- actions.setSearchingThinking(true)
- actions.setServerError(false)
- if (!values.isRateLimited) {
- actions.setRateLimited(false)
- }
- const response = (await sidePanelMaxAPI.sendMessage(message)) as MaxResponse
- await breakpoint(100)
-
- const messageContent =
- typeof response.content === 'string' ? response.content : response.content.text
-
- actions.appendAssistantMessage(messageContent)
-
- await breakpoint(300)
- actions.setSearchingThinking(false)
-
- return messageContent
- } catch (error: unknown) {
- if (
- typeof error === 'object' &&
- error &&
- 'status' in error &&
- typeof error.status === 'number'
- ) {
- if (error.status === 429) {
- const MAX_BACKOFF = 40 // Maximum backoff in seconds to prevent gateway timeouts
- const retryPeriod = Math.min(
- (error as any).data?.retry_after || MAX_BACKOFF,
- MAX_BACKOFF
- )
- const limitType = (error as any).data?.limit_type
- actions.setRateLimited(true, limitType)
- if (values.retryAttempts < 2) {
- actions.retryAfter(message, retryPeriod, limitType)
- } else {
- actions.appendAssistantMessage(
- "I'm still experiencing rate limits. Please wait a minute or two before trying again."
- )
- actions.setSearchingThinking(false)
- }
- await breakpoint(100)
- } else if ([500, 504, 524, 529].includes(error.status)) {
- actions.setServerError(true)
- await breakpoint(100)
- actions.setSearchingThinking(false)
- }
- } else {
- await breakpoint(100)
- actions.setSearchingThinking(false)
- }
-
- console.error('Error sending message:', error)
- return null
- }
- },
- },
- ],
- })),
-
- listeners(({ actions }) => ({
- retryAfter: async ({ retryAfter, message }, breakpoint) => {
- await breakpoint(retryAfter * 1000)
- actions.setRateLimited(false, undefined)
- actions.submitMessage(message)
- },
- })),
-])
diff --git a/frontend/src/layout/navigation-3000/sidepanel/panels/max/sidePanelMaxAPI.ts b/frontend/src/layout/navigation-3000/sidepanel/panels/max/sidePanelMaxAPI.ts
deleted file mode 100644
index 71b2cd2f2e3e..000000000000
--- a/frontend/src/layout/navigation-3000/sidepanel/panels/max/sidePanelMaxAPI.ts
+++ /dev/null
@@ -1,46 +0,0 @@
-import api from 'lib/api'
-
-interface RateLimit {
- limit: number
- remaining: number
- reset: string
-}
-
-interface RateLimits {
- requests: RateLimit
- input_tokens: RateLimit
- output_tokens: RateLimit
-}
-
-interface MaxResponse {
- content: string
- rate_limits: RateLimits
-}
-
-export const sidePanelMaxAPI = {
- async sendMessage(message: string): Promise<MaxResponse> {
- // Get or create session ID using sessionStorage
- let sessionId = sessionStorage.getItem('max_session_id')
- if (!sessionId) {
- sessionId = crypto.randomUUID()
- sessionStorage.setItem('max_session_id', sessionId)
- }
-
- // nosemgrep: prefer-codegen-api -- Legacy raw API call to a route outside /api/, with an unchecked response type. No generated function can cover it until the route is in the OpenAPI schema.
- const response = await api.createResponse(`/max/chat/`, {
- message,
- role: 'user',
- session_id: sessionId,
- })
-
- if (!response.ok) {
- throw new Error('Failed to send message to Max')
- }
-
- const data = await response.json()
- return {
- content: data.content,
- rate_limits: data.rate_limits,
- }
- },
-}