Model backends#
A backend is how an agent talks to a model. It is a configuration choice: the graph and the science do not change with it. See Use a different model or backend for choosing one, and Installation for what each needs.
The adapters below are exported for direct use and for subclassing; a normal
run selects one by name from config.yaml and never touches these classes.
adda.ClaudeAdapter
#
Wraps claude-agent-sdk; runs one agent turn and returns assistant text.
The SDK handles its own tool-execution loop (Bash, Read, Write, Edit). This adapter converts a list of LangChain-style message dicts to the SDK format, runs the query, and assembles the final text response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
Claude model identifier. |
required |
system_prompt
|
str
|
System prompt for the agent. |
required |
study_dir
|
Path or None
|
Working directory passed to the SDK as |
None
|
native_tools
|
list[str]
|
Tool names to enable (e.g. |
None
|
closure_tools
|
dict[str, callable] or None
|
Extra Python callables exposed to the model as MCP tools. |
None
|
Source code in src/adda/_src/backends/claude.py
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 | |
HAS_BASE_PROMPT = True
class-attribute
instance-attribute
#
base_prompt: str | None = None
class-attribute
instance-attribute
#
NATIVE_TOOLS = frozenset({'Bash', 'Edit', 'Read', 'Write', 'Glob', 'Grep', 'BashOutput', 'KillShell', 'Task', 'WebFetch', 'WebSearch'})
class-attribute
instance-attribute
#
model = model
instance-attribute
#
system_prompt = system_prompt
instance-attribute
#
study_dir = Path(study_dir) if study_dir else None
instance-attribute
#
use_default_tools: bool = DEFAULT_TOOLS in (native_tools or [])
instance-attribute
#
native_tools = [t for t in native_tools or [] if t != DEFAULT_TOOLS]
instance-attribute
#
on_init_tools: Any = None
instance-attribute
#
closure_tools = dict(closure_tools or {})
instance-attribute
#
extra_mcp_servers: dict = dict(extra_mcp_servers or {})
instance-attribute
#
extra_allowed_tools: list[str] = list(extra_allowed_tools or [])
instance-attribute
#
persistent: bool = persistent
instance-attribute
#
max_history_pairs: int = max_history_pairs
instance-attribute
#
_lock: threading.Lock = threading.Lock()
instance-attribute
#
route_watcher: Any = None
instance-attribute
#
last_usage: dict = {}
instance-attribute
#
_attempt_usages: list[dict] | None = None
instance-attribute
#
last_session_id: str | None = None
instance-attribute
#
_background_watch: Any = None
instance-attribute
#
select_native_tools(agent_tools) -> list[str]
classmethod
#
Pick which of an agent's declared tools are native SDK CLI tools.
Mirror of OpenAICompatibleAdapter.select_native_tools so the runtime can choose native tools generically for any backend (forward-compatible dispatch).
Source code in src/adda/_src/backends/claude.py
467 468 469 470 471 472 473 474 475 476 477 | |
_system_prompt_option()
#
What the CLI receives: the text alone (it REPLACES Claude Code's
prompt), or, for a node with base_prompt: Default, a preset that
keeps Claude Code's prompt and appends the text.
Source code in src/adda/_src/backends/claude.py
524 525 526 527 528 529 530 531 | |
_render_system_prompt() -> str
#
The system prompt exactly as the model sees it: base prompt plus
the <tools> catalog, with every tool the prose names rewritten to
the qualified name the SDK exposes (catalog and prose must agree).
Source code in src/adda/_src/backends/claude.py
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 | |
_compute_allowed_tools(qualified_mcp_tools) -> list[str]
#
All allowed tool names, ALWAYS as a list (never None).
The SDK does list(options.allowed_tools) when building its command,
which raises TypeError on None — so a tool-less agent (e.g. the
one-shot problem-statement reviewer) must still get [] here, not
None. An empty list correctly means "no tools allowed".
Source code in src/adda/_src/backends/claude.py
549 550 551 552 553 554 555 556 557 558 559 560 561 | |
copy() -> ClaudeAdapter
#
An independent adapter for ONE delegation.
Shares configuration; owns everything a delegation mutates: its own
closure_tools (dispatch binds ReportEvals / Write / FollowUp to
that delegation's id), its own _lock and its own per-call
last_* state. Same-role delegations therefore run concurrently
instead of queueing behind one shared lock.
Source code in src/adda/_src/backends/claude.py
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 | |
ainvoke(messages: list[dict], *, idle_timeout: float | None = None, resume: str | None = None) -> str
async
#
Run one agent turn asynchronously; return assembled text.
idle_timeout overrides the run-wide llm_stream_idle_timeout for
THIS call only — used by short advisory side-calls (e.g. the verdict
validator) that must not inherit a real agent turn's generous window.
resume (spec 12 item 3): a CLI session id to resume rather than
starting fresh -- messages then carries only the NEW turn (the
prior conversation is loaded from the resumed session itself, not
replayed here). fork_session=False always, so this continues the
SAME session rather than branching a copy of it.
Source code in src/adda/_src/backends/claude.py
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 | |
_settle_usage(last_result: Any, _msg_usage: dict, last_assistant: Any) -> None
#
Record this attempt's usage and session id. Runs from ainvoke's
finally so a stream that RAISES (idle TimeoutError, API error)
still reports the usage it had streamed; invoke sums the
attempts.
Source code in src/adda/_src/backends/claude.py
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 | |
invoke(messages: list[dict], *, idle_timeout: float | None = None, retry_max: int | None = None, resume: str | None = None, on_session_start: Any = None, on_session_end: Any = None, background_watch: Any = None) -> str
#
Synchronous wrapper around :meth:ainvoke.
Acquires _lock to serialize concurrent callers of this adapter object (each delegation has its own copy(), so they do not contend). Transient API/network failures are retried with exponential backoff (see retry_on_transient).
idle_timeout / retry_max override the run-wide stream-idle and
retry budgets for THIS call only. A short advisory side-call (verdict
validator) passes a tight idle + retry_max=1 so a hung CLI stream
aborts in ~that window instead of inheriting a real turn's
5×600s budget (which once froze a whole run for ~89 min).
resume: see :meth:ainvoke.
on_session_start: called with no arguments the instant _lock
is actually acquired -- i.e. when this call's real work begins, not
when it was merely requested. This is the point at which the call's real work begins,
distinct from when it was asked to start.
Best-effort: swallows any exception so a broken callback never
breaks the real turn.
on_session_end: called as on_session_end(session_id, usage)
with THIS call's own session id and token usage while _lock is
still held, once the turn is over (also when it raised, with
whatever it produced: None / {} if nothing). Both are
per-call output; a caller that needs ITS OWN values takes them from
here rather than from last_session_id / last_usage. Best-effort
like on_session_start.
background_watch: an infra.background_jobs.BackgroundJobWatch.
It takes its baseline here and its end snapshot inside ainvoke
just before the CLI is closed, while the CLI's children still live.
Source code in src/adda/_src/backends/claude.py
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 | |
adda.OllamaAdapter
#
Adapter for Ollama-served open-weight models.
Uses ChatOpenAI pointed at Ollama's local OpenAI-compatible endpoint
(default http://localhost:11434/v1, overridable via base_url in
config.yaml). Ollama needs no real auth, so the API key is
the conventional placeholder "local".
Source code in src/adda/_src/backends/ollama.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | |
model = model
instance-attribute
#
system_prompt = system_prompt
instance-attribute
#
study_dir = Path(study_dir) if study_dir else None
instance-attribute
#
native_tools: list[str] = list(native_tools or [])
instance-attribute
#
use_default_tools: bool = False
instance-attribute
#
on_init_tools: Any = None
instance-attribute
#
closure_tools: dict[str, Any] = dict(closure_tools or {})
instance-attribute
#
_base_url = base_url or self.DEFAULT_BASE_URL
instance-attribute
#
_api_key = api_key if api_key is not None else self.API_KEY
instance-attribute
#
extra_mcp_servers: dict = dict(extra_mcp_servers or {})
instance-attribute
#
extra_allowed_tools: list[str] = list(extra_allowed_tools or [])
instance-attribute
#
persistent: bool = persistent
instance-attribute
#
max_history_pairs: int = max_history_pairs
instance-attribute
#
_lock: threading.Lock = threading.Lock()
instance-attribute
#
_summary_cache: dict[str, str] = {}
instance-attribute
#
_agent: Any = None
instance-attribute
#
route_watcher: Any = None
instance-attribute
#
_oracle_nudge = OracleNudgeBudget()
instance-attribute
#
_notice_ctx: tuple = (None, None)
instance-attribute
#
last_usage: dict = {}
instance-attribute
#
last_session_id: str | None = None
instance-attribute
#
DEFAULT_BASE_URL = 'http://localhost:11434/v1'
class-attribute
instance-attribute
#
API_KEY = 'local'
class-attribute
instance-attribute
#
API_KEY_ENV = None
class-attribute
instance-attribute
#
select_native_tools(agent_tools) -> list[str]
classmethod
#
Pick which of an agent's declared tools are NATIVE (CLI) tools.
OpenAI-compatible backends run every tool through LangChain, so a tool is native unless it is one of the Python closure tools the node injects separately. (ClaudeAdapter overrides this with its own CLI-tool set.)
Source code in src/adda/_src/backends/openai_compatible.py
777 778 779 780 781 782 783 784 785 786 787 788 789 | |
copy() -> OpenAICompatibleAdapter
#
An independent adapter for ONE delegation (see ClaudeAdapter.copy):
own closure_tools, _lock, built agent, oracle-nudge budget,
summary cache and per-call last_* state.
Source code in src/adda/_src/backends/openai_compatible.py
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 | |
_post_tool_context(tool_name: str, tool_input: dict) -> str | None
#
Raw-oracle nudge plus queued campaign notices, same text the Claude backend's post-tool hook returns. Tool closures run on other threads, so the delegation context is the one bound at invoke time.
Source code in src/adda/_src/backends/openai_compatible.py
879 880 881 882 883 884 885 886 887 888 889 | |
_record_native_error(tool_name: str, message: str, args: dict) -> None
#
Write a native tool's error result as an ERROR_RETURN row.
Source code in src/adda/_src/backends/openai_compatible.py
891 892 893 894 895 896 897 898 899 | |
_build_tools() -> list[Any]
#
Source code in src/adda/_src/backends/openai_compatible.py
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 | |
_build_agent() -> Any
#
Source code in src/adda/_src/backends/openai_compatible.py
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 | |
_resolve_context_window() -> tuple[int, str]
#
(window, source) — explicit setting, then the server, then a
declared default.
The source is returned, not just the number, because a run whose window
came from a guess and a run whose window came from the server are not
the same run. settings.py's precedence applies to the explicit
channel, so a study can pin it and an ablation can sweep it.
Source code in src/adda/_src/backends/openai_compatible.py
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 | |
_resolve_max_output_tokens() -> int | None
#
Cap on ONE reply, or None for deliberately uncapped.
This is NOT part of the context_trim feature, and that is on
purpose. Trimming decides what the model SEES and is scaffolding whose
value is an open question; this bounds what the server will DO and is
a safety limit. Gating it on the same knob would mean the arm that
answers "is trimming worth it" also removes the only thing stopping a
turn generating for an hour, which is not the question being asked. A
constant applied to both arms is not a confound.
The resolved number is logged, so a run whose reply was cut short is distinguishable from a run whose model simply stopped.
Source code in src/adda/_src/backends/openai_compatible.py
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 | |
_summarize(prompt: str) -> str
#
One summary, from the model this run is already using.
Deliberately not a second, smaller model. A fixed summariser would remove one confound (summary quality no longer varies with the arm) and introduce two: a dependency the study does not otherwise have, and a capability the run itself never had. The interesting hypothesis on cheap models is that scaffolding compensates for capability, and a 27B run whose context is curated by a frontier model is not testing that.
Goes straight to the chat model, NOT through the agent: a summary produced by a tool-loop could call tools, and a pre_model_hook that re-enters the agent is a recursion, not a hook.
Source code in src/adda/_src/backends/openai_compatible.py
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 | |
_context_hook(system_prompt: str)
#
A pre_model_hook that keeps one turn inside the served window.
context_policy picks HOW. Both values manage the context; neither
is "off", because an unmanaged context is not an experimental arm —
it is the crash this subsystem was written to stop (Ollama evicts the
original user turn and its renderer then rejects the request with
500 no user query found in messages).
compact (the default) replaces the middle of the conversation with
a summary, so a delegation's RESULT survives even when its prose does
not. trim drops those messages instead: free, deterministic, and
blind to what it is throwing away. The trade is fidelity against
determinism — a summary can quietly restate a number, a dropped
message obviously cannot — and it is recorded per run so an analysis
can condition on it rather than assume it.
The hook returns llm_input_messages, which changes only what is
SENT. Graph state keeps every message, so the transcript on disk stays
complete and a compacted run is still fully auditable.
Source code in src/adda/_src/backends/openai_compatible.py
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 | |
_context_policy() -> str
staticmethod
#
"compact" or "trim". An unknown value RAISES.
Silently falling back to a default would make a typo'd arm run as the baseline and report as a null result, which is the one failure an ablation cannot afford.
Source code in src/adda/_src/backends/openai_compatible.py
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 | |
invoke(messages: list[dict], *, idle_timeout: float | None = None, retry_max: int | None = None, on_session_start: Any = None, on_session_end: Any = None, background_watch: Any = None) -> str
#
Run one full agent turn; return final assistant text.
Acquires _lock to serialize concurrent callers of this adapter object (each delegation has its own copy(), so they do not contend). Transient API/network failures are retried with exponential backoff (see retry_on_transient).
idle_timeout / retry_max give short advisory side-calls a tight
budget. idle_timeout is accepted for signature parity with the
Claude backend (HTTP requests carry their own socket timeout, so it is
not separately applied here); retry_max caps retries for this call.
on_session_start: see ClaudeAdapter.invoke's docstring -- called
the instant _lock is actually acquired, so a caller queued
behind another same-role delegation learns when its wait is really
over. Best-effort.
on_session_end: see ClaudeAdapter.invoke's docstring -- called
as on_session_end(session_id, usage) under the lock, also when
the turn raised (_capture_usage runs on the failure path too).
This backend has no resumable sessions, so the id is always None.
Source code in src/adda/_src/backends/openai_compatible.py
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 | |
_invoke_once(messages: list[dict]) -> str
#
Core invoke logic — build agent if needed, run, return text.
Source code in src/adda/_src/backends/openai_compatible.py
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 | |
_capture_usage(lc_msgs: list, result: dict | None) -> None
#
Set last_usage from every model call this turn produced.
Called on the success path AND from the failure path, because a turn that raises has still spent whatever the server generated before it died — dropping it undercounts by the whole turn, which on a run whose delegations fail is most of the run.
result["messages"] is the WHOLE graph state, i.e. the input
messages we passed in (lc_msgs) followed by everything the agent
loop generated this call. The model is invoked once per tool-calling
round trip, so a strategizer turn that loops several times before its
final reply produces several AI messages, each carrying its own
usage_metadata — reading only the last one silently dropped every
intermediate call's tokens.
Summing must stop at the input boundary: thread_id is fresh per
invoke, so the graph never carries earlier turns' messages into this
call, and lc_msgs is exactly the prefix the graph started from —
the reducer only ever appends. Slicing at len(lc_msgs) therefore
counts exactly the messages this invocation produced, never the
history that seeded it; summing over the whole state instead would
double-count that history on every call and inflate every total.
Only AI messages carry usage_metadata (tool/human messages don't), so anything without it contributes zero rather than raising.
Source code in src/adda/_src/backends/openai_compatible.py
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 | |
_probe_context_window() -> int | None
#
Ollama's SERVED window, which is not the model's trained length.
/v1/models reports neither, so this uses Ollama's native
/api/show. The distinction matters more here than anywhere: a
Modelfile's num_ctx is what the server will actually accept, and
Ollama's default is far below what any of these models were trained
for. Taking context_length from model_info would report the
trained window, sail past the real limit, and produce exactly the
server-side truncation this trimming exists to prevent — so the
parameter wins and the trained length is only a fallback.
Source code in src/adda/_src/backends/ollama.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | |