My agentic slop goes here. Not intended for anyone else!
1(* Advanced Configuration Demo
2
3 This example demonstrates the advanced configuration options available
4 in the OCaml Claude SDK, including:
5 - Budget limits for cost control
6 - Fallback models for reliability
7 - Settings isolation for CI/CD environments
8 - Custom buffer sizes for large outputs
9*)
10
11open Eio.Std
12open Claude
13
14let log_setup () =
15 Logs.set_reporter (Logs_fmt.reporter ());
16 Logs.set_level (Some Logs.Info)
17
18(* Example 1: CI/CD Configuration
19
20 In CI/CD environments, you want isolated, reproducible behavior
21 without any user/project/local settings interfering.
22*)
23let ci_cd_config () =
24 Options.default
25 |> Options.with_no_settings (* Disable all settings loading *)
26 |> Options.with_max_budget_usd 0.50 (* 50 cent limit per run *)
27 |> Options.with_fallback_model_string "claude-haiku-4" (* Fast fallback *)
28 |> Options.with_model_string "claude-sonnet-4-5"
29 |> Options.with_permission_mode Permissions.Mode.Bypass_permissions
30
31(* Example 2: Production Configuration with Fallback
32
33 Production usage with cost controls and automatic fallback
34 to ensure availability.
35*)
36let production_config () =
37 Options.default
38 |> Options.with_model_string "claude-sonnet-4-5"
39 |> Options.with_fallback_model_string "claude-sonnet-3-5"
40 |> Options.with_max_budget_usd 10.0 (* $10 limit *)
41 |> Options.with_max_buffer_size 5_000_000 (* 5MB buffer for large outputs *)
42
43(* Example 3: Development Configuration
44
45 Development with user settings enabled but with cost controls.
46*)
47let dev_config () =
48 Options.default
49 |> Options.with_setting_sources [Options.User; Options.Project]
50 |> Options.with_max_budget_usd 1.0 (* $1 limit for dev testing *)
51 |> Options.with_fallback_model_string "claude-haiku-4"
52
53(* Example 4: Isolated Test Configuration
54
55 For automated testing with no external settings and strict limits.
56*)
57let test_config () =
58 Options.default
59 |> Options.with_no_settings
60 |> Options.with_max_budget_usd 0.10 (* 10 cent limit per test *)
61 |> Options.with_model_string "claude-haiku-4" (* Fast, cheap model *)
62 |> Options.with_permission_mode Permissions.Mode.Bypass_permissions
63 |> Options.with_max_buffer_size 1_000_000 (* 1MB buffer *)
64
65(* Example 5: Custom Buffer Size Demo
66
67 For applications that need to handle very large outputs.
68*)
69let _large_output_config () =
70 Options.default
71 |> Options.with_max_buffer_size 10_000_000 (* 10MB buffer *)
72 |> Options.with_model_string "claude-sonnet-4-5"
73
74(* Helper to run a query with a specific configuration *)
75let run_query ~sw process_mgr config prompt =
76 print_endline "\n=== Configuration ===";
77 (match Options.max_budget_usd config with
78 | Some budget -> Printf.printf "Budget limit: $%.2f\n" budget
79 | None -> print_endline "Budget limit: None");
80 (match Options.fallback_model config with
81 | Some model -> Printf.printf "Fallback model: %s\n" (Claude.Model.to_string model)
82 | None -> print_endline "Fallback model: None");
83 (match Options.setting_sources config with
84 | Some [] -> print_endline "Settings: Isolated (no settings loaded)"
85 | Some sources ->
86 let source_str = String.concat ", " (List.map (function
87 | Options.User -> "user"
88 | Options.Project -> "project"
89 | Options.Local -> "local"
90 ) sources) in
91 Printf.printf "Settings: %s\n" source_str
92 | None -> print_endline "Settings: Default");
93 (match Options.max_buffer_size config with
94 | Some size -> Printf.printf "Buffer size: %d bytes\n" size
95 | None -> print_endline "Buffer size: Default (1MB)");
96
97 print_endline "\n=== Running Query ===";
98 let client = Client.create ~options:config ~sw ~process_mgr () in
99 Client.query client prompt;
100 let messages = Client.receive client in
101
102 Seq.iter (function
103 | Message.Assistant msg ->
104 List.iter (function
105 | Content_block.Text t ->
106 Printf.printf "Response: %s\n" (Content_block.Text.text t)
107 | _ -> ()
108 ) (Message.Assistant.content msg)
109 | Message.Result result ->
110 Printf.printf "\n=== Session Complete ===\n";
111 Printf.printf "Duration: %dms\n" (Message.Result.duration_ms result);
112 (match Message.Result.total_cost_usd result with
113 | Some cost -> Printf.printf "Cost: $%.4f\n" cost
114 | None -> ());
115 Printf.printf "Turns: %d\n" (Message.Result.num_turns result)
116 | _ -> ()
117 ) messages
118
119let main () =
120 log_setup ();
121
122 Eio_main.run @@ fun env ->
123 Switch.run @@ fun sw ->
124 let process_mgr = Eio.Stdenv.process_mgr env in
125
126 print_endline "==============================================";
127 print_endline "Claude SDK - Advanced Configuration Examples";
128 print_endline "==============================================";
129
130 (* Example: CI/CD isolated environment *)
131 print_endline "\n\n### Example 1: CI/CD Configuration ###";
132 print_endline "Purpose: Isolated, reproducible environment for CI/CD";
133 let config = ci_cd_config () in
134 run_query ~sw process_mgr config "What is 2+2? Answer in one sentence.";
135
136 (* Example: Production with fallback *)
137 print_endline "\n\n### Example 2: Production Configuration ###";
138 print_endline "Purpose: Production with cost controls and fallback";
139 let config = production_config () in
140 run_query ~sw process_mgr config "Explain OCaml in one sentence.";
141
142 (* Example: Development with settings *)
143 print_endline "\n\n### Example 3: Development Configuration ###";
144 print_endline "Purpose: Development with user/project settings";
145 let config = dev_config () in
146 run_query ~sw process_mgr config "What is functional programming? One sentence.";
147
148 (* Example: Test configuration *)
149 print_endline "\n\n### Example 4: Test Configuration ###";
150 print_endline "Purpose: Automated testing with strict limits";
151 let config = test_config () in
152 run_query ~sw process_mgr config "Say 'test passed' in one word.";
153
154 print_endline "\n\n==============================================";
155 print_endline "All examples completed successfully!";
156 print_endline "=============================================="
157
158let () =
159 try
160 main ()
161 with
162 | e ->
163 Printf.eprintf "Error: %s\n" (Printexc.to_string e);
164 Printexc.print_backtrace stderr;
165 exit 1