Awesome Reviewers

Reviewer · Performance Optimization

Defer expensive imports

Heavy or slow-loading dependencies (e.g. litellm, mcp) must not be imported at module level if they add significant startup cost (network calls, large model-cost maps, multi-second load times). Instead, defer the import to the point of use:

Instruction

Heavy or slow-loading dependencies (e.g. litellm, mcp) must not be imported at module level if they add significant startup cost (network calls, large model-cost maps, multi-second load times). Instead, defer the import to the point of use:

  • For runtime usage, import inside the function/method that needs it, and cache the result in a module-level global guarded by a None check so the cost is paid only once and existing test patches on the module attribute keep working: ```python completion = None

def _bind_litellm(): global completion if completion is None: from litellm import completion as _completion completion = _completion ```

  • For type-only usage, import under if TYPE_CHECKING: so the dependency is never loaded at runtime at all.

Document the reason with a short comment (e.g. “Deferred: litellm must not load at import swarms time”) so future contributors don’t “fix” it by moving the import back to module scope. Expect static analyzers (e.g. Pyre) to flag these as unresolved in their own environment when dependencies aren’t installed there — this is a tooling limitation, not a code defect, and is not a reason to revert the deferral.

More generally, avoid paying for expensive work (parsing, scanning, deserialization) when it isn’t needed: use cheap pre-checks to short-circuit before costly operations, and compute derived values only inside the branch where the result is actually used, rather than unconditionally on every iteration.