Agent Overview#
pycsamt.agents is the AI-assisted workflow layer of pyCSAMT. It turns
common MT, AMT, and CSAMT work into composable agents: one agent loads survey
files, another performs quality control, another prepares inversion files,
another writes a report, and so on.
The agent layer is not only an LLM wrapper. Many agents have deterministic processing paths and can run without an API key. LLM support is added where it is useful: parsing natural-language requests, selecting workflow steps, generating interpretation text, and producing report narratives.
Core Ideas#
The package is built around four concepts:
Concept |
Role |
|---|---|
Abstract base class for every agent. It provides LLM access, cost tracking, JSON extraction, plotting helpers, and common validation helpers. |
|
Standard return object with |
|
Ordered workflow executor. It chains agents, maps outputs between steps, supports dry-run previews, and checkpoints workflow state. |
|
|
High-level entry point for natural-language workflow requests. It classifies the request and builds the matching agent chain. |
Imports are lazy. Importing pycsamt.agents exposes the public names
without importing optional provider libraries such as anthropic,
openai, google-generativeai, torch, or gradio until they are
actually needed.
When To Use Agents#
Use agents when you want a workflow-oriented interface rather than individual low-level function calls.
Task |
Recommended entry point |
|---|---|
Parse a plain-English request into a structured config |
|
Load EDI, AVG, or J files and inspect station completeness |
|
Run a reproducible multi-step processing chain |
|
Let pyCSAMT choose the workflow from a request |
|
Prepare inversion inputs |
|
Run AI inversion or model-zoo workflows |
|
Generate final products |
|
Installation#
The base package can be installed without LLM, GPU, or web-interface dependencies:
1pip install pycsamt
Install optional provider clients only when you plan to use them:
1pip install anthropic
2pip install openai
3pip install google-generativeai
Install optional AI or web dependencies only for those features:
1pip install torch
2pip install gradio
The Agent Result Contract#
Every agent returns pycsamt.agents.AgentResult. The object exposes
structured fields and also supports dict-like access to data.
1from pycsamt.agents import MTLoaderAgent
2
3result = MTLoaderAgent().execute({
4 "path": "/data/WILLY_EDIs",
5})
6
7if result:
8 print(result.status)
9 print(result.summary)
10 print(result["sites"])
11 print(result.get("station_names", []))
12 print(result.cost_estimate_usd)
13else:
14 print(result.error)
15 print(result.error_fix_hint)
Important fields:
Field |
Meaning |
|---|---|
|
|
|
Short human-readable description of the run. |
|
Agent-specific structured outputs, such as |
|
Non-fatal issues encountered during execution. |
|
Optional LLM-generated interpretation text. |
|
Wall-clock runtime measured by the agent. |
|
Estimated LLM cost for that agent run. |
|
Failure details and suggested remediation. |
No-LLM Workflow Preview#
No-LLM mode is useful for CI, reproducible examples, and workflow previews. The context agent can fall back to rule-based parsing, and the coordinator can preview a plan without executing expensive steps.
1from pycsamt.agents import ContextInputAgent, MTLoaderAgent, AgentCoordinator
2
3context = ContextInputAgent()
4loader = MTLoaderAgent()
5
6coord = AgentCoordinator("preview_qc")
7coord.add_step(
8 "parse",
9 context,
10 description="Parse natural-language request into a config",
11)
12coord.add_step(
13 "load",
14 loader,
15 input_fn=lambda results: {
16 "path": (results["parse"].get("config") or {}).get("data_path", "")
17 },
18 description="Load survey files and create a station QC table",
19)
20
21result = coord.execute(
22 {"request": "Load /data/WILLY_EDIs, QC them, period 0.001 to 10 s"},
23 dry_run=True,
24)
25
26print(result.status)
27print(result["plan"])
28print(result.cost_estimate_usd)
Dry-run results are still AgentResult objects. The
data payload contains a formatted plan and a list of steps.
Direct Agent Execution#
Agents can also be used individually. This is the clearest pattern when you already know which operation you want.
1from pycsamt.agents import MTLoaderAgent, DataQCAgent
2
3load = MTLoaderAgent().execute({
4 "path": "/data/WILLY_EDIs",
5})
6
7if load.status == "success":
8 qc = DataQCAgent().execute({
9 "sites": load["sites"],
10 "output_dir": "/out/willy_qc",
11 })
12 print(qc.summary)
13 print(qc.warnings)
This style is explicit and easy to debug. It is also useful in notebooks
where intermediate objects such as Sites should be inspected between
steps.
Coordinated Workflows#
Use pycsamt.agents.AgentCoordinator when several agents should run as
a named workflow. Each step can receive either the original configuration or
a transformed view of earlier step results.
1from pycsamt.agents import (
2 AgentCoordinator,
3 MTLoaderAgent,
4 DataQCAgent,
5 StaticShiftAgent,
6 ReportAgent,
7)
8
9coord = AgentCoordinator("static_shift_report")
10coord.add_step("load", MTLoaderAgent(), description="Load survey files")
11coord.add_step(
12 "qc",
13 DataQCAgent(),
14 input_fn=lambda r: {
15 "sites": r["load"]["sites"],
16 "output_dir": "/out/willy/qc",
17 },
18 description="Run quality control",
19)
20coord.add_step(
21 "static_shift",
22 StaticShiftAgent(),
23 input_fn=lambda r: {
24 "sites": r["load"]["sites"],
25 "qc_table": r["qc"].get("qc_table"),
26 "output_dir": "/out/willy/static_shift",
27 },
28 description="Detect and correct static shift",
29)
30coord.add_step(
31 "report",
32 ReportAgent(),
33 input_fn=lambda r: {
34 "workflow_results": r,
35 "output_dir": "/out/willy/report",
36 },
37 description="Assemble the workflow report",
38)
39
40result = coord.execute({
41 "path": "/data/WILLY_EDIs",
42})
43
44print(result.summary)
45print(result.cost_estimate_usd)
Natural-Language Orchestration#
Use pycsamt.agents.WorkflowOrchestratorAgent when a user request
should determine the workflow. The orchestrator can classify requests such as
quality control, phase-tensor analysis, Occam2D preparation, ModEM
preparation, AI inversion, 2-D inversion, 3-D inversion, ensemble inversion,
joint inversion, and full workflows.
1from pycsamt.agents import WorkflowOrchestratorAgent
2
3agent = WorkflowOrchestratorAgent()
4
5result = agent.execute({
6 "request": "Run phase tensor analysis and generate a report",
7 "data_path": "/data/WILLY_EDIs",
8 "output_dir": "/out/willy_phase",
9 "dry_run": True,
10})
11
12print(result["workflow_type"])
13print(result["steps"])
Switch dry_run to False or omit it when the workflow should actually
run.
LLM-Assisted Interpretation#
Agents inherit provider settings from pycsamt.agents.AGENT_CONFIG
unless an explicit per-agent key is supplied.
1from pycsamt.agents import configure_agents
2from pycsamt.agents import PhaseAnalysisAgent
3
4configure_agents(
5 provider="claude",
6 api_key="sk-ant-...",
7 model="claude-sonnet-4-6",
8)
9
10result = PhaseAnalysisAgent().execute({
11 "path": "/data/WILLY_EDIs",
12 "output_dir": "/out/willy_phase",
13})
14
15print(result.summary)
16print(result.llm_interpretation)
17print(result.cost_estimate_usd)
For provider selection, environment variables, budgets, and custom pricing, see Agent And LLM Configuration.
AI And Model-Zoo Entry Points#
AI inversion agents are available as first-class workflow steps and direct interfaces. They are intended for workflows where trained models or model-zoo checkpoints are available.
1from pycsamt.agents import AIInversionAgent, ModelZooAgent
2
3zoo = ModelZooAgent()
4models = zoo.execute({"action": "list"})
5print(models["models"])
6
7inverter = AIInversionAgent.from_pretrained("mt1d-resnet-5layer-v1")
8result = inverter.execute({
9 "path": "/data/WILLY_EDIs",
10 "output_dir": "/out/willy_ai",
11})
12
13print(result["rms_global"])
Use Inv2DAgent, Inv3DAgent, EnsembleAgent, and
JointInversionAgent for specialized deep-learning workflows.
CLI And Web Interface#
The agent package also exposes a module CLI:
1python -m pycsamt.agents preview "Load /data/WILLY EDIs, QC, PT analysis"
2python -m pycsamt.agents list
3python -m pycsamt.agents pricing
4python -m pycsamt.agents zoo
5python -m pycsamt.agents web --port=7860
The web interface requires gradio and can also be started from Python:
1from pycsamt.agents.web import launch
2
3launch()
Outputs And Reproducibility#
Agent workflows are designed to be inspectable. A robust workflow should:
Keep the original input path and output directory in the workflow config.
Preserve intermediate
AgentResultobjects while debugging.Use
dry_run=Truebefore expensive or file-writing runs.Inspect
warningsanderror_fix_hintbefore trusting final products.Track
cost_estimate_usdper agent and session-wide spend throughAGENT_CONFIG.Use
CodeGenerationAgentwhen a workflow should be converted into a reproducible standalone Python script.
Where To Go Next#
Page |
Use it for |
|---|---|
Provider setup, key resolution, budgets, and pricing. |
|
Choosing the right agent for each task. |
|
Building explicit multi-agent workflows. |
|
Natural-language workflow classification and execution. |
|
../api/agents |
Generated API reference for |