cd /news/ai-agents/agentic-runtime-python-startup-time-… · home › topics › ai-agents › article
[ARTICLE · art-143747] src=enjii.pages.dev ↗ pub= topic=ai-agents verified=true sentiment=↑ positive

Agentic Runtime: Python Startup Time and Import Optimization (4s –> 2s)

A developer optimized the cold-start time of the Vex agentic runtime from roughly 4 seconds to about 2 seconds by deferring heavy imports, using TYPE_CHECKING guards for annotation-only imports, and moving modules such as langgraph into function-scoped on-demand imports. Profiling with tuna and Python's -X importtime identified the bottleneck, including a single `from langgraph.graph.state import END` import that consumed 1.36 seconds. Utility commands like --help and --ls now bypass main program initialization entirely by short-circuiting before the heavy engine imports.

read3 min views1 publishedOct 2, 2026

Here's where I started about 4s of cold start for both utility functions and main vex program - just disgusting speed for a cutting-edge agentic runtime.

The difference between main app and utility functions #

All utility functions are a shortcuts that usually avoid main program functionality, for example

  • --help - just show help message
  • --ls - list session, touch only sqlite database

Obviously such functions should work almost immediately. How to speed up utility functionality in this case ? Just bypass main program initialization:

async def run_agent(args):

    if args.init:
        init_workspace(console=console)
        return
    if args.list_sessions:
        await list_sessions(settings,console)
        return

    from vex_shell.engine import core
    from vex_shell.engine.agent import Agent

    tools,mcp_manager = await core.get_tools(settings)
    graph = core.build_graph(settings, tools)
    agent = await Agent.create(graph, args.session, settings, mcp_manager)

All heavy imports occur after utility args checking(from vex_shell.engine import core etc).

Main App Startup Optimization #

Okay let's consider that we have blazingly fast utility functions, but what about main app startup ? Using some extremely useful tools, like tuna and -X importtime. I have investigated which modules slow down startup. Tuna generated visual map showing the exact time required for module imports.

Based on this map, I found several bottlenecks in the codebase.

Type Annotation-Only Imports

During development, module-level imports are often added purely for type annotations:

from vex_shell.utils.config import Settings

Even if Settings is used for type hints only, the entire config module, will still be loaded. To prevent this unexpected behavior, we can use the following pattern:

from __future__ import annotations
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from vex_shell.engine.agent import Agent
    from rich.console import Console
    from vex_shell.utils.config import Settings

TYPE_CHECKING evaluates to False during runtime, ensuring these imports are used strictly for static type checking during development. This yields zero resource overhead during execution.

Function-Scoped Imports

Usually we just import most of modules at the top of .py file, but in this case we transfer all imports to application start time. Which isn't always a good choice. Alternatively we can use on-demand imports, thus import module during function call. Of course it will slow down function call time, but we can achieve shorter start time, moving delays to runtime, and it's also worth noting that any imports loaded only on the first call, after modules are already in cache.

def build_graph(settings: Settings, tools:List[BaseTool]) -> StateGraph:
    from langgraph.graph.state import  StateGraph
    from langgraph.prebuilt import ToolNode

    from .nodes import llm_call_node, verification_node, should_continue, should_verify
    from .state import State

    tool_names = [e.name for e in tools]
    
    graph = StateGraph(State)
    
    graph.add_node(
        "llm_call", partial(llm_call_node, settings=settings, tool_names=tool_names)
    )
    graph.add_node("tool_node", ToolNode(tools))
    graph.add_node("verification_node", partial(verification_node, settings=settings))

Fun to note

In nodes.py I used from langgraph.graph.state import END only for END constant, but it leads to a whole module import which consumes 1.36s. By just replacing this import with END = "__end__" I saved this time.

Approximate results #

  1. MCPManager - imported only if defined in CONFIG.toml, saves 1 second.
  2. Move all annotation-only imports into a TYPE_CHECKING guard.
  3. Move all heavy imports inside functions (ChatOpenAI ,langchain.messages ,etc)

Timings #

  • Utility functions - -ls/--help/--reset ~400ms
  • Main app start time (without MCP tools) ~2.1 seconds
  • Main app start time (with MCP tools) ~2.7 seconds

Tuna diagrams here #

P.S. #

VEX shell source code: https://codeberg.org/Enji/vex

Thanks for reading, Bye ₍^. .^₎⟆

── more in #ai-agents 4 stories · sorted by recency
── more on @vex 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/agentic-runtime-pyth…] indexed:0 read:3min 2026-10-02 · —