%22%22%22A%20marimo%20notebook%20that%20runs%20the%20BAML%20agent%3A%20a%20language%20model%20that%20explains%20an%20EBM%20through%20the%20%60%60ebaml-mcp%60%60%20tools.%22%22%22%0A%0Aimport%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20os%0A%20%20%20%20import%20shutil%0A%20%20%20%20import%20subprocess%20%20%23%20ruff%3A%20ignore%5Bsuspicious-subprocess-import%5D%20--%20the%20notebook%20runs%20the%20baml%20command%2C%20which%20is%20the%20interface%20that%20it%20shows%0A%20%20%20%20import%20sys%0A%20%20%20%20import%20tempfile%0A%20%20%20%20from%20pathlib%20import%20Path%0A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20orjson%0A%20%20%20%20import%20polars%20as%20pl%0A%20%20%20%20import%20skops.io%0A%20%20%20%20from%20interpret.glassbox%20import%20ExplainableBoostingClassifier%0A%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20ExplainableBoostingClassifier%2C%0A%20%20%20%20%20%20%20%20Path%2C%0A%20%20%20%20%20%20%20%20mo%2C%0A%20%20%20%20%20%20%20%20np%2C%0A%20%20%20%20%20%20%20%20orjson%2C%0A%20%20%20%20%20%20%20%20os%2C%0A%20%20%20%20%20%20%20%20pl%2C%0A%20%20%20%20%20%20%20%20shutil%2C%0A%20%20%20%20%20%20%20%20skops%2C%0A%20%20%20%20%20%20%20%20subprocess%2C%0A%20%20%20%20%20%20%20%20sys%2C%0A%20%20%20%20%20%20%20%20tempfile%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20The%20BAML%20agent%0A%0A%20%20%20%20The%20%5BMCP%20server%5D(mcp-notebook.md)%20gives%20a%20client%20the%20tools%20to%20read%20an%20EBM.%20This%20notebook%20runs%20a%20client%20that%20is%20a%0A%20%20%20%20language%20model.%20The%20BAML%20function%20%60explain_with_mcp%60%20starts%20%60ebaml-mcp%60%2C%20offers%20its%20tools%20to%20the%20model%2C%20lets%20the%0A%20%20%20%20model%20call%20them%2C%20and%20returns%20the%20report%20together%20with%20the%20list%20of%20tool%20calls.%20You%20learn%20how%20to%20run%20the%20agent%20from%0A%20%20%20%20the%20%60baml%60%20command%2C%20how%20to%20read%20what%20it%20did%2C%20and%20how%20it%20reports%20a%20setup%20problem.%0A%0A%20%20%20%20-%20You%20need%20no%20data%20download.%20The%20notebook%20fits%20a%20small%20EBM%20on%20made-up%20data.%0A%20%20%20%20-%20You%20need%20the%20BAML%20toolchain%20(%60baml%60%2C%20version%200.25%20or%20later)%20and%20the%20%60mcp%60%20extra%3A%20%60pip%20install%20%22ebaml%5Bmcp%5D%22%60.%0A%20%20%20%20-%20Every%20step%20runs%20without%20a%20key%2C%20except%20the%20last%20button.%20It%20sends%20real%20requests%20to%20an%20LLM.%20The%20default%20model%0A%20%20%20%20%20%20needs%20%60OPENROUTER_API_KEY%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Path%2C%20mo%2C%20os%2C%20shutil)%3A%0A%20%20%20%20project_root%20%3D%20Path(__file__).resolve().parent.parent%0A%20%20%20%20%23%20BAML%20puts%20its%20tools%20in%20~%2F.baml%2Fbin%2C%20and%20an%20older%20%60baml%60%20has%20no%20%60describe%60%0A%20%20%20%20baml_path%20%3D%20os.pathsep.join(%5Bstr(Path.home()%20%2F%20%22.baml%22%20%2F%20%22bin%22)%2C%20os.environ.get(%22PATH%22%2C%20%22%22)%5D)%0A%20%20%20%20baml_command%20%3D%20shutil.which(%22baml%22%2C%20path%3Dbaml_path)%0A%20%20%20%20mo.stop(%0A%20%20%20%20%20%20%20%20baml_command%20is%20None%2C%0A%20%20%20%20%20%20%20%20mo.callout(%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(%22The%20%60baml%60%20command%20is%20not%20on%20the%20path.%20Install%20the%20BAML%20toolchain%2C%20then%20reopen%20the%20notebook.%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20kind%3D%22danger%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20)%0A%20%20%20%20mo.callout(mo.md(f%22Found%20%60baml%60%20at%20%60%7Bbaml_command%7D%60.%22)%2C%20kind%3D%22success%22)%0A%20%20%20%20return%20baml_command%2C%20baml_path%2C%20project_root%0A%0A%0A%40app.cell%0Adef%20_(baml_command%2C%20baml_path%2C%20os%2C%20project_root%2C%20subprocess)%3A%0A%20%20%20%20def%20run_baml(*arguments%3A%20str%2C%20drop_key%3A%20bool%20%3D%20False%2C%20timeout%3A%20float%20%3D%20600)%20-%3E%20str%3A%0A%20%20%20%20%20%20%20%20%22%22%22Run%20%60%60baml%60%60%20in%20the%20project%20and%20give%20its%20output.%0A%0A%20%20%20%20%20%20%20%20The%20key%20of%20the%20environment%20is%20replaced%20by%20a%20dummy%2C%20because%20the%20offline%20tests%20need%20a%20variable%20that%20exists.%0A%20%20%20%20%20%20%20%20With%20%60%60drop_key%60%60%2C%20the%20variable%20is%20removed%2C%20to%20show%20the%20error%20of%20a%20missing%20key.%0A%0A%20%20%20%20%20%20%20%20Returns%0A%20%20%20%20%20%20%20%20-------%0A%20%20%20%20%20%20%20%20str%0A%20%20%20%20%20%20%20%20%20%20%20%20The%20standard%20output%20and%20the%20standard%20error%20of%20the%20command%2C%20without%20BAML%20warnings.%0A%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20environment%20%3D%20%7B**os.environ%2C%20%22PATH%22%3A%20baml_path%7D%0A%20%20%20%20%20%20%20%20if%20drop_key%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20environment.pop(%22OPENROUTER_API_KEY%22%2C%20None)%0A%20%20%20%20%20%20%20%20completed%20%3D%20subprocess.run(%0A%20%20%20%20%20%20%20%20%20%20%20%20%5Bbaml_command%2C%20*arguments%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20cwd%3Dproject_root%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20env%3Denvironment%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20capture_output%3DTrue%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20text%3DTrue%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20timeout%3Dtimeout%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20check%3DFalse%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20lines%20%3D%20(completed.stdout%20%2B%20completed.stderr).splitlines()%0A%20%20%20%20%20%20%20%20return%20%22%5Cn%22.join(line%20for%20line%20in%20lines%20if%20%22warning%5B%22%20not%20in%20line%20and%20line.strip())%0A%0A%20%20%20%20return%20(run_baml%2C)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Fit%20and%20save%20a%20model%0A%0A%20%20%20%20The%20agent%20reaches%20the%20model%20through%20the%20server%2C%20so%20the%20server%20needs%20a%20saved%20model%20and%20a%20CSV%20file%20with%20rows.%20The%0A%20%20%20%20notebook%20fits%20a%20binary%20classifier%20on%20three%20features%20and%20saves%20it%20with%20%60skops.io%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(ExplainableBoostingClassifier%2C%20Path%2C%20np%2C%20pl%2C%20skops%2C%20tempfile)%3A%0A%20%20%20%20workdir%20%3D%20Path(tempfile.mkdtemp(prefix%3D%22ebaml-agent-%22))%0A%20%20%20%20random_generator%20%3D%20np.random.default_rng(0)%0A%20%20%20%20people%20%3D%20pl.DataFrame(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22age%22%3A%20random_generator.integers(18%2C%2080%2C%20500)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22income%22%3A%20random_generator.normal(50%2C%2015%2C%20500).round(1)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22city%22%3A%20random_generator.choice(%5B%22north%22%2C%20%22south%22%5D%2C%20500)%2C%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20)%0A%20%20%20%20score%20%3D%20(%0A%20%20%20%20%20%20%20%20(people%5B%22age%22%5D.to_numpy()%20-%2040)%20%2F%2020%0A%20%20%20%20%20%20%20%20%2B%20(people%5B%22income%22%5D.to_numpy()%20-%2050)%20%2F%2015%0A%20%20%20%20%20%20%20%20%2B%20(people%5B%22city%22%5D.to_numpy()%20%3D%3D%20%22north%22)%0A%20%20%20%20%20%20%20%20%2B%20random_generator.normal(size%3D500)%0A%20%20%20%20)%0A%20%20%20%20approval_threshold%20%3D%200.5%0A%20%20%20%20approved%20%3D%20(score%20%3E%20approval_threshold).astype(int)%0A%20%20%20%20classifier%20%3D%20ExplainableBoostingClassifier(feature_names%3Dpeople.columns%2C%20interactions%3D0%2C%20random_state%3D0)%0A%20%20%20%20classifier.fit(people%2C%20approved)%0A%20%20%20%20model_file%20%3D%20workdir%20%2F%20%22model.skops%22%0A%20%20%20%20data_file%20%3D%20workdir%20%2F%20%22train.parquet%22%0A%20%20%20%20skops.io.dump(classifier%2C%20model_file)%0A%20%20%20%20people.write_parquet(data_file)%0A%20%20%20%20return%20data_file%2C%20model_file%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20The%20function%0A%0A%20%20%20%20%60baml%20describe%60%20prints%20the%20signature%20of%20a%20BAML%20function.%20%60command%60%20and%20%60args%60%20start%20the%20MCP%20server.%20The%20model%0A%20%20%20%20does%20not%20appear%20in%20the%20signature%2C%20because%20the%20default%20is%20the%20model%20of%20the%20BAML%20file.%20Pass%20%60model%60%20to%20choose%0A%20%20%20%20another%20one.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo%2C%20run_baml)%3A%0A%20%20%20%20mo.md(f%22%60%60%60baml%5Cn%7Brun_baml('describe'%2C%20'explain_with_mcp'%2C%20'--budget'%2C%20'30').split('%2F%2F%23')%5B0%5D.strip()%7D%5Cn%60%60%60%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Test%20the%20loop%20with%20no%20key%0A%0A%20%20%20%20The%20BAML%20test%20%60explain_with_tools%60%20runs%20the%20same%20loop%20as%20the%20agent%2C%20but%20with%20a%20scripted%20model%20and%20a%20fake%20tool.%0A%20%20%20%20The%20scripted%20model%20asks%20for%20the%20tool%20%60get_model_summary%60%20in%20its%20first%20turn%20and%20writes%20the%20report%20in%20its%20second%0A%20%20%20%20turn.%20The%20test%20checks%20that%20the%20loop%20ran%20the%20tool%2C%20that%20%60tool_calls%60%20names%20it%2C%20and%20that%20the%20usage%20adds%20up.%20The%0A%20%20%20%20test%20sends%20no%20request%2C%20so%20it%20needs%20no%20key.%20Use%20it%20after%20each%20change%20of%20the%20prompts%20or%20of%20the%20loop.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo%2C%20run_baml)%3A%0A%20%20%20%20mo.md(f%22%60%60%60text%5Cn%7Brun_baml('test'%2C%20'-i'%2C%20'root%3A%3Aexplain_with_tools')%7D%5Cn%60%60%60%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Two%20setup%20problems%20and%20their%20fixes%0A%0A%20%20%20%20Both%20problems%20show%20in%20the%20first%20seconds%2C%20and%20neither%20costs%20money.%20The%20agent%20starts%20the%20server%20before%20it%20sends%20the%0A%20%20%20%20first%20request%2C%20so%20a%20problem%20with%20the%20key%20shows%20after%20the%20server%20started.%0A%0A%20%20%20%201.%20**A%20missing%20key.**%20The%20error%20says%20which%20variable%20the%20provider%20needs.%20Set%20%60OPENROUTER_API_KEY%60%2C%20or%20pass%20a%0A%20%20%20%20%20%20%20%60model%60%20with%20another%20provider.%0A%20%20%20%202.%20**A%20server%20that%20does%20not%20start.**%20The%20error%20names%20the%20command.%20Give%20the%20path%20of%20%60ebaml-mcp%60%20in%20%60command%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Path%2C%20data_file%2C%20mo%2C%20model_file%2C%20orjson%2C%20run_baml%2C%20sys)%3A%0A%20%20%20%20%23%20the%20ebaml-mcp%20script%20that%20%60pip%20install%20%22ebaml%5Bmcp%5D%22%60%20puts%20next%20to%20the%20Python%20of%20this%20notebook%0A%20%20%20%20server_command%20%3D%20str(Path(sys.executable).parent%20%2F%20%22ebaml-mcp%22)%0A%20%20%20%20server_arguments%20%3D%20%5B%22--model%22%2C%20str(model_file)%2C%20%22--data%22%2C%20str(data_file)%5D%0A%0A%20%20%20%20def%20explain_expression(%0A%20%20%20%20%20%20%20%20question%3A%20str%2C%0A%20%20%20%20%20%20%20%20command%3A%20str%2C%0A%20%20%20%20%20%20%20%20arguments%3A%20list%5Bstr%5D%2C%0A%20%20%20%20%20%20%20%20model_literal%3A%20str%20%3D%20%22null%22%2C%0A%20%20%20%20%20%20%20%20max_steps%3A%20int%20%3D%2012%2C%0A%20%20%20%20)%20-%3E%20str%3A%0A%20%20%20%20%20%20%20%20%22%22%22Write%20the%20BAML%20call%20of%20the%20agent.%20JSON%20text%20is%20a%20valid%20BAML%20string%20or%20list%20literal.%0A%0A%20%20%20%20%20%20%20%20Returns%0A%20%20%20%20%20%20%20%20-------%0A%20%20%20%20%20%20%20%20str%0A%20%20%20%20%20%20%20%20%20%20%20%20The%20expression%20for%20%60%60baml%20run%20-e%60%60.%0A%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22root.explain_with_mcp(%7Borjson.dumps(question).decode()%7D%2C%20%7Borjson.dumps(command).decode()%7D%2C%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Borjson.dumps(arguments).decode()%7D%2C%20model%20%3D%20%7Bmodel_literal%7D%2C%20max_steps%20%3D%20%7Bmax_steps%7D)%22%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20missing_key_output%20%3D%20run_baml(%0A%20%20%20%20%20%20%20%20%22run%22%2C%0A%20%20%20%20%20%20%20%20%22-e%22%2C%0A%20%20%20%20%20%20%20%20explain_expression(%22Which%20features%20matter%3F%22%2C%20server_command%2C%20server_arguments)%2C%0A%20%20%20%20%20%20%20%20drop_key%3DTrue%2C%0A%20%20%20%20)%0A%20%20%20%20missing_server_output%20%3D%20run_baml(%0A%20%20%20%20%20%20%20%20%22run%22%2C%0A%20%20%20%20%20%20%20%20%22-e%22%2C%0A%20%20%20%20%20%20%20%20explain_expression(%22Which%20features%20matter%3F%22%2C%20%22no-such-server%22%2C%20%5B%5D)%2C%0A%20%20%20%20)%0A%20%20%20%20missing_key_message%20%3D%20missing_key_output.splitlines()%5B-1%5D%0A%20%20%20%20missing_server_message%20%3D%20missing_server_output.splitlines()%5B-1%5D%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20f%22**Missing%20key**%5Cn%5Cn%60%60%60text%5Cn%7Bmissing_key_message%7D%5Cn%60%60%60%5Cn%5Cn%22%0A%20%20%20%20%20%20%20%20f%22**Server%20that%20does%20not%20start**%5Cn%5Cn%60%60%60text%5Cn%7Bmissing_server_message%7D%5Cn%60%60%60%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%20explain_expression%2C%20server_arguments%2C%20server_command%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Run%20the%20agent%0A%0A%20%20%20%20The%20last%20cells%20send%20**real%20requests**.%20The%20agent%20makes%20one%20request%20for%20each%20step%2C%20and%20a%20step%20is%20one%20round%20of%20tool%0A%20%20%20%20calls.%20A%20report%20usually%20takes%20three%20to%20six%20requests.%20The%20default%20model%20is%20the%20free%20model%20of%20OpenRouter%2C%20so%20the%0A%20%20%20%20cost%20is%20zero%2C%20but%20the%20free%20model%20can%20be%20slow%20or%20rate%20limited.%0A%0A%20%20%20%20Type%20a%20question.%20To%20use%20another%20model%2C%20type%20%60PROVIDER%2FMODEL%60%20(for%20example%20%60openai%2Fgpt-4o-mini%60)%2C%20as%20the%20page%0A%20%20%20%20%22Choosing%20the%20model%22%20explains.%20Leave%20the%20field%20empty%20for%20the%20default.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo%2C%20os)%3A%0A%20%20%20%20key_from_env%20%3D%20bool(os.environ.get(%22OPENROUTER_API_KEY%22))%0A%20%20%20%20api_key_input%20%3D%20None%20if%20key_from_env%20else%20mo.ui.text(kind%3D%22password%22%2C%20label%3D%22OpenRouter%20API%20key%22%2C%20full_width%3DTrue)%0A%20%20%20%20key_status%20%3D%20(%0A%20%20%20%20%20%20%20%20mo.callout(%22Loaded%20%60OPENROUTER_API_KEY%60%20from%20the%20environment.%22%2C%20kind%3D%22success%22)%0A%20%20%20%20%20%20%20%20if%20key_from_env%0A%20%20%20%20%20%20%20%20else%20mo.vstack(%5Bmo.callout(%22%60OPENROUTER_API_KEY%60%20is%20not%20set%20in%20the%20environment.%22%2C%20kind%3D%22warn%22)%2C%20api_key_input%5D)%0A%20%20%20%20)%0A%20%20%20%20key_status%0A%20%20%20%20return%20(api_key_input%2C)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20question_input%20%3D%20mo.ui.text_area(%0A%20%20%20%20%20%20%20%20value%3D%22Which%20features%20drive%20the%20model%2C%20and%20is%20any%20effect%20surprising%3F%22%2C%0A%20%20%20%20%20%20%20%20label%3D%22Question%22%2C%0A%20%20%20%20%20%20%20%20full_width%3DTrue%2C%0A%20%20%20%20)%0A%20%20%20%20model_input%20%3D%20mo.ui.text(placeholder%3D%22openai%2Fgpt-4o-mini%22%2C%20label%3D%22Model%20(optional)%22)%0A%20%20%20%20step_slider%20%3D%20mo.ui.slider(3%2C%2020%2C%20value%3D12%2C%20label%3D%22Most%20steps%22)%0A%20%20%20%20agent_button%20%3D%20mo.ui.run_button(label%3D%22Run%20the%20agent%22)%0A%20%20%20%20mo.vstack(%5Bquestion_input%2C%20model_input%2C%20step_slider%2C%20agent_button%5D)%0A%20%20%20%20return%20agent_button%2C%20model_input%2C%20question_input%2C%20step_slider%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20agent_button%2C%0A%20%20%20%20api_key_input%2C%0A%20%20%20%20explain_expression%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20model_input%2C%0A%20%20%20%20orjson%2C%0A%20%20%20%20os%2C%0A%20%20%20%20question_input%2C%0A%20%20%20%20run_baml%2C%0A%20%20%20%20server_arguments%2C%0A%20%20%20%20server_command%2C%0A%20%20%20%20step_slider%2C%0A)%3A%0A%20%20%20%20mo.stop(not%20agent_button.value%2C%20mo.md(%22*Press%20the%20button%20to%20send%20the%20requests.*%22))%0A%20%20%20%20typed_key%20%3D%20api_key_input.value%20if%20api_key_input%20is%20not%20None%20else%20%22%22%0A%20%20%20%20if%20typed_key%3A%0A%20%20%20%20%20%20%20%20%23%20a%20typed%20key%20lives%20in%20this%20process%20only%2C%20and%20the%20baml%20child%20process%20inherits%20it%0A%20%20%20%20%20%20%20%20os.environ%5B%22OPENROUTER_API_KEY%22%5D%20%3D%20typed_key%0A%20%20%20%20mo.stop(not%20os.environ.get(%22OPENROUTER_API_KEY%22)%2C%20mo.callout(%22Set%20%60OPENROUTER_API_KEY%60%20first.%22%2C%20kind%3D%22warn%22))%0A%20%20%20%20chosen_model%20%3D%20model_input.value.strip()%0A%20%20%20%20provider%2C%20_%2C%20model_name%20%3D%20chosen_model.partition(%22%2F%22)%0A%20%20%20%20model_literal%20%3D%20(%0A%20%20%20%20%20%20%20%20f%22ModelSpec%20%7B%7B%20provider%3A%20%7Borjson.dumps(provider).decode()%7D%2C%20model%3A%20%7Borjson.dumps(model_name).decode()%7D%20%7D%7D%22%0A%20%20%20%20%20%20%20%20if%20model_name%0A%20%20%20%20%20%20%20%20else%20%22null%22%0A%20%20%20%20)%0A%20%20%20%20agent_output%20%3D%20run_baml(%0A%20%20%20%20%20%20%20%20%22run%22%2C%0A%20%20%20%20%20%20%20%20%22-e%22%2C%0A%20%20%20%20%20%20%20%20explain_expression(%0A%20%20%20%20%20%20%20%20%20%20%20%20question_input.value%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20server_command%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20server_arguments%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20model_literal%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20max_steps%3Dstep_slider.value%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%22--output-format%22%2C%0A%20%20%20%20%20%20%20%20%22json%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%20(agent_output%2C)%0A%0A%0A%40app.cell%0Adef%20_(agent_output%2C%20mo%2C%20orjson)%3A%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20agent_result%20%3D%20orjson.loads(agent_output%5Bagent_output.index(%22%7B%22)%20%3A%5D)%0A%20%20%20%20except%20(ValueError%2C%20orjson.JSONDecodeError)%3A%0A%20%20%20%20%20%20%20%20mo.stop(%0A%20%20%20%20%20%20%20%20%20%20%20%20predicate%3DTrue%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20output%3Dmo.callout(mo.md(f%22The%20agent%20failed%3A%5Cn%5Cn%60%60%60text%5Cn%7Bagent_output%7D%5Cn%60%60%60%22)%2C%20kind%3D%22danger%22)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20mo.vstack(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(%22%23%23%23%20Tools%20that%20the%20model%20called%2C%20in%20order%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(%22%5Cn%22.join(f%22%7Bstep%7D.%20%60%7Bname%7D%60%22%20for%20step%2C%20name%20in%20enumerate(agent_result%5B%22tool_calls%22%5D%2C%20start%3D1)))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22The%20model%20sent%20**%7Bagent_result%5B'usage'%5D%5B'input_tokens'%5D%7D**%20input%20tokens%20and%20wrote%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22**%7Bagent_result%5B'usage'%5D%5B'output_tokens'%5D%7D**%20output%20tokens.%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(%22%23%23%23%20Report%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(agent_result%5B%22report%22%5D)%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Read%20the%20result%0A%0A%20%20%20%20-%20%60tool_calls%60%20shows%20the%20evidence.%20A%20good%20run%20calls%20%60get_model_summary%60%20and%20%60list_features%60%20first%2C%20then%0A%20%20%20%20%20%20%60get_graph%60%20for%20the%20features%20that%20matter.%20A%20report%20with%20no%20tool%20call%20rests%20on%20nothing%2C%20so%20do%20not%20trust%20it.%0A%20%20%20%20-%20Compare%20each%20number%20of%20the%20report%20with%20the%20output%20of%20the%20tool%20in%20the%0A%20%20%20%20%20%20%5BMCP%20server%20notebook%5D(mcp-notebook.md).%20The%20tools%20are%20exact%2C%20and%20the%20model%20only%20puts%20them%20in%20words.%0A%20%20%20%20-%20The%20same%20loop%20runs%20on%20tools%20that%20you%20supply.%20%60explain_with_tools%60%20takes%20a%20list%20of%20%60ai.tools.Tool%60%2C%20so%20a%20later%0A%20%20%20%20%20%20server%20with%20edit%20tools%20needs%20no%20change%20of%20the%20loop.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
8ba12ca71460777a209135c5e590b074