| name | traigent-debugging |
| description | Debug and troubleshoot Traigent optimization issues. Use when encountering CostLimitExceeded, ConfigurationError, OptimizationStateError, ModuleNotFoundError, or when optimization produces unexpected results. Covers mock mode, logging configuration, and common error resolution. |
| license | AGPL-3.0-only OR LicenseRef-Traigent-Commercial |
| metadata | {"author":"Traigent","version":"1.0"} |
Debugging and Troubleshooting Traigent
When to Use
Use this skill when:
- An optimization run fails or produces unexpected results
- You encounter a Traigent exception (CostLimitExceeded, ConfigurationError, etc.)
- You need to test without real API keys (mock mode)
- You want to increase logging verbosity for diagnosis
- Your function works standalone but fails during optimization
- Import errors occur due to missing optional dependencies
Quick Diagnostic
Enable detailed logging to see what Traigent is doing at each step:
export TRAIGENT_LOG_LEVEL=DEBUG
export TRAIGENT_DEBUG=1
Then run your optimization. Debug output includes:
- Configuration sampling decisions
- Trial execution start/stop/status
- Metric extraction and scoring
- Cost tracking per trial
- Backend communication (if using hybrid portal tracking)
Common Errors
ConfigurationError
When raised: Invalid or malformed configuration values, unsupported features, missing required configuration.
traigent.utils.exceptions.ConfigurationError: Invalid configuration_space: 'model' values must be a list
Common causes and fixes:
@traigent.optimize(
configuration_space={"model": "gpt-4o-mini"},
)
@traigent.optimize(
configuration_space={"model": ["gpt-4o-mini"]},
)
@traigent.optimize(
configuration_space={},
)
@traigent.optimize(
configuration_space={"temperature": [0.0, 0.5, 1.0]},
)
Set TRAIGENT_DEBUG=1 to see the full traceback instead of the clean error message.
CostLimitExceeded
When raised: Cost approval is declined before the first trial (pre-run only). Has accumulated and limit attributes. CostLimitExceeded is a subclass of OptimizationError.
Mid-run budget overruns do not raise this exception. When accumulated spend hits the limit during a run, the run stops gracefully and returns a partial OptimizationResult — check result.stop_reason == "cost_limit".
traigent.utils.exceptions.CostLimitExceeded: Cost limit exceeded: $0.52 >= $0.50 USD
Fixes:
@traigent.optimize(
configuration_space={"model": ["gpt-4o-mini", "gpt-4o"]},
cost_limit=2.0,
)
@traigent.optimize(
configuration_space={"model": ["gpt-4o-mini"]},
)
@traigent.optimize(
configuration_space={"model": ["gpt-4o-mini", "gpt-4o"]},
max_trials=5,
)
Handling the pre-run exception:
from traigent.utils.exceptions import CostLimitExceeded
try:
results = await func.optimize()
except CostLimitExceeded as e:
print(f"Cost approval declined: ${e.accumulated:.2f} of ${e.limit:.2f} limit")
Handling a mid-run budget stop (no exception — check stop_reason):
results = await func.optimize()
if results.stop_reason == "cost_limit":
print(f"Run stopped at cost limit: ${results.total_cost:.2f} spent")
OptimizationStateError
When raised: Accessing configuration in an invalid lifecycle state.
traigent.utils.exceptions.OptimizationStateError: Cannot access config outside of optimization trial
Common causes:
@traigent.optimize(...)
def my_func(text):
config = traigent.get_config()
return config["model"]
my_func("hello")
results = my_func.optimize_sync()
my_func.apply_best_config(results)
my_func("hello")
Has current_state and expected_states attributes for diagnostics.
ProviderValidationError
When raised: @traigent.optimize never raises this automatically -- there
is no validate_providers= decorator parameter. It is only raised if your
own code calls the standalone traigent.providers.validate_providers()
helper as a pre-run check and chooses to raise on a failed result:
from traigent.providers import get_failed_providers, validate_providers
from traigent.utils.exceptions import ProviderValidationError
results = validate_providers(["gpt-4o-mini", "claude-3-haiku-20240307"])
failed = get_failed_providers(results)
if failed:
raise ProviderValidationError(f"Provider validation failed: {failed}", failed_providers=failed)
traigent.utils.exceptions.ProviderValidationError: Provider validation failed:
- openai: InvalidAPIKey
- anthropic: MissingAPIKey
Fixes:
export OPENAI_API_KEY="sk-..."
export ANTHROPIC_API_KEY="sk-ant-..."
If your own pre-run check should be skippable, gate it on
traigent.utils.env_config.skip_provider_validation() (true when
TRAIGENT_SKIP_PROVIDER_VALIDATION=true or mock mode is on) rather than
building a bespoke skip flag -- @traigent.optimize itself has no
provider-validation skip parameter.
The failed_providers attribute contains a list of (provider, error_type) tuples.
InvocationError
When raised: The decorated function raised an exception during a trial.
traigent.utils.exceptions.InvocationError: Function 'classify' failed with config {'model': 'gpt-4o'}
Has config, input_data, and original_error attributes. Check the original error:
from traigent.utils.exceptions import InvocationError
try:
results = await func.optimize()
except InvocationError as e:
print(f"Config that caused failure: {e.config}")
print(f"Original error: {e.original_error}")
EvaluationError
When raised: The evaluator function failed when scoring a trial's output.
traigent.utils.exceptions.EvaluationError: Evaluator raised exception for config {'model': 'gpt-4o'}
Check your evaluator function handles edge cases (empty output, None, unexpected formats).
FeatureNotAvailableError
When raised: A feature requires an uninstalled plugin or optional dependency.
traigent.utils.exceptions.FeatureNotAvailableError: Feature 'LangChain integration' is not available.
Requires the 'traigent-langchain' plugin. Install with: pip install traigent[integrations]
Fix: Install the indicated package.
ModuleNotFoundError (Python)
When raised: Missing optional dependencies.
ModuleNotFoundError: No module named 'langchain_openai'
Fix:
pip install traigent[integrations]
pip install traigent[all]
pip install traigent[dev]
Mock Mode
Test your optimization setup without making real API calls or connecting to the backend:
export TRAIGENT_MOCK_LLM=true
export TRAIGENT_OFFLINE_MODE=true
import os
os.environ["TRAIGENT_MOCK_LLM"] = "true"
os.environ["TRAIGENT_OFFLINE_MODE"] = "true"
import traigent
@traigent.optimize(
configuration_space={"model": ["gpt-4o-mini", "gpt-4o"], "temperature": [0.0, 0.5]},
objectives=["accuracy"],
eval_dataset="test_data.jsonl",
max_trials=5,
)
def my_func(text):
config = traigent.get_config()
return "mock response"
results = await my_func.optimize()
Mock mode is essential for:
- CI/CD pipelines
- Unit testing
- Validating configuration space setup
- Testing evaluator logic
See Mock Mode reference for details.
Troubleshooting Decision Tree
No trials ran
- Check dataset: does the file exist and contain valid JSONL?
- Check configuration space: is it non-empty with valid lists?
- Check for ConfigurationError in output
- Enable
TRAIGENT_LOG_LEVEL=DEBUG and check for early failures
All trials failed
- Check API keys: are they set and valid?
- Check
results.failed_trials for error messages:
for trial in results.failed_trials:
print(f"{trial.trial_id}: {trial.error_message}")
- Test your function standalone (outside optimization) with a sample config
- Check provider status pages for outages
Wrong results (low scores)
- Check your evaluator: does it correctly score good vs bad outputs?
- Check your dataset: are expected outputs correct?
- Check configuration space: does it include good model/parameter combinations?
- Check
results.best_metrics and compare with manual testing
- Look at individual trial scores:
for trial in results.successful_trials:
print(f"{trial.config} -> {trial.metrics}")
Cost too high
- Reduce
max_trials to limit total API calls
- Set a
cost_limit on the decorator
- Use cheaper models in the configuration space (e.g.,
gpt-4o-mini instead of gpt-4o)
- Reduce dataset size for initial exploration
- Check
results.experiment_stats.cost_per_configuration to identify expensive configs
Optimization is slow
- Check trial durations:
results.experiment_stats.average_trial_duration
- Reduce dataset size for faster feedback
- Set a
timeout on the decorator
- Use smaller models for initial exploration
- Reduce
max_trials or configuration space size
Environment Verification
Verify your Traigent installation and environment:
traigent info
traigent validate dataset.jsonl
From Python:
import traigent
print(traigent.__version__)
import os
print(f"Mock LLM: {os.getenv('TRAIGENT_MOCK_LLM', 'false')}")
print(f"Offline mode: {os.getenv('TRAIGENT_OFFLINE_MODE', 'false')}")
print(f"Log level: {os.getenv('TRAIGENT_LOG_LEVEL', 'INFO')}")
Graceful Fallback Pattern
When optimization might fail, use a try/except pattern with a known-good default:
import traigent
from traigent.utils.exceptions import TraigentError, CostLimitExceeded
DEFAULT_CONFIG = {"model": "gpt-4o-mini", "temperature": 0.0}
@traigent.optimize(
configuration_space={
"model": ["gpt-4o-mini", "gpt-4o"],
"temperature": [0.0, 0.3, 0.7],
},
objectives=["accuracy"],
eval_dataset="eval_data.jsonl",
max_trials=10,
)
def classify(text):
config = traigent.get_config()
return result
try:
results = await classify.optimize()
if results.best_score is not None and results.best_score >= 0.7:
classify.apply_best_config(results)
print(f"Applied optimized config: {results.best_config}")
else:
print(f"Score {results.best_score} too low, using default config")
except CostLimitExceeded as e:
print(f"Budget exceeded (${e.accumulated:.2f}/${e.limit:.2f}), using default config")
except TraigentError as e:
print(f"Optimization failed: {e.message}, using default config")
This pattern ensures your application remains functional even when optimization encounters problems.
Reference Files