@ax-llm/ax 19.0.19 → 19.0.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ax-llm/ax",
3
- "version": "19.0.19",
3
+ "version": "19.0.21",
4
4
  "type": "module",
5
5
  "description": "The best library to work with LLMs",
6
6
  "repository": {
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  name: ax-agent
3
3
  description: This skill helps an LLM generate correct AxAgent code using @ax-llm/ax. Use when the user asks about agent(), child agents, namespaced functions, discovery mode, shared fields, llmQuery(...), RLM code execution, or offline tuning with agent.optimize(...).
4
- version: "19.0.19"
4
+ version: "19.0.21"
5
5
  ---
6
6
 
7
7
  # AxAgent Codegen Rules (@ax-llm/ax)
@@ -18,6 +18,8 @@ Use this skill to generate `AxAgent` code. Prefer short, modern, copyable patter
18
18
  - If `functions.discovery` is `true`, discover callables from modules before using them.
19
19
  - In stdout-mode RLM, use one observable `console.log(...)` step per non-final actor turn.
20
20
  - For long RLM tasks, prefer `contextPolicy: { preset: 'adaptive' }` so older successful turns collapse into checkpoint summaries while live runtime state stays visible.
21
+ - Default `actorOptions.promptLevel` to `'detailed'` and opt down to `'basic'` only when the user wants a shorter actor prompt.
22
+ - Use `actorTurnCallback` when the user needs per-turn observability into generated code, raw runtime result, formatted output, or provider thoughts.
21
23
 
22
24
  ## Mental Model
23
25
 
@@ -46,9 +48,51 @@ Important:
46
48
 
47
49
  - `contextPolicy` controls prompt replay and compression, not runtime persistence.
48
50
  - A value created by successful actor code still exists in the runtime session even if the earlier turn is later shown only as a summary or checkpoint.
49
- - Used discovery docs are replay artifacts too: `adaptive` and `lean` can hide old `listModuleFunctions(...)` / `getFunctionDefinitions(...)` output after the actor successfully uses the discovered callable.
51
+ - Used discovery docs are replay artifacts too: `lean` hides old `listModuleFunctions(...)` / `getFunctionDefinitions(...)` output by default after the actor successfully uses the discovered callable, while `adaptive` keeps them unless you opt into pruning.
50
52
  - Reliability-first defaults now prefer "summarize first, delete only when clearly safe" instead of aggressively pruning older evidence as soon as context grows.
51
53
 
54
+ ## Choosing Presets, Prompt Level, And Model Size
55
+
56
+ Treat these three knobs as a bundle:
57
+
58
+ - `contextPolicy.preset` decides how much raw history the actor keeps seeing.
59
+ - `actorOptions.promptLevel` decides how prescriptive the actor prompt is.
60
+ - Model size decides how well the actor can recover from compressed context and terse guidance.
61
+
62
+ Recommended combinations:
63
+
64
+ - Short task, debugging, or weaker/cheaper model: `preset: 'full'` with `promptLevel: 'detailed'`.
65
+ - Long multi-turn task, general default, medium-to-strong model: `preset: 'adaptive'` with `promptLevel: 'detailed'`.
66
+ - Long task where the actor keeps making avoidable exploration mistakes: `preset: 'adaptive'` with `promptLevel: 'detailed'`.
67
+ - Very long task under token pressure, stronger model only: `preset: 'lean'` with `promptLevel: 'basic'`.
68
+ - Discovery-heavy or schema-uncertain work with a capable model: `preset: 'adaptive'` with `promptLevel: 'detailed'`.
69
+
70
+ Practical rule:
71
+
72
+ - The leaner the replay policy, the stronger the model should usually be.
73
+ - `full` gives the model more raw evidence, so smaller models often do better there.
74
+ - `adaptive` is the default middle ground for real agent work.
75
+ - `lean` should be reserved for models that can reason well from runtime state plus summaries instead of exact old code/output.
76
+ - `detailed` is not automatically "better"; it is more controlling. Use it when the actor needs tighter exploration rhythm, not just because the task is hard.
77
+
78
+ Prompt-level guidance:
79
+
80
+ - `basic`: opt-down mode. Gives the actor concise exploration guidance and works best when the model is already reliable.
81
+ - `detailed`: adds explicit exploration recipes, truncation recovery guidance, error-avoidance tips, and stronger one-step-per-turn discipline.
82
+
83
+ Use `promptLevel: 'detailed'` when:
84
+
85
+ - the actor is probing unfamiliar or messy data shapes
86
+ - discovery mode is central to the task
87
+ - the model keeps over-logging, combining too many steps, or guessing field/function names
88
+ - you are optimizing for reliability over prompt compactness
89
+
90
+ Use `promptLevel: 'basic'` when:
91
+
92
+ - the model is already following the one-step rhythm well
93
+ - you want a shorter actor prompt
94
+ - the task is straightforward and the runtime/state carries most of the complexity
95
+
52
96
  ## Critical Rules
53
97
 
54
98
  - Use `agent(...)` factory syntax for new code.
@@ -527,9 +571,11 @@ Rules:
527
571
  - Use `preset: 'full'` when the actor should keep seeing raw prior code and outputs with minimal compression.
528
572
  - Use `preset: 'adaptive'` when the task needs runtime state across many turns but older successful work should collapse into checkpoint summaries while important recent steps can still stay fully replayed.
529
573
  - Use `preset: 'lean'` when you want more aggressive compression and can rely mostly on current runtime state plus checkpoint summaries and compact action summaries.
574
+ - `adaptive` now keeps used discovery docs by default and uses slightly richer live-state/checkpoint settings than `lean`; it should be the first choice unless you have a strong reason to prefer `full` or `lean`.
530
575
  - Use `state.summary` to inject a compact `Live Runtime State` block into the actor prompt. The block is structured and provenance-aware: variables are rendered with compact type/size/preview metadata, and when Ax can infer it, a short source suffix like `from t3 via db.search` is included. Combine `maxEntries` with `maxChars` so large runtime objects do not dominate the prompt.
531
576
  - Use `state.inspect` with `inspectThresholdChars` so the actor is reminded to call `inspect_runtime()` when replayed action history starts getting large.
532
- - `adaptive` and `lean` hide used discovery docs by default; set `contextPolicy.pruneUsedDocs: false` if you want to keep replaying them.
577
+ - `adaptive` keeps used discovery docs by default; set `contextPolicy.pruneUsedDocs: true` only when you want more aggressive cleanup.
578
+ - `lean` hides used discovery docs by default; set `contextPolicy.pruneUsedDocs: false` if you want to keep replaying them.
533
579
  - `full` keeps used discovery docs by default; set `contextPolicy.pruneUsedDocs: true` if you want the same cleanup there.
534
580
  - Use `summarizerOptions` to tune the internal checkpoint-summary AxGen program.
535
581
  - If you configure `expert.tombstones`, treat the object form as options for the internal tombstone-summary AxGen program.
@@ -559,6 +605,88 @@ Turn 3:
559
605
  final({ answer: '...' });
560
606
  ```
561
607
 
608
+ ## Actor Turn Observability
609
+
610
+ Use `actorTurnCallback` when the caller needs structured telemetry for each actor turn.
611
+
612
+ What it gives you:
613
+
614
+ - `code`: the normalized JavaScript code the actor produced
615
+ - `result`: the raw untruncated runtime return value from executing that code
616
+ - `output`: the formatted action-log output string after Ax normalizes and truncates it for prompt replay
617
+ - `thought`: the actor model's `thought` field when `showThoughts` is enabled and the provider returns one
618
+ - `actorResult`: the full actor payload, including actor-owned output fields when `actorFields` are configured
619
+ - `isError`: whether the execution path for that turn was treated as an error
620
+
621
+ Use it for:
622
+
623
+ - debug UIs that want to show code plus raw runtime results
624
+ - tracing and analytics
625
+ - capturing `thought` for internal diagnostics when supported by the provider
626
+ - storing per-turn execution artifacts without scraping the prompt/action log
627
+
628
+ Important:
629
+
630
+ - `output` is not raw stdout; it is the formatted replay string used in the action log.
631
+ - `result` is the raw runtime result before Ax formats/truncates it.
632
+ - `thought` is optional and only appears when the underlying `AxGen` call had `showThoughts` enabled and the provider actually returned a thought field.
633
+
634
+ Good pattern:
635
+
636
+ ```typescript
637
+ const supportAgent = agent('query:string -> answer:string', {
638
+ contextFields: ['query'],
639
+ runtime,
640
+ actorTurnCallback: ({ turn, code, result, output, thought, isError }) => {
641
+ console.log({
642
+ turn,
643
+ isError,
644
+ code,
645
+ rawResult: result,
646
+ replayOutput: output,
647
+ thought,
648
+ });
649
+ },
650
+ actorOptions: {
651
+ model: 'gpt-5.4-mini',
652
+ showThoughts: true,
653
+ },
654
+ });
655
+ ```
656
+
657
+ ## Actor Prompt Controls
658
+
659
+ Use `actorOptions` for actor-only model/prompt tuning and `responderOptions` for responder-only tuning.
660
+
661
+ Key fields:
662
+
663
+ - `actorOptions.description`: append extra actor-specific instructions without changing the responder prompt
664
+ - `actorOptions.promptLevel`: choose `'basic'` or `'detailed'` guidance for the actor template
665
+ - `actorOptions.model` / `responderOptions.model`: split model choice across actor and responder when needed
666
+
667
+ Good split-model pattern:
668
+
669
+ ```typescript
670
+ const researchAgent = agent('query:string -> answer:string', {
671
+ contextFields: ['query'],
672
+ runtime,
673
+ contextPolicy: { preset: 'adaptive' },
674
+ actorOptions: {
675
+ model: 'gpt-5.4',
676
+ promptLevel: 'detailed',
677
+ },
678
+ responderOptions: {
679
+ model: 'gpt-5.4-mini',
680
+ },
681
+ });
682
+ ```
683
+
684
+ Model guidance:
685
+
686
+ - Put the stronger model on the actor when the task depends on multi-turn exploration, discovery, runtime state reuse, or compressed replay.
687
+ - Put the stronger model on the responder only when the hard part is final synthesis/formatting rather than exploration.
688
+ - For cost-sensitive setups, a common pattern is stronger actor + cheaper responder, not the other way around.
689
+
562
690
  Invalid pattern:
563
691
 
564
692
  ```javascript
@@ -752,11 +880,25 @@ Rules:
752
880
 
753
881
  - `llmQuery(...)` forwards only the explicit `context` argument.
754
882
  - Parent inputs are not automatically available to `llmQuery(...)` children.
883
+ - In `mode: 'simple'`, `llmQuery(...)` is a direct semantic helper.
884
+ - In `mode: 'advanced'`, `llmQuery(...)` delegates a focused subtask to a child `AxAgent` with its own runtime and action log while recursion depth remains.
885
+ - In advanced mode, no parent `contextFields` are auto-inserted into recursive children. Only explicit `llmQuery(..., context)` payload is available there.
886
+ - If `context` is a plain object, safe keys are exposed as child runtime globals and the full payload is also available as `context`.
887
+ - In advanced mode, use `llmQuery(...)` to offload discovery-heavy, tool-heavy, or multi-turn semantic branches so the parent action log stays smaller and more focused.
888
+ - In advanced mode, use batched `llmQuery([...])` only for independent subtasks. Use serial calls when later work depends on earlier results.
889
+ - In advanced mode, a good pattern is: parent does coarse discovery and JS narrowing, child `llmQuery(...)` calls handle focused branch analysis, then parent merges child outputs and finishes.
890
+ - In advanced mode with `functions.discovery: true`, prefer putting noisy tool discovery, `getFunctionDefinitions(...)`, and branch-specific tool chatter inside delegated child calls when those branches are independent or semantically distinct.
891
+ - In advanced mode, pass compact named object context to children instead of huge raw parent payloads. This makes the delegated prompt easier to follow and gives the child useful top-level globals.
892
+ - In advanced mode, do not assume child-created variables, discovered docs, or action-log history come back to the parent. Only the child return value comes back.
893
+ - In advanced mode, if a child calls `ask_clarification(...)`, that clarification bubbles up and ends the top-level run.
894
+ - In advanced mode, recursion is depth-limited: `maxDepth: 0` makes top-level `llmQuery(...)` simple, `maxDepth: 1` makes top-level `llmQuery(...)` advanced and child `llmQuery(...)` simple.
895
+ - In advanced mode, batched delegated children are cancelled when a sibling child asks for clarification or aborts, so use batched form only when those branches are truly independent.
896
+ - `maxSubAgentCalls` is a shared budget across the whole top-level run, including recursive children.
755
897
  - Single-call `llmQuery(...)` may return `[ERROR] ...` on non-abort failures.
756
898
  - Batched `llmQuery([...])` returns per-item `[ERROR] ...`.
757
899
  - If a result starts with `[ERROR]`, inspect or branch on it instead of assuming success.
758
900
 
759
- Example:
901
+ Minimal example:
760
902
 
761
903
  ```javascript
762
904
  const summary = await llmQuery('Summarize this incident', inputs.context);
@@ -767,6 +909,70 @@ if (summary.startsWith('[ERROR]')) {
767
909
  }
768
910
  ```
769
911
 
912
+ Advanced recursive discovery example:
913
+
914
+ ```javascript
915
+ const narrowedIncidents = incidents.map((incident) => ({
916
+ id: incident.id,
917
+ timeline: incident.timeline,
918
+ notes: incident.notes.slice(0, 1200),
919
+ }));
920
+
921
+ const [severityReview, followupReview] = await llmQuery([
922
+ {
923
+ query:
924
+ 'Use discovery and available tools to review severity policy alignment. Return compact findings.',
925
+ context: {
926
+ incidents: narrowedIncidents,
927
+ rubric: 'severity-policy',
928
+ },
929
+ },
930
+ {
931
+ query:
932
+ 'Use discovery and available tools to review postmortem and follow-up obligations. Return compact findings.',
933
+ context: {
934
+ incidents: narrowedIncidents,
935
+ rubric: 'postmortem-followup',
936
+ },
937
+ },
938
+ ]);
939
+
940
+ const merged = await llmQuery(
941
+ 'Merge these delegated reviews into one manager-ready summary with next steps.',
942
+ {
943
+ severityReview,
944
+ followupReview,
945
+ audience: inputs.audience,
946
+ }
947
+ );
948
+ ```
949
+
950
+ Delegation decision guide:
951
+
952
+ - **JS-only** — deterministic logic (filter, sort, count, regex, date math) → do it inline, don't delegate.
953
+ - **Single-shot semantic** — needs LLM reasoning but no tools or multi-step exploration → single `llmQuery` with narrow context.
954
+ - **Full delegation** — needs its own discovery, tool calls, or >2 turns of exploratory work → `llmQuery` as child agent.
955
+ - **Parallel fan-out** — 2+ independent subtasks each qualifying for delegation → batched `llmQuery([...])`.
956
+
957
+ Context handling:
958
+
959
+ - In advanced mode, the `context` object is injected into the child's JS runtime as named globals — it does NOT go into the child's LLM prompt. The child's prompt sees only a compact metadata summary (types, sizes, element keys) of the delegated context.
960
+ - The child actor explores the delegated context with code, the same way the parent explores `inputs.*`.
961
+ - Always narrow with JS before delegating — never pass raw `inputs.*`. Name context keys semantically (e.g. `{ emails: filtered, rubric: 'classify-urgency' }`).
962
+ - Estimate total sub-agent calls before fanning out. `maxSubAgentCalls` is a shared budget across all recursion levels.
963
+
964
+ Divide-and-conquer patterns:
965
+
966
+ - **Fan-Out / Fan-In**: JS narrows into categories → `llmQuery([...])` fans out per category → JS or one more `llmQuery` merges results.
967
+ - **Pipeline**: serial `llmQuery` calls where each depends on the prior result.
968
+ - **Scout-then-Execute**: first child explores (e.g. check availability) → parent processes with JS → second child acts (e.g. draft invite).
969
+
970
+ Notes:
971
+
972
+ - Use these patterns when one task naturally splits into focused semantic branches with their own discovery or tool usage.
973
+ - Keep the parent responsible for orchestration, cheap JS narrowing, and final assembly.
974
+ - See `src/examples/rlm-discovery.ts` for the full recursive discovery demo.
975
+
770
976
  ## Short API Reference
771
977
 
772
978
  ### `agentIdentity`
@@ -817,18 +1023,29 @@ agentIdentity?: {
817
1023
  maxTurns?: number;
818
1024
  contextPolicy?: AxContextPolicyConfig;
819
1025
  actorFields?: string[];
820
- actorCallback?: (result: Record<string, unknown>) => void | Promise<void>;
1026
+ actorTurnCallback?: (turn: {
1027
+ turn: number;
1028
+ actorResult: Record<string, unknown>;
1029
+ code: string;
1030
+ result: unknown;
1031
+ output: string;
1032
+ isError: boolean;
1033
+ thought?: string;
1034
+ }) => void | Promise<void>;
821
1035
  inputUpdateCallback?: (currentInputs: Record<string, unknown>) => Promise<Record<string, unknown> | undefined> | Record<string, unknown> | undefined;
822
1036
  mode?: 'simple' | 'advanced';
823
1037
  recursionOptions?: Partial<Omit<AxProgramForwardOptions, 'functions'>> & {
824
1038
  maxDepth?: number;
1039
+ promptLevel?: 'detailed' | 'basic';
825
1040
  };
826
- actorOptions?: Partial<AxProgramForwardOptions & { description?: string }>;
1041
+ actorOptions?: Partial<AxProgramForwardOptions & { description?: string; promptLevel?: 'detailed' | 'basic' }>;
827
1042
  responderOptions?: Partial<AxProgramForwardOptions & { description?: string }>;
828
1043
  judgeOptions?: Partial<AxJudgeOptions>;
829
1044
  }
830
1045
  ```
831
1046
 
1047
+ - `actorTurnCallback` fires for the root agent and for recursive child agents that run actor turns.
1048
+
832
1049
  ## Examples
833
1050
 
834
1051
  Fetch these for full working code:
@@ -839,7 +1056,7 @@ Fetch these for full working code:
839
1056
  - [Smart Home](https://raw.githubusercontent.com/ax-llm/ax/refs/heads/main/src/examples/smart-home.ts) — state management
840
1057
  - [RLM](https://raw.githubusercontent.com/ax-llm/ax/refs/heads/main/src/examples/rlm.ts) — RLM basic
841
1058
  - [RLM Long Task](https://raw.githubusercontent.com/ax-llm/ax/refs/heads/main/src/examples/rlm-long-task.ts) — RLM context policy
842
- - [RLM Discovery](https://raw.githubusercontent.com/ax-llm/ax/refs/heads/main/src/examples/rlm-discovery.ts) — discovery mode
1059
+ - [RLM Discovery](https://raw.githubusercontent.com/ax-llm/ax/refs/heads/main/src/examples/rlm-discovery.ts) — advanced recursive `llmQuery` plus discovery-heavy delegated subtasks
843
1060
  - [RLM Shared Fields](https://raw.githubusercontent.com/ax-llm/ax/refs/heads/main/src/examples/rlm-shared-fields.ts) — shared fields
844
1061
  - [RLM Adaptive Replay](https://raw.githubusercontent.com/ax-llm/ax/refs/heads/main/src/examples/rlm-adaptive-replay.ts) — adaptive replay
845
1062
  - [RLM Live Runtime State](https://raw.githubusercontent.com/ax-llm/ax/refs/heads/main/src/examples/rlm-live-runtime-state.ts) — structured runtime-state rendering
package/skills/ax-ai.md CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  name: ax-ai
3
3
  description: This skill helps an LLM generate correct AI provider setup and configuration code using @ax-llm/ax. Use when the user asks about ai(), providers, models, presets, embeddings, extended thinking, context caching, or mentions OpenAI/Anthropic/Google/Azure/Groq/DeepSeek/Mistral/Cohere/Together/Ollama/HuggingFace/Reka/OpenRouter with @ax-llm/ax.
4
- version: "19.0.19"
4
+ version: "19.0.21"
5
5
  ---
6
6
 
7
7
  # AI Provider Codegen Rules (@ax-llm/ax)
package/skills/ax-flow.md CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  name: ax-flow
3
3
  description: This skill helps an LLM generate correct AxFlow workflow code using @ax-llm/ax. Use when the user asks about flow(), AxFlow, workflow orchestration, parallel execution, DAG workflows, conditional routing, map/reduce patterns, or multi-node AI pipelines.
4
- version: "19.0.19"
4
+ version: "19.0.21"
5
5
  ---
6
6
 
7
7
  # AxFlow Codegen Rules (@ax-llm/ax)
package/skills/ax-gen.md CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  name: ax-gen
3
3
  description: This skill helps an LLM generate correct AxGen code using @ax-llm/ax. Use when the user asks about ax(), AxGen, generators, forward(), streamingForward(), assertions, field processors, step hooks, self-tuning, or structured outputs.
4
- version: "19.0.19"
4
+ version: "19.0.21"
5
5
  ---
6
6
 
7
7
  # AxGen Codegen Rules (@ax-llm/ax)
package/skills/ax-gepa.md CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  name: ax-gepa
3
3
  description: This skill helps an LLM generate correct AxGEPA optimization code using @ax-llm/ax. Use when the user asks about AxGEPA, GEPA, Pareto optimization, multi-objective prompt tuning, reflective prompt evolution, validationExamples, maxMetricCalls, or optimizing a generator, flow, or agent tree.
4
- version: "19.0.19"
4
+ version: "19.0.21"
5
5
  ---
6
6
 
7
7
  # AxGEPA Codegen Rules (@ax-llm/ax)
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  name: ax-learn
3
3
  description: This skill helps an LLM generate correct AxLearn code using @ax-llm/ax. Use when the user asks about self-improving agents, trace-backed learning, feedback-aware updates, or AxLearn modes.
4
- version: "19.0.19"
4
+ version: "19.0.21"
5
5
  ---
6
6
 
7
7
  # AxLearn Codegen Rules (@ax-llm/ax)
package/skills/ax-llm.md CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  name: ax
3
3
  description: This skill helps with using the @ax-llm/ax TypeScript library for building LLM applications. Use when the user asks about ax(), ai(), f(), s(), agent(), flow(), AxGen, AxAgent, AxFlow, signatures, streaming, or mentions @ax-llm/ax.
4
- version: "19.0.19"
4
+ version: "19.0.21"
5
5
  ---
6
6
 
7
7
  # Ax Library (@ax-llm/ax) Quick Reference
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  name: ax-signature
3
3
  description: This skill helps an LLM generate correct DSPy signature code using @ax-llm/ax. Use when the user asks about signatures, s(), f(), field types, string syntax, fluent builder API, validation constraints, or type-safe inputs/outputs.
4
- version: "19.0.19"
4
+ version: "19.0.21"
5
5
  ---
6
6
 
7
7
  # Ax Signature Reference