Client Retention Risk and Fee Contraction Scoring Script
Build a PySpark feature engineering and scoring pipeline to detect client attrition, delivery friction, and revenue contraction across major accounts.
Use this template when setting up proactive account health monitoring for professional services firms. It outputs a distributed batch script that scores churn propensity and engagement decay across high-value client accounts.
Role: Lead Revenue Analytics Engineer for global management consulting partnerships.
Context
- Account classification structure: {{client_tier_definitions}}
- CRM and delivery telemetry table: {{crm_telemetry_table}}
- Account receivables and billing records: {{invoice_aging_data}}
- Multi-factor risk weights: {{risk_weighting_matrix}}
- Historical recency decay: {{scoring_decay_rate}}
- Data export destination: {{export_format}}
Task
Build a scalable PySpark feature engineering and risk scoring batch script that analyzes delivery friction, invoice disputes, and communication drops to quantify account churn propensity across {{client_tier_definitions}}.
Method
- Ingest stakeholder interaction logs and project status telemetry from {{crm_telemetry_table}}.
- Ingest payment timeliness, disputed line items, and invoice write-downs from {{invoice_aging_data}}.
- Construct rolling temporal features measuring engagement cadence drops, applying {{scoring_decay_rate}} to weigh recent quarters more heavily.
- Engineer invoice friction metrics tracking average days sales outstanding (DSO) drift and unapproved fee resistance.
- Combine delivery, interaction, and billing features using the normalized parameters in {{risk_weighting_matrix}}.
- Compute a composite Account Contraction Risk Score (0-100) and categorize accounts into actionable intervention tiers.
- Generate key risk driver attributions for each at-risk account to explain the underlying root causes to client partners.
- Write the scored output dataset and metadata snapshot to {{export_format}} with partitioned parquet or delta layout.
Constraints
- MUST use broadcast joins for dimension lookup tables to minimize cluster shuffle overhead.
- MUST NOT drop accounts with sparse interaction history; treat low telemetry as a primary risk indicator.
- The scoring script must be strictly idempotent to allow backfilling historical risk snapshots.
- Output schemas must enforce non-nullable constraints on primary account keys and composite scores.
Output format
- Part 1: Feature engineering architecture and weight dictionary specification (150-200 words)
- Part 2: Complete PySpark batch scoring script including schema definitions and dataframe transformations
- Part 3: Data validation assertions and target storage partition layout (100-150 words)
Self-review
- Ensure the scoring formula normalizes output strictly within the 0 to 100 range.
- Confirm all 6 context variables are actively utilized within the PySpark script logic.
- Verify that sparse data edge cases do not result in null or NaN score generations.
Explicit role, a named task, and discrete steps the model can follow.
Background, inputs and variables the model needs before it starts.
Hard boundaries — what the model must and must not do.
A named, field-level shape for the response.
Ordered work items that force analysis before an answer.
Length and structure that travel across frontier models.
Signal density — instruction weight without padding.
Documented variables so the scaffold adapts to new inputs.
Quality bar, assumptions and behaviour when inputs are thin.
How much real usage the template has behind it.