Programming Neam
📖 12 min read

Case Study: Secure Data Science Pipeline #


The Challenge #

A mid-sized financial services company wants to predict customer churn using machine learning. Their data science team has built accurate models before -- but this time, the stakes are higher. New regulatory requirements demand full compliance with the OWASP Agentic Security Initiative (ASI01–10), and because the system will process EU customer data, the EU AI Act requires transparency documentation, risk classification, and human oversight for any AI system that influences customer-facing decisions.

The pipeline must:

Traditionally, meeting these requirements means stitching together a dozen separate tools: an ML framework, a security scanner, a cost tracker, a compliance document generator, an approval workflow, and more. In Neam v1.0, every one of these concerns is a compiled declaration -- checked at compile time, enforced at runtime, and auditable by design.


Architecture #

The pipeline uses five interlocking layers, each expressed as Neam declarations:

Security Layer #

Cost Layer #

Cloud Layer #

Compliance Layer #

Agent Layer #


The Complete Program #

Here is the complete, verified Neam program that implements the secure data science pipeline. Every security control, cost limit, and compliance requirement is expressed as a declaration that the compiler checks before a single line of agent code runs:

neam
budget B { cost: 500.00, tokens: 2000000 }

// OWASP Security layer
goal_integrity ChurnGoal {
    declared_objectives: ["predict churn", "identify drivers", "deploy safely"],
    verification: { method: "semantic_similarity", threshold: 0.75 }
}
circuit_breaker SafeCB { failure_threshold: 3, half_open_timeout: "30s" }
human_gate DeployGate { approve_before: ["deploy"], workflow: { timeout: "15m" } }
agent_attestation HealthCheck { attest_interval: "5m" }

// Cloud layer
gateway API { auth: { method: "oauth2" }, routes: { health: "/health" } }
model_router Router { strategy: "cost_optimized", routes: { simple: "haiku", complex: "opus" } }

// AIBOM for EU AI Act compliance
aibom_config BOM { format: "cyclonedx", auto_generate: true, eu_ai_act: { risk_classification: "limited" } }

// Data science agents
datascientist agent ChurnDS { provider: "openai", model: "gpt-4o", budget: B }
ds_status(ChurnDS);

causal agent WhyCausal { provider: "openai", model: "o3-mini", budget: B }
causal_status(WhyCausal);

// Sentinel watches everything
securitysentinel agent Sentinel {
    provider: "openai", model: "gpt-4o", budget: B,
    monitors: { goal_integrity: { check: "per_phase" }, behavioral_anomaly: { sigma: 3.0 } },
    actions: { on_critical: "kill_switch" }
}

// Cost management
costguardian agent CostOps {
    provider: "ollama", model: "llama3:8b", budget: B,
    tracking: { per_agent: true, per_phase: true },
    alerts: { budget_warning: 0.75, budget_critical: 0.90 }
}

// Orchestrate
infrastructure_profile Infra { data_warehouse: { platform: "postgres" } }
dio agent SecureDIO {
    mode: "config",
    task: "Predict churn with OWASP-compliant security",
    infrastructure: Infra,
    provider: "openai", model: "gpt-4o", budget: B
}
print(dio_solve(SecureDIO, "full_system"));

Let's walk through what happens when this program compiles and runs:

  1. Compile time: The compiler validates that every agent references a valid budget, that the goal_integrity declaration lists at least one objective, that the circuit_breaker thresholds are positive integers, and that the AIBOM format is a recognized standard. If any declaration is malformed, compilation fails with a clear error -- before any LLM call is ever made.
  2. Runtime -- Security setup: The SecuritySentinel agent (Sentinel) registers itself as a monitor for all other agents. The goal integrity checker loads the declared objectives and initializes the semantic similarity engine. The circuit breaker starts in "closed" state.
  3. Runtime -- Data science: The DIO orchestrator activates the DataScientist agent to ingest data, engineer features, and train the churn model. Every LLM call is routed through the Model Router, which selects the cheapest adequate model. The CostGuardian tracks every token spent.
  4. Runtime -- Causal analysis: The Causal agent runs counterfactual analysis to identify why customers churn, not just which ones will. This phase uses the more capable o3-mini model for its reasoning depth.
  5. Runtime -- Deployment gate: When the pipeline reaches the deployment phase, the Human Gate activates. A notification is sent to the approval workflow, and execution pauses until a human approves (or the 15-minute timeout expires, aborting the deployment).
  6. Runtime -- Continuous monitoring: Throughout all phases, the Sentinel checks every agent action against the declared objectives. If the DataScientist agent suddenly starts generating marketing copy (goal drift), the Sentinel flags a violation. If 3 consecutive LLM calls fail, the circuit breaker opens, preventing cascading failures.

Evaluation #

Before deploying the pipeline, we run it through Neam-Gym's security evaluation mode. This uses red-team prompts to test whether the pipeline's OWASP controls actually hold under adversarial conditions:

neam
gym_evaluator ChurnEval {
    mode: "security",
    agent: "./build/churn_pipeline.neamb",
    dataset: "./eval/red_team_prompts.jsonl",
    graders: { primary: "owasp_compliance" },
    thresholds: { compliance_rate: 1.0 }
}

The evaluator compiles the pipeline bytecode, then runs each prompt from the red-team dataset against it. The owasp_compliance grader checks that:

The threshold is set to 1.0 -- 100% compliance. Any failure means the pipeline does not ship. This is not a best-effort check; it is a hard gate enforced by the build system.


Key Takeaways #

For a deep dive into the gateway, model router, and cloud deployment architecture referenced in this case study, see Chapter 31: Cloud Agentic Stack. For the full OWASP security model, see Chapter 29: OWASP Security for AI Agents. For agent evaluation with Neam-Gym, see Chapter 30: Agent Evaluation with Neam-Gym.

Start typing to search...