Multi-Task Attention-based Transformer for Sequential API Recommendation
Project description
Context Engineer
A reproducible research package for the paper:
Rethink Context Engineering Using an Attention-based Architecture Yiqiao Yin — University of Chicago Booth School of Business / Columbia University
This package implements a multi-task attention-based transformer for sequential API recommendation. It simultaneously predicts the next API action, session goal, and session boundary from user interaction sequences modeled as Markov chains.
Key Results (from the paper)
| Metric | Value |
|---|---|
| API Prediction Accuracy (Top-1) | 79.83% |
| Top-5 Hit Rate | 99.97% |
| Top-10 Hit Rate | 100.00% |
| Goal Prediction Accuracy | 81.6% |
| Session End Accuracy | 99.3% |
| Improvement over Markov baseline | +432% |
Installation
pip install context-engineer
For visualization support:
pip install context-engineer[viz]
Quick Start
Reproduce the Full Experiment
from context_engineer import run_pipeline
# Run with paper defaults: 2000 users, 100 APIs, 60 epochs
results = run_pipeline()
# Access results
model = results["model"] # Trained PyTorch model
metrics = results["metrics"] # Evaluation metrics dict
history = results["training_history"]
Generate Simulation Data Only
from context_engineer import simulate_multitask_markov_data
# Generate user session logs with 4 persona types
sequences, goals = simulate_multitask_markov_data(
num_users=500,
num_apis=100,
clicks_per_user=10,
)
# sequences: list of API call sequences per user
# goals: list of session goal labels (0-3)
Train with Custom Parameters
from context_engineer import run_pipeline
results = run_pipeline(
num_users=1000,
num_epochs=30,
embed_dim=64,
num_heads=4,
learning_rate=0.001,
seed=123,
)
Command-Line Interface
# Run the full pipeline
context-engineer run
# Custom parameters
context-engineer run --num-users 1000 --epochs 30 --seed 123
# Generate data only (outputs JSON)
context-engineer generate --num-users 500 --output data.json
Architecture
The model uses a shared transformer encoder with three task-specific prediction heads:
Input Sequence [API_1, API_2, ..., API_t]
|
Embedding + Positional Encoding
|
Transformer Encoder (3 layers, 8 heads)
|
Shared Feature Representation
/|\
/ | \
/ | \
v v v
Next Goal Session
API Head End Head
Head
- Primary task: Next API prediction (100-class classification)
- Auxiliary task 1: Session goal classification (4 classes)
- Auxiliary task 2: Session end detection (binary)
- Loss: Weighted combination (1.0 / 0.3 / 0.2)
Dataset Design
The simulator generates realistic API usage patterns with:
- 100 APIs across 10 functional categories (Auth, User Mgmt, Data Input, Data Processing, ML Training, ML Prediction, Basic Viz, Advanced Viz, Export, Admin)
- 4 user personas: Data Scientists (80% adherence), Business Analysts (90%), Developers (60%), Power Users (70%)
- 4 session goals: ML Pipeline, Data Analysis, User Management, Quick Visualization
- Markov chain transitions with high-probability workflow patterns (75-90%)
Package Structure
src/context_engineer/
__init__.py # Public API
data.py # Data simulation & dataset classes
model.py # Transformer model architecture
train.py # Training, evaluation, inference
pipeline.py # End-to-end pipeline
cli.py # Command-line interface
API Reference
Core Functions
| Function | Description |
|---|---|
run_pipeline(**kwargs) |
Run the full experiment end-to-end |
simulate_multitask_markov_data(...) |
Generate simulated user sessions |
create_multitask_training_pairs(...) |
Convert sequences to supervised pairs |
train_multitask_model(...) |
Train with early stopping and cosine annealing |
evaluate_multitask_model(...) |
Evaluate with accuracy, MRR, Hit Rate@K |
set_random_seeds(seed) |
Set seeds for reproducibility |
Core Classes
| Class | Description |
|---|---|
MultiTaskMarkovChainAPISimulator |
Configurable Markov chain data generator |
MultiTaskMarkovAPIRecommender |
Multi-task transformer model |
MultiTaskMarkovDataset |
PyTorch Dataset for training data |
Citation
If you use this package in your research, please cite:
@article{yin2025rethink,
title={Rethink Context Engineering Using an Attention-based Architecture},
author={Yin, Yiqiao},
year={2025}
}
License
MIT License. See LICENSE for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file context_engineer-0.2.0.tar.gz.
File metadata
- Download URL: context_engineer-0.2.0.tar.gz
- Upload date:
- Size: 4.5 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d3489f462debff43814d2afbed9597dbc0fff717f0f5ea33969e7c79bccf359b
|
|
| MD5 |
71e2f4e337f81882a4abb3d486634cb9
|
|
| BLAKE2b-256 |
9ed8acc70cb67d26d4fd092af61aba33a5a072eccb9eac73231987d7e12e688a
|
File details
Details for the file context_engineer-0.2.0-py3-none-any.whl.
File metadata
- Download URL: context_engineer-0.2.0-py3-none-any.whl
- Upload date:
- Size: 27.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
34f2409c0fb323b2a8f2b939f404a2b054f45f43e7c37aa417aa550a53eda3d8
|
|
| MD5 |
7c49564d478b1a3b1c57bfc795d19d62
|
|
| BLAKE2b-256 |
107e78f5e06b10be992a18c59385add26daafa49f733c83ce07ce370c26b9e53
|