askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
test_ai_effort_and_budget.py · 7.4 KB · 168 lines
| 1 | """Output budget and effort follow the tool subset, and only reach models |
| 2 | that accept them. |
| 3 | |
| 4 | 1024 output tokens was too small once models think before answering and |
| 5 | once builder mode writes whole objects: a create_object call cut off |
| 6 | mid-source arrives as a truncated tool call. Effort is a Claude 4.6+ |
| 7 | request field; sending it to claude-haiku-4-5 or to OpenAI would be a 400 |
| 8 | that fails the turn, so it is dropped for them, never sent. |
| 9 | """ |
| 10 | |
| 11 | import json |
| 12 | |
| 13 | import pytest |
| 14 | |
| 15 | import object_ai |
| 16 | import object_server |
| 17 | from ai_fakes import anthropic_message, chat_server, install_fake_transport, text_block |
| 18 | from test_object_server import request |
| 19 | |
| 20 | CONVERSATION = ["global_search", "list_records", "get_record", "create_record"] |
| 21 | BUILDER = ["global_search", "list_objects", "get_object_source", "create_object", |
| 22 | "update_object_source", "execute_object"] |
| 23 | |
| 24 | |
| 25 | def test_builder_mode_is_any_subset_that_can_write_object_source(): |
| 26 | assert object_ai.chat_mode(CONVERSATION) == "conversation" |
| 27 | assert object_ai.chat_mode(BUILDER) == "builder" |
| 28 | assert object_ai.chat_mode(["update_object_source"]) == "builder" |
| 29 | assert object_ai.chat_mode([]) == "conversation" |
| 30 | assert object_ai.chat_mode(None) == "conversation" |
| 31 | |
| 32 | |
| 33 | @pytest.mark.parametrize("model,expected", [ |
| 34 | ("claude-haiku-5-5", True), |
| 35 | ("claude-sonnet-5-5", True), |
| 36 | ("claude-opus-5-5", True), |
| 37 | ("claude-sonnet-5", True), |
| 38 | ("claude-opus-4-8", True), |
| 39 | ("claude-sonnet-4-6", True), |
| 40 | ("claude-haiku-4-5", False), |
| 41 | ("claude-sonnet-4-5", False), |
| 42 | ("claude-haiku-4-5-20251001", False), # dated snapshot ids are not read |
| 43 | ("claude-mystery", False), |
| 44 | ]) |
| 45 | def test_effort_only_for_claude_4_6_and_later(model, expected): |
| 46 | assert object_ai.model_supports_effort("anthropic", model) is expected |
| 47 | |
| 48 | |
| 49 | def test_effort_never_for_openai(): |
| 50 | assert object_ai.model_supports_effort("openai", "gpt-5") is False |
| 51 | assert object_ai.model_supports_effort("openai", "claude-haiku-5-5") is False |
| 52 | |
| 53 | |
| 54 | def test_max_tokens_defaults_by_mode_and_stream(): |
| 55 | assert object_ai.resolve_max_tokens(mode="conversation") == 4096 |
| 56 | assert object_ai.resolve_max_tokens(mode="builder") == 8192 |
| 57 | assert object_ai.resolve_max_tokens(mode="builder", stream=True) == 16384 |
| 58 | assert object_ai.resolve_max_tokens(mode="conversation", stream=True) == 4096 |
| 59 | |
| 60 | |
| 61 | def test_max_tokens_requests_are_capped(): |
| 62 | assert object_ai.resolve_max_tokens(100_000) == object_ai.MAX_TOKENS_CAP == 8192 |
| 63 | assert object_ai.resolve_max_tokens(100_000, stream=True) == 16384 |
| 64 | assert object_ai.resolve_max_tokens(500) == 500 |
| 65 | # An operator cap bounds both paths, defaults included. |
| 66 | assert object_ai.resolve_max_tokens(100_000, stream=True, cap=2000) == 2000 |
| 67 | assert object_ai.resolve_max_tokens(mode="builder", cap=2000) == 2000 |
| 68 | for bad in (0, -1, "8192", True, 1.5): |
| 69 | with pytest.raises(object_ai.InvalidChatRequestError): |
| 70 | object_ai.resolve_max_tokens(bad) |
| 71 | |
| 72 | |
| 73 | def test_effort_defaults_by_mode_and_validates(): |
| 74 | assert object_ai.resolve_effort(mode="conversation") == "low" |
| 75 | assert object_ai.resolve_effort(mode="builder") == "high" |
| 76 | assert object_ai.resolve_effort("max", mode="conversation") == "max" |
| 77 | with pytest.raises(object_ai.InvalidChatRequestError): |
| 78 | object_ai.resolve_effort("extreme") |
| 79 | |
| 80 | |
| 81 | def _body(model, service="anthropic", **kwargs): |
| 82 | bodies = [] |
| 83 | |
| 84 | def send_http(url, headers, body): |
| 85 | bodies.append(json.loads(body)) |
| 86 | if service == "anthropic": |
| 87 | return anthropic_message([text_block("ok")]) |
| 88 | return 200, json.dumps({"choices": [{"message": {"content": "ok"}}]}).encode() |
| 89 | |
| 90 | object_ai.run_chat(send_http=send_http, dispatch_tool=lambda n, a: {}, service=service, |
| 91 | model=model, key="sk-test", message="hi", **kwargs) |
| 92 | return bodies[0] |
| 93 | |
| 94 | |
| 95 | def test_run_chat_sends_effort_inside_output_config_for_supported_models(): |
| 96 | body = _body("claude-haiku-5-5", effort="low") |
| 97 | assert body["output_config"] == {"effort": "low"} |
| 98 | assert "effort" not in body |
| 99 | for refused in ("temperature", "top_p", "top_k", "thinking"): |
| 100 | assert refused not in body |
| 101 | |
| 102 | |
| 103 | def test_run_chat_drops_effort_for_models_that_refuse_it(): |
| 104 | assert "output_config" not in _body("claude-haiku-4-5", effort="high") |
| 105 | openai = _body("gpt-5-mini", service="openai", effort="high") |
| 106 | assert "output_config" not in openai and "reasoning_effort" not in openai |
| 107 | |
| 108 | |
| 109 | def test_endpoint_uses_low_effort_and_4096_for_conversation(tmp_path, monkeypatch): |
| 110 | _, headers = chat_server(tmp_path, monkeypatch) |
| 111 | seen = [] |
| 112 | install_fake_transport(monkeypatch, [anthropic_message([text_block("ok")])], seen) |
| 113 | status, _, chat = request("/api/ai/chat", method="POST", headers=headers, |
| 114 | body=json.dumps({"message": "hi", "tools": CONVERSATION}).encode()) |
| 115 | assert status == 200, chat |
| 116 | sent = seen[0]["body"] |
| 117 | assert sent["model"] == "claude-haiku-5-5" # the default |
| 118 | assert sent["max_tokens"] == 4096 |
| 119 | assert sent["output_config"] == {"effort": "low"} |
| 120 | |
| 121 | |
| 122 | def test_endpoint_uses_high_effort_and_8192_for_builder(tmp_path, monkeypatch): |
| 123 | _, headers = chat_server(tmp_path, monkeypatch) |
| 124 | seen = [] |
| 125 | install_fake_transport(monkeypatch, [anthropic_message([text_block("ok")])], seen) |
| 126 | status, _, chat = request("/api/ai/chat", method="POST", headers=headers, |
| 127 | body=json.dumps({"message": "hi", "tools": BUILDER}).encode()) |
| 128 | assert status == 200, chat |
| 129 | assert seen[0]["body"]["max_tokens"] == 8192 |
| 130 | assert seen[0]["body"]["output_config"] == {"effort": "high"} |
| 131 | |
| 132 | |
| 133 | def test_endpoint_effort_and_cap_are_configurable(tmp_path, monkeypatch): |
| 134 | _, headers = chat_server(tmp_path, monkeypatch) |
| 135 | monkeypatch.setenv(object_server.AI_EFFORT_ENV, "medium") |
| 136 | monkeypatch.setenv(object_server.AI_BUILDER_EFFORT_ENV, "xhigh") |
| 137 | monkeypatch.setenv(object_server.AI_MAX_TOKENS_CAP_ENV, "3000") |
| 138 | seen = [] |
| 139 | install_fake_transport(monkeypatch, [anthropic_message([text_block("ok")])] * 3, seen) |
| 140 | for tools in (CONVERSATION, BUILDER): |
| 141 | request("/api/ai/chat", method="POST", headers=headers, |
| 142 | body=json.dumps({"message": "hi", "tools": tools}).encode()) |
| 143 | request("/api/ai/chat", method="POST", headers=headers, |
| 144 | body=json.dumps({"message": "hi", "effort": "max", "max_tokens": 50}).encode()) |
| 145 | assert [s["body"]["output_config"]["effort"] for s in seen] == ["medium", "xhigh", "max"] |
| 146 | assert [s["body"]["max_tokens"] for s in seen] == [3000, 3000, 50] |
| 147 | |
| 148 | |
| 149 | def test_endpoint_refuses_a_bad_effort_or_max_tokens(tmp_path, monkeypatch): |
| 150 | _, headers = chat_server(tmp_path, monkeypatch) |
| 151 | install_fake_transport(monkeypatch, []) |
| 152 | for extra in ({"effort": "turbo"}, {"max_tokens": "lots"}, {"max_tokens": 0}): |
| 153 | status, _, body = request("/api/ai/chat", method="POST", headers=headers, |
| 154 | body=json.dumps({"message": "hi", **extra}).encode()) |
| 155 | assert status == 400, body |
| 156 | |
| 157 | |
| 158 | def test_endpoint_sends_no_effort_to_haiku_4_5(tmp_path, monkeypatch): |
| 159 | _, headers = chat_server(tmp_path, monkeypatch) |
| 160 | seen = [] |
| 161 | install_fake_transport(monkeypatch, [anthropic_message([text_block("ok")])], seen) |
| 162 | status, _, _ = request( |
| 163 | "/api/ai/chat", method="POST", headers=headers, |
| 164 | body=json.dumps({"message": "hi", "model": "anthropic:claude-haiku-4-5", |
| 165 | "tools": BUILDER}).encode()) |
| 166 | assert status == 200 |
| 167 | assert "output_config" not in seen[0]["body"] |
| 168 | assert seen[0]["body"]["max_tokens"] == 8192 |