robotsix_chat.llm.agent
LLM chat agent backed by robotsix-llmio's per-level model factory.
:class:LlmioChatAgent satisfies the chat server's ChatAgent protocol
(async def stream(message) -> AsyncIterator[str]). It selects the backend
purely from a capability level via
:func:robotsix_llmio.config.create_model: the level encodes the combined
provider-model identifier (resolved from llmio's tier config), so this
package never names a concrete provider class or model.
Levels (1 cheap/frequent, 2 workhorse, 3 frontier) are a pure capability
axis. Provider redundancy is llmio's failover axis: every turn runs through
:func:robotsix_llmio.core.failover.acall_with_failover, which serves the
level on the keyless default slot (Claude SDK via the logged-in claude
CLI) and retries the SAME level on the keyed OpenRouter fallback slot when
the default fails in a provider-shaped way — arming a sticky failover window
after repeated failures.
Responses are returned as a single block (not token-streamed): llmio's Claude
SDK model does not support incremental streaming through pydantic-ai, so each
stream call yields the full reply once. The chat server still frames it as a
normal SSE token + done sequence.
The provider dependencies are obtained through robotsix-llmio's own extras —
robotsix-llmio[claude-sdk] and robotsix-llmio[openrouter] —
wired via this package's claude-sdk / openrouter extras.
LlmioChatAgent
Stream LLM responses via robotsix-llmio, selected by capability level.
Each stream call builds a fresh llmio agent handle (deterministically
closed). When a :class:~robotsix_chat.memory.ChatMemory is supplied, the
agent gains continuity across calls: it recalls relevant memory before
replying and persists the exchange afterwards (the write runs in the
background so it never adds latency). With the default :class:NullMemory
it stays fully stateless.
Source code in src/robotsix_chat/llm/agent.py
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 | |
has_api_key
property
Whether a non-empty API key is configured for keyed levels.
Keyless (claudeSDK) levels never need it; keyed (OpenRouter) levels cannot serve a turn without it. Surfaced so the UI can mark keyed model levels unavailable when no key is present.
memory
property
The agent's memory backend (for health reporting / recovery wiring).
model_level
property
The level this agent was configured with.
Read-only: a session's escalated level is passed per turn via
stream(model_level=...) rather than mutating the shared agent.
__init__(*, model_level, instruction, api_key='', anthropic_api_key='', memory=None, tools=None, request_tools_factory=None, event_sink=None, failover_window_seconds=None, tier_overrides=None)
Store the agent configuration for later stream calls.
api_key is the configured OpenRouter key, if any. It is forwarded only to keyed (OpenRouter) slot attempts — holding it is what lets llmio's provider failover reach the keyed fallback slot when the shared Claude credential or subscription quota is the thing that failed.
anthropic_api_key is the configured Anthropic API key, if any. It
is forwarded only to slots served by the direct Anthropic Messages
API provider (anthropic). When empty, llmio's provider falls
back to the ANTHROPIC_API_KEY environment variable.
failover_window_seconds, when set, overrides how long llmio routes
calls straight to the fallback (OpenRouter) slot after the default
(Claude) slot fails repeatedly, before automatically returning to
the default. None keeps llmio's baked policy (15 minutes).
request_tools_factory is called once per stream invocation with
the request's client_id to produce per-request tools (e.g. the
subsession tools whose closures capture that session id). It keeps
the module dependency acyclic: those tools are built fresh per
request inside stream, not baked into the shared agent.
event_sink, when given, receives an activity frame (see
:func:robotsix_chat.chat.events.activity_frame) for every tool
call, tool result, thinking block, or intermediate assistant text
the claudeSDK backend streams during a turn — live feedback on what
the agent is doing while the final reply is still pending. A
non-claudeSDK level (e.g. an OpenRouter tier) simply never triggers
it: robotsix_llmio.claude_sdk.activity_events() is a no-op unless
the resolved transport is the Claude Agent SDK.
Source code in src/robotsix_chat/llm/agent.py
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 | |
stream(message, *, history=None, session_id=None, client_id=None, images=None, trace_metadata=None, trace_name=None, model_level=None, skip_recall=False)
async
Yield the assistant's reply to message as a single block.
skip_recall suppresses the automatic long-term memory recall for
this turn. It is set by callers whose turn input is machine-generated
boilerplate rather than an operator utterance — chiefly event-driven
and periodic monitor wakes, whose prompts are self-contained by
design and whose fixed instruction text recalls near-random,
noise-only matches while hammering the memory component on every
wake. Interactive operator turns leave it False and keep full
recall.
history is the prior (user, assistant) turns of the current
conversation, replayed to the agent so it has multi-turn context.
session_id groups this run's trace spans under one conversation in
Langfuse (a fresh id starts a new trace). client_id identifies the
owning browser — it is forwarded to the per-request tools factory so
delegation tools can tag spawned tasks correctly. images is an
optional list of (media_type, raw_bytes) pairs (e.g.
[("image/png", b"...")]) — attachments are handed to llmio's
build_agent(images=...) seam: the Claude transport reads them
natively, while text-only OpenRouter models get an ask_image
tool (answered by the tier config's vision binding) instead. Images
belong to the turn they arrive with only — replayed history is
text-only by construction, so an old attachment can never poison a
later turn.
trace_name is an optional human-readable label for the Langfuse
trace (e.g. "chat-turn", "subsession-turn") so cost can be
attributed by function. When None the trace inherits whatever
name the calling context set (if any); this keeps the feedback
runner's feedback-<type> trace name from being overwritten.
All keyword arguments are optional — with none, the agent behaves as a
single stateless query.
trace_metadata is stamped as span attributes for observability (parent/owner lineage, subsession ids, etc.).
model_level overrides the agent's configured level for this turn only. The chat server passes a session's escalated level here, so one shared agent instance can serve sessions running at different tiers without rebuilding it.
Transient upstream errors (OpenRouter provider failures, 5xx,
network blips) are retried locally per slot attempt. Provider-shaped
failures that outlive those retries (usage exhaustion, a dead
credential, an outage) are handed to llmio's provider failover,
which retries the SAME level on the other provider slot and arms a
sticky failover window on repeated default-slot failures. Only when
both slots fail does the error surface — the chat server turns that
into an SSE error frame.
Source code in src/robotsix_chat/llm/agent.py
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 | |