Multi Step JSON Tool Calling 1d Optimization
Source: examples/optimization/multi_step_json_tool_calling_1d_optimization.py
Introduction
Practical Bayesian optimization motivates iterative search over expensive objective evaluations, while Toolformer and Plan-and-Solve motivate explicit action/reason loops for model-guided exploration. This example operationalizes that idea as a JSON tool-calling optimization workflow with traceable proposals and evaluations.
Technical Implementation
Configure
Tracerwith JSONL + console output so each run emits machine-readable traces and lifecycle logs.Build the runtime surface (public APIs only) and execute
MultiStepAgent.run(...)with a fixedrequest_id.Configure and invoke
Toolboxintegrations (core/script/MCP/callable) before assembling the final payload.Print a compact JSON payload including
trace_infofor deterministic tests and docs examples.
flowchart LR
A["Input prompt or scenario"] --> B["main(): runtime wiring"]
B --> C["MultiStepAgent.run(...)"]
C --> D["optimization loop combines callable tools with explicit final answers"]
C --> E["Tracer JSONL + console events"]
D --> F["ExecutionResult/payload"]
E --> F
F --> G["Printed JSON output"]
1from __future__ import annotations
2
3import json
4from collections.abc import Mapping
5from pathlib import Path
6
7from design_research_agents import (
8 CallableToolConfig,
9 LlamaCppServerLLMClient,
10 MultiStepAgent,
11 Toolbox,
12 Tracer,
13)
14
15_EXAMPLE_LLAMA_CLIENT_KWARGS = {
16 "model": "Qwen_Qwen3-4B-Instruct-2507-Q4_K_M.gguf",
17 "hf_model_repo_id": "bartowski/Qwen_Qwen3-4B-Instruct-2507-GGUF",
18 "api_model": "qwen3-4b-instruct-2507-q4km",
19 "context_window": 8192,
20 "startup_timeout_seconds": 240.0,
21 "request_timeout_seconds": 240.0,
22}
23
24
25def _objective(x: float) -> float:
26 return x * x
27
28
29def main() -> None:
30 """Optimize ``x^2`` from ``x=3`` by letting the LLM choose each tool step."""
31 # Fixed request id keeps traces and docs output deterministic across runs.
32 request_id = "example-optimization-json-tool-calling-design-001"
33 tracer = Tracer(
34 enabled=True,
35 trace_dir=Path("artifacts/examples/traces"),
36 enable_jsonl=True,
37 enable_console=True,
38 )
39 initial_x = 3.0
40 evaluation_history: list[dict[str, float]] = []
41
42 def _evaluate(payload: Mapping[str, object]) -> dict[str, object]:
43 raw_x = payload.get("x", initial_x)
44 x_value = float(raw_x) if isinstance(raw_x, (int, float)) else initial_x
45 f_x = _objective(x_value)
46 evaluation_record = {"x": x_value, "f_x": f_x}
47 evaluation_history.append(evaluation_record)
48 best_record = min(evaluation_history, key=lambda record: record["f_x"])
49 previous_record = evaluation_history[-2] if len(evaluation_history) > 1 else None
50 return {
51 "x": x_value,
52 "f_x": f_x,
53 "evaluations": len(evaluation_history),
54 "previous_x": None if previous_record is None else previous_record["x"],
55 "previous_f_x": None if previous_record is None else previous_record["f_x"],
56 "best_x": best_record["x"],
57 "best_objective": best_record["f_x"],
58 "improved_best": best_record is evaluation_record,
59 "history": list(evaluation_history),
60 }
61
62 # Run the optimization example using public runtime surfaces. Using this with statement will automatically
63 # shut down the managed client and tool runtime when the example is done.
64 with (
65 Toolbox(
66 enable_core_tools=False,
67 callable_tools=(
68 CallableToolConfig(
69 name="optimizer.evaluate",
70 description="Evaluate f(x) = x^2 at a proposed x and return the best observation so far.",
71 handler=_evaluate,
72 input_schema={
73 "type": "object",
74 "additionalProperties": False,
75 "properties": {"x": {"type": "number"}},
76 "required": ["x"],
77 },
78 ),
79 ),
80 ) as tools,
81 LlamaCppServerLLMClient(**_EXAMPLE_LLAMA_CLIENT_KWARGS) as llm_client,
82 ):
83 optimization_agent = MultiStepAgent(
84 mode="json",
85 llm_client=llm_client,
86 tool_runtime=tools,
87 max_steps=6,
88 # This example uses prompt guidance rather than tool-enforced step directions.
89 tool_calling_system_prompt=(
90 "You are solving a simple one-dimensional black-box minimization problem. "
91 "Use optimizer.evaluate to test concrete x values, and rely on observed tool results instead "
92 "of guessing numeric outcomes. Prefer a short, informative search that moves toward lower "
93 "observed objective values, then emit final_answer once the best observed x is well-supported."
94 ),
95 tracer=tracer,
96 )
97 result = optimization_agent.run(
98 prompt=(
99 "Minimize the black-box function f(x). Begin by evaluating x=3. "
100 "Use the observed results to choose a few better candidate x values, keeping the search efficient. "
101 "When you have enough evidence, emit final_answer with exactly the keys best_x, "
102 "best_objective, and evaluations, and use only values that came from tool observations."
103 ),
104 request_id=request_id,
105 )
106
107 # Print the results
108 summary = result.summary()
109 print(json.dumps(summary, ensure_ascii=True, indent=2, sort_keys=True))
110
111
112if __name__ == "__main__":
113 main()
Expected Results
Run Command
PYTHONPATH=src python3 examples/optimization/multi_step_json_tool_calling_1d_optimization.py
Example output shape (values vary by run):
{
"success": true,
"final_output": "<example-specific payload>",
"terminated_reason": "<string-or-null>",
"error": null,
"trace": {
"request_id": "<request-id>",
"trace_dir": "artifacts/examples/traces",
"trace_path": "artifacts/examples/traces/run_<timestamp>_<request_id>.jsonl"
}
}