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.

fix(slack): send the classifier token cap as max_completion_tokens (#102744)

Brandon52356d5fddd6
diff --git a/posthog/temporal/ai/slack_app/activities/classifiers.py b/posthog/temporal/ai/slack_app/activities/classifiers.py
index eeffd58f60e1..68f4d4d27a7f 100644
--- a/posthog/temporal/ai/slack_app/activities/classifiers.py
+++ b/posthog/temporal/ai/slack_app/activities/classifiers.py
@@ -38,7 +38,8 @@ CLASSIFIER_MODEL = "claude-haiku-4-5-20251001"
# The model-override and agent-directed classifiers both run on a reasoning model, which
# draws its reasoning from the same token budget as the reply. The reply is one short JSON
# object; the headroom is for the thinking in front of it, and a truncated turn falls back
-# to the safe answer.
+# to the safe answer. Reasoning-class is also why the cap rides on `max_completion_tokens`:
+# the chat-completions route rejects `max_tokens` outright.
#
# The gateway client defaults to a 600s read and two retries, which is the right shape for
# a generation call and the wrong one here. Left unbounded these never get to fall back,
@@ -46,13 +47,13 @@ CLASSIFIER_MODEL = "claude-haiku-4-5-20251001"
# as the timeout: the activity is sync, so a thread Temporal has stopped waiting on keeps
# blocking until the client itself returns.
MODEL_OVERRIDE_CLASSIFIER_MODEL = "gpt-5.6-luna"
-MODEL_OVERRIDE_MAX_TOKENS = 2048
+MODEL_OVERRIDE_MAX_COMPLETION_TOKENS = 2048
# One call per `@PostHog`, on the mention text alone. Measured mean is ~1.7s per call.
MODEL_OVERRIDE_TIMEOUT_SECONDS = 10.0
MODEL_OVERRIDE_MAX_RETRIES = 1
AGENT_DIRECTED_CLASSIFIER_MODEL = "gpt-5.6-luna"
-AGENT_DIRECTED_MAX_TOKENS = 2048
+AGENT_DIRECTED_MAX_COMPLETION_TOKENS = 2048
# One call per reply in every thread the agent is working in, and its prompt carries the
# thread the override classifier's does not. The eval suite sees 3-9s on that shape, close
# enough to a 10s ceiling that the tail would drop instructions rather than misread them.
@@ -296,7 +297,7 @@ def classify_message_is_agent_directed(
response = client.chat.completions.create(
model=AGENT_DIRECTED_CLASSIFIER_MODEL,
messages=[{"role": "user", "content": prompt}],
- max_tokens=AGENT_DIRECTED_MAX_TOKENS,
+ max_completion_tokens=AGENT_DIRECTED_MAX_COMPLETION_TOKENS,
response_format=_agent_directed_response_format(),
)
# Tolerant parse on top of the schema on purpose: the gateway fronts several
@@ -450,7 +451,7 @@ def classify_slack_app_model_override(
response = client.chat.completions.create(
model=MODEL_OVERRIDE_CLASSIFIER_MODEL,
messages=[{"role": "user", "content": prompt}],
- max_tokens=MODEL_OVERRIDE_MAX_TOKENS,
+ max_completion_tokens=MODEL_OVERRIDE_MAX_COMPLETION_TOKENS,
response_format=_model_override_response_format(choices),
)
# Tolerant parse on top of the schema on purpose: the gateway fronts several
diff --git a/posthog/temporal/tests/ai/slack_app/activities/test_classify_message_is_agent_directed.py b/posthog/temporal/tests/ai/slack_app/activities/test_classify_message_is_agent_directed.py
index 7b818f811a6a..208aa5b602ad 100644
--- a/posthog/temporal/tests/ai/slack_app/activities/test_classify_message_is_agent_directed.py
+++ b/posthog/temporal/tests/ai/slack_app/activities/test_classify_message_is_agent_directed.py
@@ -61,6 +61,19 @@ class TestClassifyMessageIsAgentDirected:
assert options["timeout"] < POSTHOG_CODE_SLACK_MENTION_TIMEOUT_SECONDS
assert options["max_retries"] * options["timeout"] < POSTHOG_CODE_SLACK_MENTION_TIMEOUT_SECONDS
+ def test_token_cap_uses_the_reasoning_model_parameter(self):
+ # A rejected `max_tokens` would read as "not agent directed" on every untagged reply.
+ client = self._fake_client('{"agent_directed": false}')
+ with patch(
+ "posthog.temporal.ai.slack_app.activities.classifiers.build_openai_client",
+ return_value=client,
+ ):
+ classify_message_is_agent_directed("lunch in 5?", TASK_TITLE, THREAD)
+
+ kwargs = client.chat.completions.create.call_args.kwargs
+ assert "max_tokens" not in kwargs
+ assert kwargs["max_completion_tokens"] > 0
+
def test_reply_is_pinned_to_a_single_boolean(self):
# The schema, not the prompt, is what stops a reasoning model answering with its
# reasoning — prose parses to nothing, which reads as a refused call.
diff --git a/posthog/temporal/tests/ai/slack_app/activities/test_classify_slack_app_model_override.py b/posthog/temporal/tests/ai/slack_app/activities/test_classify_slack_app_model_override.py
index b63799b37b3d..e9ad0e6eb7cc 100644
--- a/posthog/temporal/tests/ai/slack_app/activities/test_classify_slack_app_model_override.py
+++ b/posthog/temporal/tests/ai/slack_app/activities/test_classify_slack_app_model_override.py
@@ -123,6 +123,19 @@ class TestClassifySlackAppModelOverride:
assert options["timeout"] < POSTHOG_CODE_SLACK_MENTION_TIMEOUT_SECONDS
assert options["max_retries"] * options["timeout"] < POSTHOG_CODE_SLACK_MENTION_TIMEOUT_SECONDS
+ def test_token_cap_uses_the_reasoning_model_parameter(self):
+ # The route 400s on `max_tokens`, and the except turns that into a silent "no override".
+ fake_client = self._fake_client('{"model": null, "reasoning_effort": null}')
+ with patch(
+ "posthog.temporal.ai.slack_app.activities.classifiers.build_openai_client",
+ return_value=fake_client,
+ ):
+ classify_slack_app_model_override("use fable for this", CHOICES)
+
+ kwargs = fake_client.chat.completions.create.call_args.kwargs
+ assert "max_tokens" not in kwargs
+ assert kwargs["max_completion_tokens"] > 0
+
def test_prompt_snapshot_matches(self, snapshot):
"""The prompt is the whole classifier — the catalogue it offers, the
instruction-vs-subject-matter examples, and the reply contract. Pinning it means